Skip to content

Commit b0c7814

Browse files
authored
Merge pull request #9 from LS-Myron/main
Added fix for backoff method type and refactor of code
2 parents 8f2312e + b76dafa commit b0c7814

File tree

8 files changed

+60
-61
lines changed

8 files changed

+60
-61
lines changed

Controller/Adminhtml/Product/MassEnrich.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
use Magento\Backend\Model\View\Result\Redirect;
88
use Magento\Catalog\Api\ProductRepositoryInterface;
99
use Magento\Backend\App\Action;
10-
use Magento\Catalog\Controller\Adminhtml\Product\Builder;
1110
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1211
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
13-
use Magento\Framework\App\ObjectManager;
1412
use Magento\Framework\Controller\ResultFactory;
1513
use Magento\Framework\Exception\LocalizedException;
1614
use Magento\Ui\Component\MassAction\Filter;
@@ -21,12 +19,12 @@ class MassEnrich extends Action implements HttpPostActionInterface
2119
{
2220
protected $overwrite = true;
2321
public function __construct(
24-
Context $context,
25-
private Filter $filter,
26-
private CollectionFactory $collectionFactory,
27-
private Config $config,
28-
private Publisher $publisher,
29-
private ProductRepositoryInterface $productRepository,
22+
Context $context,
23+
private readonly Filter $filter,
24+
private readonly CollectionFactory $collectionFactory,
25+
private readonly Config $config,
26+
private readonly Publisher $publisher,
27+
private readonly ProductRepositoryInterface $productRepository,
3028
) {
3129
parent::__construct($context);
3230
}
@@ -37,7 +35,7 @@ public function __construct(
3735
* @return Redirect
3836
* @throws LocalizedException
3937
*/
40-
public function execute()
38+
public function execute(): Redirect
4139
{
4240
$collection = $this->filter->getCollection($this->collectionFactory->create());
4341

Model/Config.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
declare(strict_types=1);
33
namespace MageOS\CatalogDataAI\Model;
44

5-
use Magento\Store\Model\Store;
5+
use Magento\Framework\App\Config\ScopeConfigInterface;
66
USE Magento\Catalog\Model\Product;
77

88
class Config
@@ -18,83 +18,84 @@ class Config
1818
public const XML_PATH_OPENAI_API_ADVANCED_PRESENCE_PENALTY = 'catalog_ai/advanced/presence_penalty';
1919

2020
public function __construct(
21-
private \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
22-
) {}
21+
private readonly ScopeConfigInterface $scopeConfig
22+
) {
23+
}
2324

24-
public function isEnabled()
25+
public function isEnabled(): bool
2526
{
2627
return $this->scopeConfig->isSetFlag(
2728
self::XML_PATH_ENRICH_ENABLED
2829
);
2930
}
30-
public function IsAsync()
31+
public function IsAsync(): bool
3132
{
3233
return $this->scopeConfig->isSetFlag(
3334
self::XML_PATH_USE_ASYNC
3435
);
3536
}
3637

37-
public function getApiKey()
38+
public function getApiKey(): mixed
3839
{
3940
return $this->scopeConfig->getValue(
4041
self::XML_PATH_OPENAI_API_KEY
4142
);
4243
}
43-
public function getApiModel()
44+
public function getApiModel(): mixed
4445
{
4546
return $this->scopeConfig->getValue(
4647
self::XML_PATH_OPENAI_API_MODEL
4748
);
4849
}
49-
public function getApiMaxTokens()
50+
public function getApiMaxTokens(): int
5051
{
5152
return (int)$this->scopeConfig->getValue(
5253
self::XML_PATH_OPENAI_API_MAX_TOKENS
5354
);
5455
}
5556

56-
public function getProductPrompt(String $attributeCode)
57+
public function getProductPrompt(string $attributeCode): mixed
5758
{
5859
$path = 'catalog_ai/product/' . $attributeCode;
5960
return $this->scopeConfig->getValue(
6061
$path
6162
);
6263
}
63-
public function getProductPromptToken(String $attributeCode)
64+
public function getProductPromptToken(string $attributeCode): mixed
6465
{
6566
$path = 'catalog_ai/product/' . $attributeCode;
6667
return $this->scopeConfig->getValue(
6768
$path
6869
);
6970
}
7071

71-
public function canEnrich(Product $product)
72+
public function canEnrich(Product $product): bool
7273
{
7374
return $this->isEnabled() && $this->getApiKey() && $product->isObjectNew();
7475
}
7576

76-
public function getSystemPrompt()
77+
public function getSystemPrompt(): mixed
7778
{
7879
return $this->scopeConfig->getValue(
7980
self::XML_PATH_OPENAI_API_ADVANCED_SYSTEM_PROMPT
8081
);
8182
}
8283

83-
public function getTemperature()
84+
public function getTemperature(): float
8485
{
8586
return (float)$this->scopeConfig->getValue(
8687
self::XML_PATH_OPENAI_API_ADVANCED_TEMPERATURE
8788
);
8889
}
8990

90-
public function getFrequencyPenalty()
91+
public function getFrequencyPenalty(): float
9192
{
9293
return (float)$this->scopeConfig->getValue(
9394
self::XML_PATH_OPENAI_API_ADVANCED_FREQUENCY_PENALTY
9495
);
9596
}
9697

97-
public function getPresencePenalty()
98+
public function getPresencePenalty(): float
9899
{
99100
return (float)$this->scopeConfig->getValue(
100101
self::XML_PATH_OPENAI_API_ADVANCED_PRESENCE_PENALTY

Model/Product/Consumer.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
use Magento\Catalog\Model\ProductRepository;
77
use Magento\Store\Model\StoreManagerInterface;
8-
use MageOS\CatalogDataAI\Model\Product\Request;
9-
use MageOS\CatalogDataAI\Model\Product\Enricher;
108

119
/**
1210
* Class Consumer
@@ -18,12 +16,12 @@ class Consumer
1816
* Consumer constructor.
1917
*/
2018
public function __construct(
21-
private Enricher $enricher,
22-
private ProductRepository $productRepository,
23-
private StoreManagerInterface $storeManager
19+
private readonly Enricher $enricher,
20+
private readonly ProductRepository $productRepository,
21+
private readonly StoreManagerInterface $storeManager
2422
) {}
2523

26-
public function execute(Request $request)
24+
public function execute(Request $request): void
2725
{
2826
// @TODO: enrich for all stores if different value or language
2927
$this->storeManager->setCurrentStore(0);

Model/Product/Enricher.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
use Magento\Catalog\Model\Product;
88
use OpenAI\Factory;
99
use OpenAI\Client;
10-
use OpenAI\Responses\Meta;
10+
use OpenAI\Responses\Meta\MetaInformation;
1111
use OpenAI\Exceptions\ErrorException;
1212

1313
class Enricher
1414
{
1515
private Client $client;
1616
public function __construct
1717
(
18-
private Factory $clientFactory,
19-
private Config $config
18+
private readonly Factory $clientFactory,
19+
private readonly Config $config
2020
) {
21-
$this->client = $this->clientFactory->withApiKey($this->config->getApiKey())
21+
$this->client = $this->clientFactory
22+
->withApiKey($this->config->getApiKey())
2223
->make();
2324
}
2425

25-
public function getAttributes()
26+
public function getAttributes(): array
2627
{
2728
return [
2829
'short_description',
@@ -36,15 +37,14 @@ public function getAttributes()
3637
/**
3738
* @todo move to parser class/pool
3839
*/
39-
public function parsePrompt($prompt, $product): String
40+
public function parsePrompt($prompt, $product): string
4041
{
41-
$prompt = preg_replace_callback('/\{\{(.+?)\}\}/', function ($matches) use ($product) {
42+
return preg_replace_callback('/\{\{(.+?)\}\}/', function ($matches) use ($product) {
4243
return $product->getData($matches[1]);
4344
}, $prompt);
44-
return $prompt;
4545
}
4646

47-
public function enrichAttribute($product, $attributeCode)
47+
public function enrichAttribute($product, $attributeCode): void
4848
{
4949
if(!$product->getData('mageos_catalogai_overwrite') && $product->getData($attributeCode)){
5050
return;
@@ -79,7 +79,7 @@ public function enrichAttribute($product, $attributeCode)
7979
}
8080
}
8181

82-
public function backoff(Meta $meta)
82+
public function backoff(MetaInformation $meta): void
8383
{
8484
if($meta->requestLimit->remaining < 1) {
8585
sleep($this->strToSeconds($meta->requestLimit->reset));
@@ -91,7 +91,7 @@ public function backoff(Meta $meta)
9191
}
9292
}
9393

94-
private function strToSeconds(string $time)
94+
private function strToSeconds(string $time): float|int
9595
{
9696
preg_match('/(?:([0-9]+)h)?(?:([0-9]+)m)?(?:([0-9]+)s)?/', $time, $matches);
9797

@@ -102,7 +102,7 @@ private function strToSeconds(string $time)
102102
return $hours * 3600 + $minutes * 60 + $seconds;
103103
}
104104

105-
public function execute(Product $product)
105+
public function execute(Product $product): void
106106
{
107107
foreach ($this->getAttributes() as $attributeCode) {
108108
try {

Model/Product/Publisher.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
declare(strict_types=1);
33

44
namespace MageOS\CatalogDataAI\Model\Product;
5+
56
use Magento\Framework\MessageQueue\PublisherInterface;
67
use MageOS\CatalogDataAI\Model\Product\RequestFactory;
78

@@ -11,18 +12,18 @@ class Publisher
1112

1213
/**
1314
* Publisher constructor.
14-
* @param Publisher $publisher
1515
*/
16-
public function __construct
17-
(
18-
private PublisherInterface $publisher,
19-
private RequestFactory $requestFactory,
20-
) {}
16+
public function __construct(
17+
private readonly PublisherInterface $publisher,
18+
private readonly RequestFactory $requestFactory,
19+
) {
20+
}
2121

2222
/**
23-
* @param data
23+
* @param int|string $productId
24+
* @param bool $overwrite
2425
*/
25-
public function execute(int|string $productId, $overwrite = false)
26+
public function execute(int|string $productId, bool $overwrite = false): void
2627
{
2728
$request = $this->requestFactory->create([
2829
'id' => (int)$productId,

Model/Product/Request.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
class Request
1010
{
1111
public function __construct(
12-
private int $id,
13-
private bool $overwrite
14-
) {}
12+
private readonly int $id,
13+
private readonly bool $overwrite
14+
) {
15+
}
1516

1617
/**
1718
* Retrieve products id.
18-
* @return int
1919
*/
2020
public function getId(): int
2121
{
@@ -24,7 +24,6 @@ public function getId(): int
2424

2525
/**
2626
* Retrieve overwrite flag.
27-
* @return bool
2827
*/
2928
public function getOverwrite(): bool
3029
{

Observer/Product/SaveAfter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MageOS\CatalogDataAI\Observer\Product;
55

6+
use Magento\Catalog\Model\Product;
67
use Magento\Framework\Event\ObserverInterface;
78
use Magento\Framework\Event\Observer;
89
use MageOS\CatalogDataAI\Model\Config;
@@ -11,13 +12,13 @@
1112
class SaveAfter implements ObserverInterface
1213
{
1314
public function __construct(
14-
private Config $config,
15-
private Publisher $publisher
15+
private readonly Config $config,
16+
private readonly Publisher $publisher
1617
) {}
1718

1819
public function execute(Observer $observer): void
1920
{
20-
/** @var \Magento\Catalog\Model\Product $product */
21+
/** @var Product $product */
2122
$product = $observer->getProduct();
2223

2324
if($this->config->canEnrich($product) && $this->config->isAsync()) {

Observer/Product/SaveBefore.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace MageOS\CatalogDataAI\Observer\Product;
55

6+
use Magento\Catalog\Model\Product;
67
use Magento\Framework\Event\ObserverInterface;
78
use Magento\Framework\Event\Observer;
89
use MageOS\CatalogDataAI\Model\Config;
@@ -11,13 +12,13 @@
1112
class SaveBefore implements ObserverInterface
1213
{
1314
public function __construct(
14-
private Config $config,
15-
private Enricher $enricher
15+
private readonly Config $config,
16+
private readonly Enricher $enricher
1617
) {}
1718

1819
public function execute(Observer $observer): void
1920
{
20-
/** @var \Magento\Catalog\Model\Product $product */
21+
/** @var Product $product */
2122
$product = $observer->getProduct();
2223

2324
if($this->config->canEnrich($product) && !$this->config->isAsync()) {

0 commit comments

Comments
 (0)