Skip to content

Commit d650257

Browse files
authored
Merge pull request #12 from channelengine/v1.3.6-gm
Adobe Marketplace submission related changes
2 parents 29fd9ec + 8e57a52 commit d650257

12 files changed

Lines changed: 305 additions & 73 deletions

File tree

Api/StatusApiInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ChannelEngine\Magento2\Api;
4+
5+
interface StatusApiInterface
6+
{
7+
/**
8+
* Get status of the ChannelEngine module
9+
*
10+
* @return string
11+
*/
12+
public function getStatus();
13+
}

Helper/ProductHelper.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php namespace ChannelEngine\Magento2\Helper;
2+
3+
use Magento\Framework\Stdlib\DateTime\DateTime;
4+
5+
class ProductHelper
6+
{
7+
/**
8+
* @var DateTime
9+
*/
10+
private DateTime $dateTime;
11+
12+
private const UPDATE_THRESHOLD_SECONDS = 10;
13+
14+
/**
15+
* @param DateTime $dateTime
16+
*/
17+
public function __construct(DateTime $dateTime)
18+
{
19+
$this->dateTime = $dateTime;
20+
}
21+
22+
/**
23+
* Check if the product was updated recently (within the last 10 seconds).
24+
*
25+
* @param $product
26+
* @return bool
27+
* @throws \Exception
28+
*/
29+
public function wasUpdatedRecently($product): bool
30+
{
31+
$existingCeUpdatedAt = $product->getCustomAttribute('ce_updated_at');
32+
33+
// If the attribute doesn't exist, treat it as not updated recently
34+
if (!$existingCeUpdatedAt || !$existingCeUpdatedAt->getValue()) {
35+
return false;
36+
}
37+
38+
$lastUpdatedAt = $existingCeUpdatedAt->getValue();
39+
$currentTime = $this->dateTime->gmtTimestamp();
40+
41+
$productUpdatedAtTime = strtotime($lastUpdatedAt);
42+
43+
if ($productUpdatedAtTime === false) {
44+
return false;
45+
}
46+
47+
return ($currentTime - $productUpdatedAtTime) <= self::UPDATE_THRESHOLD_SECONDS;
48+
}
49+
}

Model/StatusApi.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ChannelEngine\Magento2\Model;
4+
5+
use ChannelEngine\Magento2\Api\StatusApiInterface;
6+
7+
class StatusApi implements StatusApiInterface
8+
{
9+
public function getStatus()
10+
{
11+
return "OK";
12+
}
13+
}

Observer/BulkProductObserver.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function execute(Observer $observer)
3939
$storeId = $this->storeManager->getStore()->getId();
4040

4141
$this->updateCeAttribute($productIds, $storeId);
42-
42+
4343
$this->logger->info('Updated ce_updated_at field for the following product ids:' . json_encode($productIds));
4444
} catch (\Exception $e) {
4545
$this->logger->error('Updating ce_updated_at field was unsuccessful', ['exception' => $e]);
@@ -49,7 +49,7 @@ public function execute(Observer $observer)
4949
private function getSkusFromProducts(array $products)
5050
{
5151
$skus = [];
52-
52+
5353
if (!is_array($products))
5454
return $skus;
5555

@@ -62,13 +62,13 @@ private function getSkusFromProducts(array $products)
6262
return $skus;
6363
}
6464

65-
private function getProductIds(array $skus)
65+
private function getProductIds(array $skus)
6666
{
6767
$searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $skus, 'in')->create();
6868
$entities = $this->productRepository->getList($searchCriteria)->getItems();
69-
69+
7070
$ids = [];
71-
71+
7272
if (!is_array($entities))
7373
return $ids;
7474

@@ -79,9 +79,9 @@ private function getProductIds(array $skus)
7979
return $ids;
8080
}
8181

82-
private function updateCeAttribute(array $productIds, $storeId)
82+
private function updateCeAttribute(array $productIds, $storeId)
8383
{
8484
$date = date('Y-m-d H:i:s');
8585
$this->massAction->updateAttributes($productIds, array('ce_updated_at' => $date), $storeId);
8686
}
87-
}
87+
}

