Skip to content

Commit ab46a63

Browse files
authored
Respect Store Configuration During Incremental Updates (#175)
1 parent d67befa commit ab46a63

File tree

5 files changed

+204
-82
lines changed

5 files changed

+204
-82
lines changed

app/code/Meta/Catalog/Observer/Product/DeleteAfter.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
namespace Meta\Catalog\Observer\Product;
1919

2020
use Exception;
21+
use GuzzleHttp\Exception\GuzzleException;
22+
use Magento\Catalog\Api\Data\ProductInterface;
2123
use Meta\BusinessExtension\Helper\FBEHelper;
2224
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2325
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
24-
use Magento\Catalog\Model\Product;
2526
use Magento\Framework\Event\Observer;
2627
use Magento\Framework\Event\ObserverInterface;
2728
use Meta\Catalog\Helper\Product\Identifier;
29+
use Magento\Framework\Message\ManagerInterface;
2830

2931
class DeleteAfter implements ObserverInterface
3032
{
@@ -36,34 +38,42 @@ class DeleteAfter implements ObserverInterface
3638
/**
3739
* @var GraphAPIAdapter
3840
*/
39-
protected $graphApiAdapter;
41+
private $graphApiAdapter;
4042

4143
/**
4244
* @var FBEHelper
4345
*/
44-
protected $fbeHelper;
46+
private $fbeHelper;
4547

4648
/**
4749
* @var Identifier
4850
*/
4951
private $identifier;
5052

53+
/**
54+
* @var ManagerInterface
55+
*/
56+
private $messageManager;
57+
5158
/**
5259
* @param SystemConfig $systemConfig
5360
* @param GraphAPIAdapter $graphApiAdapter
5461
* @param FBEHelper $fbeHelper
5562
* @param Identifier $identifier
63+
* @param ManagerInterface $messageManager
5664
*/
5765
public function __construct(
5866
SystemConfig $systemConfig,
5967
GraphApiAdapter $graphApiAdapter,
6068
FBEHelper $fbeHelper,
61-
Identifier $identifier
69+
Identifier $identifier,
70+
ManagerInterface $messageManager,
6271
) {
6372
$this->systemConfig = $systemConfig;
6473
$this->graphApiAdapter = $graphApiAdapter;
6574
$this->fbeHelper = $fbeHelper;
6675
$this->identifier = $identifier;
76+
$this->messageManager = $messageManager;
6777
}
6878

6979
/**
@@ -76,28 +86,53 @@ public function __construct(
7686
*/
7787
public function execute(Observer $observer)
7888
{
79-
if (!($this->systemConfig->isActiveExtension() && $this->systemConfig->isActiveIncrementalProductUpdates())) {
80-
return;
81-
}
82-
83-
/** @var Product $product */
8489
$product = $observer->getEvent()->getProduct();
90+
8591
if (!$product->getId()) {
8692
return;
8793
}
8894

89-
// @todo observer should not know how to assemble request
90-
$requestData = [
91-
'method' => 'DELETE',
92-
'data' => ['id' => $this->identifier->getMagentoProductRetailerId($product)],
93-
];
95+
$stores = $this->systemConfig->getStoreManager()->getStores();
96+
97+
foreach ($stores as $store) {
98+
$this->deleteProduct($store->getId(), $product);
99+
}
100+
}
101+
102+
/**
103+
* Process Product Delete from Meta Catalog
104+
*
105+
* @param int $storeId
106+
* @param ProductInterface $product
107+
* @return void
108+
*/
109+
private function deleteProduct($storeId, $product): void
110+
{
111+
$isActive = $this->systemConfig->isActiveExtension($storeId);
112+
$shouldIncrement = $this->systemConfig->isActiveIncrementalProductUpdates($storeId);
113+
114+
if (!($isActive && $shouldIncrement)) {
115+
return;
116+
}
94117

95118
try {
96-
$storeId = $product->getStoreId();
119+
// @todo observer should not know how to assemble request
120+
$requestData = [
121+
'method' => 'DELETE',
122+
'data' => ['id' => $this->identifier->getMagentoProductRetailerId($product)],
123+
];
124+
97125
$catalogId = $this->systemConfig->getCatalogId($storeId);
98126
$this->graphApiAdapter->catalogBatchRequest($catalogId, [$requestData]);
127+
} catch (GuzzleException $e) {
128+
$this->messageManager->addErrorMessage(
129+
'Error deleting product from one or more Meta Catalogs. Please check setup and try again'
130+
);
131+
$this->fbeHelper->logException($e);
132+
return;
99133
} catch (Exception $e) {
100134
$this->fbeHelper->logException($e);
135+
return;
101136
}
102137
}
103138
}

app/code/Meta/Catalog/Observer/Product/SaveAfter.php

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
namespace Meta\Catalog\Observer\Product;
1919

2020
use Exception;
21+
use GuzzleHttp\Exception\GuzzleException;
22+
use Magento\Catalog\Api\ProductRepositoryInterface;
23+
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;
24+
use Magento\Framework\Exception\NoSuchEntityException;
2125
use Meta\BusinessExtension\Helper\FBEHelper;
2226
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2327
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2428
use Meta\Catalog\Model\Product\Feed\Method\BatchApi;
25-
use Magento\Catalog\Model\Product;
2629
use Magento\Framework\Event\Observer;
2730
use Magento\Framework\Event\ObserverInterface;
31+
use Magento\Framework\Message\ManagerInterface;
2832

2933
class SaveAfter implements ObserverInterface
3034
{
@@ -36,34 +40,50 @@ class SaveAfter implements ObserverInterface
3640
/**
3741
* @var FBEHelper
3842
*/
39-
protected $fbeHelper;
43+
private $fbeHelper;
4044

4145
/**
4246
* @var BatchApi
4347
*/
44-
protected $batchApi;
48+
private $batchApi;
4549

4650
/**
4751
* @var GraphAPIAdapter
4852
*/
49-
protected $graphApiAdapter;
53+
private $graphApiAdapter;
54+
55+
/**
56+
* @var ManagerInterface
57+
*/
58+
private $messageManager;
59+
60+
/**
61+
* @var ProductRepositoryInterface
62+
*/
63+
private $productRepo;
5064

5165
/**
5266
* @param SystemConfig $systemConfig
5367
* @param FBEHelper $helper
5468
* @param BatchApi $batchApi
5569
* @param GraphAPIAdapter $graphApiAdapter
70+
* @param ManagerInterface $messageManager
71+
* @param ProductRepositoryInterface $productRepo
5672
*/
5773
public function __construct(
5874
SystemConfig $systemConfig,
5975
FBEHelper $helper,
6076
BatchApi $batchApi,
61-
GraphAPIAdapter $graphApiAdapter
77+
GraphAPIAdapter $graphApiAdapter,
78+
ManagerInterface $messageManager,
79+
ProductRepositoryInterface $productRepo
6280
) {
6381
$this->systemConfig = $systemConfig;
6482
$this->fbeHelper = $helper;
6583
$this->batchApi = $batchApi;
6684
$this->graphApiAdapter = $graphApiAdapter;
85+
$this->messageManager = $messageManager;
86+
$this->productRepo = $productRepo;
6787
}
6888

6989
/**
@@ -72,41 +92,69 @@ public function __construct(
7292
* Call an API to product save from facebook catalog
7393
* after save product from Magento
7494
*
75-
* @todo Take into consideration current store scope
7695
* @param Observer $observer
7796
*/
7897
public function execute(Observer $observer)
7998
{
80-
if (!($this->systemConfig->isActiveExtension() && $this->systemConfig->isActiveIncrementalProductUpdates())) {
99+
$product = $observer->getEvent()->getProduct();
100+
$pid = $product->getId();
101+
102+
if (!$pid) {
81103
return;
82104
}
83105

84-
/** @var Product $product */
85-
$product = $observer->getEvent()->getProduct();
86-
if (!$product->getId()) {
87-
return;
106+
$stores = $this->systemConfig->getStoreManager()->getStores(false, true);
107+
108+
foreach ($stores as $store) {
109+
$this->updateProduct($store->getId(), $pid);
88110
}
111+
}
112+
113+
/**
114+
* Process product update
115+
*
116+
* @param int $storeId
117+
* @param int $productId
118+
* @return void
119+
*/
120+
private function updateProduct($storeId, $productId): void
121+
{
122+
$isActive = $this->systemConfig->isActiveExtension($storeId);
123+
$shouldIncrement = $this->systemConfig->isActiveIncrementalProductUpdates($storeId);
89124

90-
if ($product->getSendToFacebook() === \Magento\Eav\Model\Entity\Attribute\Source\Boolean::VALUE_NO) {
125+
if (!($isActive && $shouldIncrement)) {
91126
return;
92127
}
93128

94-
$productStoreId = $product->getStoreId();
95-
$storeId = $this->fbeHelper->getStore()->getId();
96-
$product->setStoreId($storeId);
129+
try {
130+
$product = $this->productRepo->getById($productId, false, $storeId, true);
97131

98-
// @todo implement error handling/logging for invalid access token and other non-happy path scenarios
99-
// @todo implement batch API status check
100-
// @todo implement async call
132+
if (!$product->getSendToFacebook()) {
133+
return;
134+
}
135+
136+
// @todo implement error handling/logging for invalid access token and other non-happy path scenarios
137+
// @todo implement batch API status check
138+
// @todo implement async call
101139

102-
try {
103140
$catalogId = $this->systemConfig->getCatalogId($storeId);
104141
$requestData = $this->batchApi->buildRequestForIndividualProduct($product);
105142
$this->graphApiAdapter->catalogBatchRequest($catalogId, [$requestData]);
143+
} catch (NoSuchEntityException $e) {
144+
$this->messageManager->addErrorMessage(
145+
'Failed to update Meta for one or more stores. Please see Exception log for more detail.'
146+
);
147+
$this->fbeHelper->logException($e);
148+
return;
149+
} catch (GuzzleException $e) {
150+
$this->messageManager->addNoticeMessage(
151+
'Error sending Increment Update To Meta. Please check your Store Connection Settings.'
152+
);
153+
$this->fbeHelper->logException($e);
154+
return;
106155
} catch (Exception $e) {
107156
$this->fbeHelper->logException($e);
157+
return;
108158
}
109-
110-
$product->setStoreId($productStoreId);
111159
}
112160
}

app/code/Meta/Catalog/Test/Unit/Observer/ProcessProductAfterDeleteEventObserverTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace Meta\Catalog\Test\Unit\Observer;
1919

20+
use Magento\Store\Api\Data\StoreInterface;
21+
use Magento\Store\Model\StoreManagerInterface;
2022
use Meta\BusinessExtension\Helper\FBEHelper;
2123
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2224
use Meta\BusinessExtension\Model\System\Config;
@@ -27,6 +29,7 @@
2729
use Magento\Framework\Event\Observer;
2830
use PHPUnit\Framework\TestCase;
2931
use PHPUnit\Framework\MockObject\MockObject;
32+
use Magento\Framework\Message\ManagerInterface;
3033

3134
class ProcessProductAfterDeleteEventObserverTest extends TestCase
3235
{
@@ -65,6 +68,11 @@ class ProcessProductAfterDeleteEventObserverTest extends TestCase
6568
*/
6669
private $identifier;
6770

71+
/**
72+
* @var MockObject
73+
*/
74+
private $messageManager;
75+
6876
/**
6977
* Used to set the values before running a test
7078
*
@@ -78,17 +86,25 @@ public function setUp(): void
7886
$this->_product->expects($this->atLeastOnce())->method('getId')->will($this->returnValue("1234"));
7987
$this->_product->expects($this->never())->method('getSku');
8088

89+
$storeManager = $this->createMock(StoreManagerInterface::class);
90+
$store = $this->createMock(StoreInterface::class);
91+
$this->systemConfig->method('getStoreManager')->willReturn($storeManager);
92+
$storeManager->method('getStores')->willReturn([$store]);
93+
$store->method('getId')->willReturn('1');
94+
8195
$event = $this->getMockBuilder(Event::class)->addMethods(['getProduct'])->getMock();
8296
$event->expects($this->once())->method('getProduct')->will($this->returnValue($this->_product));
8397
$this->_eventObserverMock = $this->createMock(Observer::class);
8498
$this->_eventObserverMock->expects($this->once())->method('getEvent')->will($this->returnValue($event));
8599
$this->_graphApi = $this->createMock(GraphAPIAdapter::class);
86100
$this->identifier = $this->createMock(Identifier::class);
101+
$this->messageManager = $this->createMock(ManagerInterface::class);
87102
$this->processProductAfterDeleteEventObserver = new ProcessProductAfterDeleteEventObserver(
88103
$this->systemConfig,
89104
$this->_graphApi,
90105
$this->fbeHelper,
91-
$this->identifier
106+
$this->identifier,
107+
$this->messageManager,
92108
);
93109
}
94110

0 commit comments

Comments
 (0)