Skip to content

Commit 6b8e8bf

Browse files
authored
Decouple modules (#421)
* Decouple modules * fixes
1 parent 1c3c78d commit 6b8e8bf

File tree

7 files changed

+200
-56
lines changed

7 files changed

+200
-56
lines changed

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/PostFBEOnboardingSync.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
use Meta\BusinessExtension\Helper\FBEHelper;
2424
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
25-
use Meta\Catalog\Helper\CatalogSyncHelper;
2625
use Magento\Backend\App\Action\Context;
27-
use Meta\Sales\Plugin\ShippingSyncer;
2826
use Magento\Framework\Controller\Result\JsonFactory;
27+
use Magento\Framework\Event\ManagerInterface as EventManager;
28+
use Throwable;
2929

3030
class PostFBEOnboardingSync extends AbstractAjax
3131
{
@@ -43,14 +43,9 @@ class PostFBEOnboardingSync extends AbstractAjax
4343
private $systemConfig;
4444

4545
/**
46-
* @var CatalogSyncHelper
46+
* @var EventManager
4747
*/
48-
private $catalogSyncHelper;
49-
50-
/**
51-
* @var ShippingSyncer
52-
*/
53-
private $shippingSyncer;
48+
private EventManager $eventManager;
5449

5550
/**
5651
* Construct
@@ -59,22 +54,18 @@ class PostFBEOnboardingSync extends AbstractAjax
5954
* @param JsonFactory $resultJsonFactory
6055
* @param FBEHelper $fbeHelper
6156
* @param SystemConfig $systemConfig
62-
* @param CatalogSyncHelper $catalogSyncHelper
63-
* @param ShippingSyncer $shippingSyncer
6457
*/
6558
public function __construct(
66-
Context $context,
67-
JsonFactory $resultJsonFactory,
68-
FBEHelper $fbeHelper,
69-
SystemConfig $systemConfig,
70-
CatalogSyncHelper $catalogSyncHelper,
71-
ShippingSyncer $shippingSyncer
59+
Context $context,
60+
JsonFactory $resultJsonFactory,
61+
FBEHelper $fbeHelper,
62+
SystemConfig $systemConfig,
63+
EventManager $eventManager
7264
) {
7365
parent::__construct($context, $resultJsonFactory, $fbeHelper);
7466
$this->fbeHelper = $fbeHelper;
7567
$this->systemConfig = $systemConfig;
76-
$this->catalogSyncHelper = $catalogSyncHelper;
77-
$this->shippingSyncer = $shippingSyncer;
68+
$this->eventManager = $eventManager;
7869
}
7970

8071
/**
@@ -93,8 +84,7 @@ public function executeForJson(): array
9384
}
9485

9586
try {
96-
$store = $this->systemConfig->getStoreManager()->getStore($storeId);
97-
$storeName = $store->getName();
87+
$storeName = $this->systemConfig->getStoreManager()->getStore($storeId)->getName();
9888
if (!$this->systemConfig->getAccessToken($storeId)) {
9989
$response['success'] = false;
10090
$response['message'] = __(
@@ -110,15 +100,15 @@ public function executeForJson(): array
110100
return $response;
111101
}
112102

113-
// Immediately after onboarding we initiate full catalog sync.
114-
// It syncs all products and all categories to Meta Catalog
115-
$this->catalogSyncHelper->syncFullCatalog($storeId);
116-
$this->shippingSyncer->syncShippingProfiles('post_fbe_onboarding', $store);
103+
// Dispatch the facebook_post_fbe_onboarding_sync event,
104+
// so observers in other Meta modules can subscribe and trigger their syncs,
105+
// such as full catalog sync, and shipping profiles sync
106+
$this->eventManager->dispatch('facebook_fbe_onboarding_after', ['store_id' => $storeId]);
117107

118108
$response['success'] = true;
119109
$response['message'] = 'Post FBE Onboarding Sync successful';
120110
return $response;
121-
} catch (\Throwable $e) {
111+
} catch (Throwable $e) {
122112
$response['success'] = false;
123113
$response['message'] = $e->getMessage();
124114
$this->fbeHelper->logExceptionImmediatelyToMeta(

app/code/Meta/BusinessExtension/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"magento/module-eav": "*",
1414
"magento/module-security": "*",
1515
"magento/module-store": "*",
16-
"meta/module-catalog": "*",
1716
"facebook/php-business-sdk": "^15.0.0"
1817
},
1918
"autoload": {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Catalog\Observer\Facebook;
22+
23+
use Magento\Framework\Event\Observer;
24+
use Magento\Framework\Event\ObserverInterface;
25+
use Meta\BusinessExtension\Helper\FBEHelper;
26+
use Meta\Catalog\Helper\CatalogSyncHelper;
27+
use Throwable;
28+
29+
class SyncFullCatalog implements ObserverInterface
30+
{
31+
/**
32+
* @var FBEHelper
33+
*/
34+
private FBEHelper $fbeHelper;
35+
36+
/**
37+
* @var CatalogSyncHelper
38+
*/
39+
private CatalogSyncHelper $catalogSyncHelper;
40+
41+
/**
42+
* Constructor
43+
*
44+
* @param FBEHelper $fbeHelper
45+
* @param CatalogSyncHelper $catalogSyncHelper
46+
*/
47+
public function __construct(
48+
FBEHelper $fbeHelper,
49+
CatalogSyncHelper $catalogSyncHelper
50+
) {
51+
$this->fbeHelper = $fbeHelper;
52+
$this->catalogSyncHelper = $catalogSyncHelper;
53+
}
54+
55+
/**
56+
* Execute observer on FBE onboarding completion
57+
*
58+
* Immediately after onboarding we initiate full catalog sync.
59+
* It syncs all products and all categories to Meta Catalog
60+
*
61+
* @param Observer $observer
62+
*/
63+
public function execute(Observer $observer): void
64+
{
65+
$storeId = $observer->getEvent()->getStoreId();
66+
67+
try {
68+
$this->catalogSyncHelper->syncFullCatalog($storeId);
69+
} catch (Throwable $e) {
70+
$this->fbeHelper->logExceptionImmediatelyToMeta(
71+
$e,
72+
[
73+
'store_id' => $storeId,
74+
'event' => 'full_catalog_sync',
75+
'event_type' => 'meta_observer'
76+
]
77+
);
78+
}
79+
}
80+
}

app/code/Meta/Catalog/etc/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
<event name="catalog_category_delete_before">
77
<observer name="custom_delete_category" instance="Meta\Catalog\Observer\ProcessCategoryBeforeDeleteEventObserver" />
88
</event>
9+
<event name="facebook_fbe_onboarding_after">
10+
<observer name="facebook_sync_full_catalog" instance="Meta\Catalog\Observer\Facebook\SyncFullCatalog" />
11+
</event>
912
</config>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Observer\Facebook;
22+
23+
use Magento\Framework\Event\Observer;
24+
use Magento\Framework\Event\ObserverInterface;
25+
use Meta\BusinessExtension\Helper\FBEHelper;
26+
use Meta\Sales\Plugin\ShippingSyncer;
27+
use Throwable;
28+
29+
class SyncShippingProfiles implements ObserverInterface
30+
{
31+
/**
32+
* @var FBEHelper
33+
*/
34+
private FBEHelper $fbeHelper;
35+
36+
/**
37+
* @var ShippingSyncer
38+
*/
39+
private ShippingSyncer $shippingSyncer;
40+
41+
/**
42+
* Constructor
43+
*
44+
* @param FBEHelper $fbeHelper
45+
* @param ShippingSyncer $shippingSyncer
46+
*/
47+
public function __construct(
48+
FBEHelper $fbeHelper,
49+
ShippingSyncer $shippingSyncer
50+
) {
51+
$this->fbeHelper = $fbeHelper;
52+
$this->shippingSyncer = $shippingSyncer;
53+
}
54+
55+
/**
56+
* Execute observer on FBE onboarding completion
57+
*
58+
* Immediately after onboarding we initiate full catalog sync.
59+
* It syncs all products and all categories to Meta Catalog
60+
*
61+
* @param Observer $observer
62+
*/
63+
public function execute(Observer $observer): void
64+
{
65+
$storeId = $observer->getEvent()->getStoreId();
66+
67+
try {
68+
$this->shippingSyncer->syncShippingProfiles('meta_observer');
69+
} catch (Throwable $e) {
70+
$this->fbeHelper->logExceptionImmediatelyToMeta(
71+
$e,
72+
[
73+
'store_id' => $storeId,
74+
'event' => 'shipping_profiles_sync',
75+
'event_type' => 'meta_observer'
76+
]
77+
);
78+
}
79+
}
80+
}

app/code/Meta/Sales/Plugin/ShippingSyncer.php

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
namespace Meta\Sales\Plugin;
2222

2323
use Exception;
24-
use Magento\Framework\Filesystem;
25-
use Magento\Store\Api\Data\StoreInterface;
2624
use Magento\Store\Model\StoreManagerInterface;
2725
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2826
use Meta\BusinessExtension\Helper\FBEHelper;
@@ -40,11 +38,6 @@ class ShippingSyncer
4038
*/
4139
private FBEHelper $fbeHelper;
4240

43-
/**
44-
* @var Filesystem
45-
*/
46-
private Filesystem $fileSystem;
47-
4841
/**
4942
* @var GraphAPIAdapter
5043
*/
@@ -68,7 +61,6 @@ class ShippingSyncer
6861
/**
6962
* Constructor for Shipping settings update plugin
7063
*
71-
* @param Filesystem $fileSystem
7264
* @param GraphAPIAdapter $graphApiAdapter
7365
* @param FBEHelper $fbeHelper
7466
* @param SystemConfig $systemConfig
@@ -77,15 +69,13 @@ class ShippingSyncer
7769
* @param ShippingData $shippingData
7870
*/
7971
public function __construct(
80-
FileSystem $fileSystem,
8172
GraphAPIAdapter $graphApiAdapter,
8273
FBEHelper $fbeHelper,
8374
SystemConfig $systemConfig,
8475
StoreManagerInterface $storeManager,
8576
ShippingFileBuilder $shippingFileBuilder,
8677
ShippingData $shippingData
8778
) {
88-
$this->fileSystem = $fileSystem;
8979
$this->graphApiAdapter = $graphApiAdapter;
9080
$this->fbeHelper = $fbeHelper;
9181
$this->systemConfig = $systemConfig;
@@ -97,46 +87,45 @@ public function __construct(
9787
/**
9888
* Syncing shipping profiles to Meta
9989
*
100-
* @param string $event_type
101-
* @param StoreInterface|null $store
90+
* @param string $eventType
91+
* @param int|null $storeId
10292
* @return void
10393
*/
104-
public function syncShippingProfiles(string $event_type, StoreInterface $store = null)
94+
public function syncShippingProfiles(string $eventType, $storeId = null)
10595
{
106-
if ($store !== null) {
107-
$this->syncShippingProfilesForStore($event_type, $store);
96+
if ($storeId !== null) {
97+
$this->syncShippingProfilesForStore($eventType, $storeId);
10898
return;
10999
}
110100
foreach ($this->storeManager->getStores() as $store) {
111-
$this->syncShippingProfilesForStore($event_type, $store);
101+
$this->syncShippingProfilesForStore($eventType, $store->getId());
112102
}
113103
}
114104

115105
/**
116106
* Syncing shipping profiles for an individual store
117107
*
118-
* @param string $event_type
119-
* @param StoreInterface $store
108+
* @param string $eventType
109+
* @param int $storeId
120110
* @return void
121111
*/
122-
private function syncShippingProfilesForStore(string $event_type, StoreInterface $store)
112+
private function syncShippingProfilesForStore(string $eventType, $storeId)
123113
{
114+
$accessToken = $this->systemConfig->getAccessToken($storeId);
115+
$partnerIntegrationId = $this->systemConfig->getCommercePartnerIntegrationId($storeId);
116+
if ($accessToken === null || $partnerIntegrationId === null) {
117+
return;
118+
}
119+
124120
try {
125-
$accessToken = $accessToken ?? $this->systemConfig->getAccessToken($store->getId());
126-
if ($accessToken === null) {
127-
return;
128-
}
129-
$this->shippingData->setStoreId((int)$store->getId());
121+
$this->shippingData->setStoreId($storeId);
130122
$shippingProfiles = [
131123
$this->shippingData->buildShippingProfile(ShippingProfileTypes::TABLE_RATE),
132124
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FLAT_RATE),
133125
$this->shippingData->buildShippingProfile(ShippingProfileTypes::FREE_SHIPPING),
134126
];
135127
$fileUri = $this->shippingFileBuilder->createFile($shippingProfiles);
136-
$accessToken = $accessToken ?? $this->systemConfig->getAccessToken($store->getId());
137-
$partnerIntegrationId = $partnerIntegrationId ??
138-
$this->systemConfig->getCommercePartnerIntegrationId($store->getId());
139-
$this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($store->getId()))
128+
$this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($storeId))
140129
->setAccessToken($accessToken);
141130
$this->graphApiAdapter->uploadFile(
142131
$partnerIntegrationId,
@@ -148,7 +137,7 @@ private function syncShippingProfilesForStore(string $event_type, StoreInterface
148137
$this->fbeHelper->logExceptionImmediatelyToMeta($e, [
149138
'store_id' => $this->fbeHelper->getStore()->getId(),
150139
'event' => 'shipping_profile_sync',
151-
'event_type' => $event_type
140+
'event_type' => $eventType
152141
]);
153142
}
154143
}

app/code/Meta/Sales/etc/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<event name="sales_order_payment_refund">
1313
<observer name="facebook_order_refund" instance="Meta\Sales\Observer\Order\Refund" />
1414
</event>
15+
<event name="facebook_fbe_onboarding_after">
16+
<observer name="facebook_sync_shipping_profiles" instance="Meta\Sales\Observer\Facebook\SyncShippingProfiles" />
17+
</event>
1518
<event name="facebook_order_create_after">
1619
<observer name="facebook_order_create_after" instance="Meta\Sales\Observer\Facebook\OrderCreateAfter" />
1720
</event>

0 commit comments

Comments
 (0)