Observer/ProductObserver.php

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,63 @@
11
<?php namespace ChannelEngine\Magento2\Observer;
22

3+
use Magento\Framework\Event\Observer;
34
use Magento\Framework\Event\ObserverInterface;
5+
use Magento\Framework\Stdlib\DateTime\DateTime;
6+
use Psr\Log\LoggerInterface;
7+
use ChannelEngine\Magento2\Helper\ProductHelper;
48

59
class ProductObserver implements ObserverInterface
610
{
7-
public function __construct()
11+
/**
12+
* @var DateTime
13+
*/
14+
private DateTime $dateTime;
15+
/**
16+
* @var LoggerInterface
17+
*/
18+
private LoggerInterface $logger;
19+
/**
20+
* @var ProductHelper
21+
*/
22+
private ProductHelper $productHelper;
23+
24+
/**
25+
* @param LoggerInterface $logger
26+
* @param DateTime $dateTime
27+
* @param ProductHelper $productHelper
28+
*/
29+
public function __construct(
30+
LoggerInterface $logger,
31+
DateTime $dateTime,
32+
ProductHelper $productHelper
33+
)
834
{
9-
35+
$this->dateTime = $dateTime;
36+
$this->logger = $logger;
37+
$this->productHelper = $productHelper;
1038
}
1139

12-
public function execute(\Magento\Framework\Event\Observer $observer)
40+
/**
41+
* @param Observer $observer
42+
* @return void
43+
*/
44+
public function execute(Observer $observer)
1345
{
14-
$product = $observer->getProduct();
15-
$date = date('Y-m-d H:i:s');
16-
$attr = 'ce_updated_at';
46+
try {
47+
$product = $observer->getProduct();
48+
49+
if ($this->productHelper->wasUpdatedRecently($product)) {
50+
return;
51+
}
52+
53+
$date = $this->dateTime->gmtDate();
54+
$attr = 'ce_updated_at';
1755

18-
// Set both: https://magento.stackexchange.com/a/229280
19-
$product->setData($attr, $date);
20-
$product->setCustomAttribute($attr, $date);
56+
// Set both: https://magento.stackexchange.com/a/229280
57+
$product->setData($attr, $date);
58+
$product->setCustomAttribute($attr, $date);
59+
} catch (\Exception $e) {
60+
$this->logger->error($e->getMessage());
61+
}
2162
}
22-
}
63+
}

Observer/StockItemObserver.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,76 @@
11
<?php namespace ChannelEngine\Magento2\Observer;
22

3+
use Magento\Framework\Event\Observer;
34
use Magento\Framework\Event\ObserverInterface;
45
use Magento\Catalog\Model\ProductRepository;
6+
use Magento\Framework\Stdlib\DateTime\DateTime;
7+
use Psr\Log\LoggerInterface;
8+
use ChannelEngine\Magento2\Helper\ProductHelper;
59

