Skip to content

Commit e35d261

Browse files
sachinkumarRPSachin Kumarbgiamarino
authored
Resolve type error and refector code (#278)
* refactor the code for getFeedId method * Resolve TypeError for Push Products Button * Address Static Test --------- Co-authored-by: Sachin Kumar <[email protected]> Co-authored-by: Ben Giamarino <[email protected]>
1 parent dbbd782 commit e35d261

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

app/code/Meta/Catalog/Model/Product/Feed/Builder/MultiSourceInventory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,8 @@ private function getStockQty(Product $product, int $stockId)
127127
*/
128128
public function initInventoryForProduct(Product $product): MultiSourceInventory
129129
{
130-
$websiteId = $product->getStore()->getWebsiteId();
130+
$websiteId = (int) $product->getStore()->getWebsiteId();
131131
$stockId = $this->stockByWebsiteIdResolver->execute($websiteId)->getStockId();
132-
133132
$this->product = $product;
134133
$this->stockStatus = $this->isInStock($product, $stockId);
135134
$this->stockQty = $this->getStockQty($product, $stockId);
@@ -157,7 +156,7 @@ public function getInventory(): int
157156
return 0;
158157
}
159158
$outOfStockThreshold = $this->systemConfig->getOutOfStockThreshold($this->product->getStoreId());
160-
$quantityAvailableForCatalog = $this->stockQty - $outOfStockThreshold;
159+
$quantityAvailableForCatalog = (int) $this->stockQty - $outOfStockThreshold;
161160
return $quantityAvailableForCatalog > 0 ? $quantityAvailableForCatalog : 0;
162161
}
163162
}

app/code/Meta/Catalog/Model/Product/Feed/Method/FeedApi.php

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -105,38 +105,31 @@ public function __construct(
105105
}
106106

107107
/**
108-
* Get feed id
108+
* Get FB Feed ID
109109
*
110-
* @param int $feedId
111-
* @param array $catalogFeeds
112-
* @param string $feedName
113-
* @param int $catalogId
114-
* @return void
110+
* @return mixed|null
115111
* @throws GuzzleException
116112
*/
117-
118-
private function getFeedId($feedId, $catalogFeeds, $feedName, $catalogId)
113+
private function getFbFeedId()
119114
{
115+
$feedId = $this->systemConfig->getFeedId($this->storeId);
116+
$feedName = self::FB_FEED_NAME;
117+
$catalogId = $this->systemConfig->getCatalogId($this->storeId);
118+
$catalogFeeds = $this->graphApiAdapter->getCatalogFeeds($catalogId);
119+
120+
// make sure feed exists on meta side, not deleted
120121
if ($feedId) {
121-
$magentoFeeds = array_filter($catalogFeeds, function ($a) use ($feedId) {
122-
return $a['id'] === $feedId;
123-
});
122+
$magentoFeeds = array_filter($catalogFeeds, fn($a) => $a['id'] === $feedId);
123+
// in case feed id is not found in meta catalog, feed id on
124+
// magento will be flushed and new feed will be created in Meta Catalog
124125
if (empty($magentoFeeds)) {
125126
$feedId = null;
126127
}
127128
}
128129

129-
if (!$feedId) {
130-
$magentoFeeds = array_filter($catalogFeeds, function ($a) use ($feedName) {
131-
return $a['name'] === $feedName;
132-
});
133-
if (!empty($magentoFeeds)) {
134-
$feedId = $magentoFeeds[array_key_first($magentoFeeds)]['id'];
135-
}
136-
}
137-
138130
if (!$feedId) {
139131
$feedId = $this->graphApiAdapter->createEmptyFeed($catalogId, $feedName);
132+
140133
$maxAttempts = 5;
141134
$attempts = 0;
142135
do {
@@ -147,26 +140,14 @@ private function getFeedId($feedId, $catalogFeeds, $feedName, $catalogId)
147140
$attempts++;
148141
usleep(2000000);
149142
} while ($attempts < $maxAttempts);
150-
}
151-
}
152143

153-
/**
154-
* Get FB Feed Id
155-
*
156-
* @return mixed|null
157-
* @throws GuzzleException
158-
*/
159-
private function getFbFeedId()
160-
{
161-
$feedId = $this->systemConfig->getFeedId($this->storeId);
162-
$feedName = self::FB_FEED_NAME;
163-
$catalogId = $this->systemConfig->getCatalogId($this->storeId);
164-
$catalogFeeds = $this->graphApiAdapter->getCatalogFeeds($catalogId);
165-
$this->getFeedId($feedId, $catalogFeeds, $feedName, $catalogId);
166-
if ($feedId && $this->systemConfig->getFeedId($this->storeId) != $feedId) {
167-
$this->systemConfig
168-
->saveConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_FEED_ID, $feedId, $this->storeId)
169-
->cleanCache();
144+
if ($feedId) {
145+
$this->systemConfig->saveConfig(
146+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_FEED_ID,
147+
$feedId,
148+
$this->storeId
149+
)->cleanCache();
150+
}
170151
}
171152
return $feedId;
172153
}
@@ -256,7 +237,6 @@ public function execute($storeId = null)
256237
$this->builder->setStoreId($this->storeId);
257238
$this->graphApiAdapter->setDebugMode($this->systemConfig->isDebugMode($storeId))
258239
->setAccessToken($this->systemConfig->getAccessToken($storeId));
259-
260240
try {
261241
$feedId = $this->getFbFeedId();
262242
if (!$feedId) {

0 commit comments

Comments
 (0)