610
class StockItemObserver implements ObserverInterface
711
{
812
/**
913
* @var ProductRepository
1014
*/
11-
private $productRepository;
15+
private ProductRepository $productRepository;
16+
/**
17+
* @var LoggerInterface
18+
*/
19+
private LoggerInterface $logger;
20+
/**
21+
* @var DateTime
22+
*/
23+
private DateTime $dateTime;
24+
/**
25+
* @var ProductHelper
26+
*/
27+
private ProductHelper $productHelper;
1228

13-
public function __construct(ProductRepository $productRepository)
29+
/**
30+
* @param ProductRepository $productRepository
31+
* @param LoggerInterface $logger
32+
* @param DateTime $dateTime
33+
* @param ProductHelper $productHelper
34+
*/
35+
public function __construct(
36+
ProductRepository $productRepository,
37+
LoggerInterface $logger,
38+
DateTime $dateTime,
39+
ProductHelper $productHelper
40+
)
1441
{
1542
$this->productRepository = $productRepository;
43+
$this->logger = $logger;
44+
$this->dateTime = $dateTime;
45+
$this->productHelper = $productHelper;
1646
}
1747

18-
public function execute(\Magento\Framework\Event\Observer $observer)
48+
/**
49+
* @param Observer $observer
50+
* @return void
51+
*/
52+
public function execute(Observer $observer)
1953
{
20-
$stockItem = $observer->getItem();
21-
$productId = $stockItem->getProductId();
22-
$product = $this->productRepository->getById($productId);
23-
$date = date('Y-m-d H:i:s');
24-
$attr = 'ce_updated_at';
25-
26-
// Set both: https://magento.stackexchange.com/a/229280
27-
$product->setData($attr, $date);
28-
$product->setCustomAttribute($attr, $date);
29-
30-
// Save only the attribute, to prevent cyclic events (when already performing a product save)
31-
$product->getResource()->saveAttribute($product, $attr);
54+
try {
55+
$stockItem = $observer->getItem();
56+
$productId = $stockItem->getProductId();
57+
$product = $this->productRepository->getById($productId);
58+
59+
if ($this->productHelper->wasUpdatedRecently($product)) {
60+
return;
61+
}
62+
63+
$date = $this->dateTime->gmtDate();
64+
$attr = 'ce_updated_at';
65+
66+
// Set both: https://magento.stackexchange.com/a/229280
67+
$product->setData($attr, $date);
68+
$product->setCustomAttribute($attr, $date);
69+
70+
// Save only the attribute, to prevent cyclic events (when already performing a product save)
71+
$product->getResource()->saveAttribute($product, $attr);
72+
} catch (\Exception $e) {
73+
$this->logger->error($e->getMessage());
74+
}
3275
}
33-
}
76+
}

Setup/Patch/Data/ChannelEngine.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php namespace ChannelEngine\Magento2\Setup\Patch\Data;
2+
3+
use Magento\Framework\Setup\ModuleDataSetupInterface;
4+
use Magento\Framework\Setup\Patch\DataPatchInterface;
5+
use Magento\Catalog\Model\Product;
6+
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
7+
use Magento\Eav\Setup\EavSetupFactory;
8+
9+
class ChannelEngine implements DataPatchInterface
10+
{
11+
/**
12+
* @var ModuleDataSetupInterface
13+
*/
14+
private $setup;
15+
16+
/**
17+
* @var EavSetupFactory
18+
*/
19+
protected $eavSetupFactory;
20+
/**
21+
* @var array|array[]
22+
*/
23+
private $productAttributes;
24+
25+
/**
26+
* @param ModuleDataSetupInterface $setup
27+
* @param EavSetupFactory $eavSetupFactory
28+
*/
29+
public function __construct(
30+
ModuleDataSetupInterface $setup,
31+
EavSetupFactory $eavSetupFactory
32+
) {
33+
$this->setup = $setup;
34+
$this->eavSetupFactory = $eavSetupFactory;
35+
36+
$this->productAttributes = [
37+
'ce_updated_at' => [
38+
'type' => 'datetime',
39+
'backend' => '',
40+
'frontend' => '',
41+
'label' => 'ChannelEngine last product update',
42+
'input' => 'date',
43+
'class' => '',
44+
'source' => '',
45+
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
46+
'visible' => false,
47+
'required' => false,
48+
'user_defined' => false,
49+
'default' => 0,
50+
'searchable' => false,
51+
'filterable' => false,
52+
'comparable' => false,
53+
'visible_on_front' => false,
54+
'used_in_product_listing' => false,
55+
'unique' => false,
56+
'apply_to' => ''
57+
],
58+
];
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
public function apply()
65+
{
66+
$this->setup->startSetup();
67+
68+
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->setup]);
69+
70+
foreach ($this->productAttributes as $attr => $config) {
71+
$eavSetup->addAttribute(Product::ENTITY, $attr, $config);
72+
}
73+
74+
$this->setup->endSetup();
75+
}
76+
77+
/**
78+
* @inheritdoc
79+
*/
80+
public function getAliases()
81+
{
82+
return [];
83+
}
84+
85+
/**
86+
* @inheritdoc
87+
*/
88+
public static function getDependencies()
89+
{
90+
return [];
91+
}
92+
}

0 commit comments

Comments
 (0)