Skip to content

Commit f83ffc3

Browse files
committed
added async enrichment process
1 parent c65470d commit f83ffc3

File tree

11 files changed

+164
-33
lines changed

11 files changed

+164
-33
lines changed

Model/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MageOS\CatalogDataAI\Model;
44

55
use Magento\Store\Model\Store;
6+
USE Magento\Catalog\Model\Product;
67

78
class Config
89
{
@@ -62,4 +63,9 @@ public function getProductPromptToken(String $attributeCode)
6263
$path
6364
);
6465
}
66+
67+
public function canEnrich(Product $product)
68+
{
69+
return $this->isEnabled() && $this->getApiKey() && $product->isObjectNew();
70+
}
6571
}

Model/Product/Consumer.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\CatalogDataAI\Model\Product;
5+
6+
use Magento\Catalog\Model\ProductRepository;
7+
use Magento\Store\Model\StoreManagerInterface;
8+
use MageOS\CatalogDataAI\Model\Product\Enricher;
9+
10+
/**
11+
* Class Consumer
12+
* @package Gaiterjones\RabbitMQ\MessageQueues\Product
13+
*/
14+
class Consumer
15+
{
16+
/**
17+
* Consumer constructor.
18+
*/
19+
public function __construct(
20+
private Enricher $enricher,
21+
private ProductRepository $productRepository,
22+
private StoreManagerInterface $storeManager
23+
) {}
24+
25+
public function execute($id)
26+
{
27+
// @TODO: enrich for all stores if different value or language
28+
$this->storeManager->setCurrentStore(0);
29+
$product = $this->productRepository->getById($id);
30+
$this->enricher->execute($product);
31+
$this->productRepository->save($product);
32+
}
33+
34+
}
Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace MageOS\CatalogDataAI\Observer;
4+
namespace MageOS\CatalogDataAI\Model\Product;
55

6-
use Magento\Framework\Event\ObserverInterface;
7-
use Magento\Framework\Event\Observer;
86
use MageOS\CatalogDataAI\Model\Config;
7+
use Magento\Catalog\Model\Product;
98
use OpenAI\Factory;
109
use OpenAI\Client;
11-
use Psr\Log\LoggerInterface;
1210

13-
class Product implements ObserverInterface
11+
class Enricher
1412
{
1513
private Client $client;
16-
public function __construct(
14+
public function __construct
15+
(
1716
private Factory $clientFactory,
18-
private Config $config,
19-
private LoggerInterface $logger
17+
private Config $config
2018
) {
2119
$this->client = $this->clientFactory->withApiKey($this->config->getApiKey())
2220
->make();
@@ -30,6 +28,18 @@ public function getAttributes()
3028
];
3129
}
3230

31+
/**
32+
* @todo move to parser class/pool
33+
*/
34+
public function parsePrompt($prompt, $product): String
35+
{
36+
return str_replace(
37+
['{{name}}', '{{sku}}'],
38+
[$product->getName(), $product->getSku()],
39+
$prompt
40+
);
41+
}
42+
3343
public function enrichAttribute($product, $attributeCode)
3444
{
3545
if($product->getData($attributeCode)) {
@@ -53,33 +63,11 @@ public function enrichAttribute($product, $attributeCode)
5363
}
5464
}
5565

56-
/**
57-
* @todo move to parser class/pool
58-
*/
59-
public function parsePrompt($prompt, $product): String
60-
{
61-
return str_replace(
62-
['{{name}}', '{{sku}}'],
63-
[$product->getName(), $product->getSku()],
64-
$prompt
65-
);
66-
}
67-
68-
public function execute(Observer $observer): void
66+
public function execute(Product $product)
6967
{
70-
if(!$this->config->isEnabled() || !$this->config->getApiKey()) {
71-
return;
72-
}
73-
/** @var \Magento\Catalog\Model\Product $product */
74-
$product = $observer->getProduct();
75-
76-
// only enrich new products
77-
// if(!$product->isObjectNew()) {
78-
// return;
79-
// }
80-
8168
foreach ($this->getAttributes() as $attributeCode) {
8269
$this->enrichAttribute($product, $attributeCode);
8370
}
71+
//@TODO: throw exception?
8472
}
8573
}

Model/Product/Publisher.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\CatalogDataAI\Model\Product;
5+
use Magento\Framework\MessageQueue\PublisherInterface;
6+
7+
class Publisher
8+
{
9+
const TOPIC_NAME = 'mageos.product.enrich';
10+
11+
/**
12+
* Publisher constructor.
13+
* @param Publisher $publisher
14+
*/
15+
public function __construct
16+
(
17+
private PublisherInterface $publisher
18+
) {}
19+
20+
/**
21+
* @param data
22+
*/
23+
public function execute(int|string $productId)
24+
{
25+
$this->publisher->publish(self::TOPIC_NAME, (int)$productId);
26+
}
27+
}

Observer/Product/SaveAfter.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\CatalogDataAI\Observer\Product;
5+
6+
use Magento\Framework\Event\ObserverInterface;
7+
use Magento\Framework\Event\Observer;
8+
use MageOS\CatalogDataAI\Model\Config;
9+
use MageOS\CatalogDataAI\Model\Product\Publisher;
10+
11+
class SaveAfter implements ObserverInterface
12+
{
13+
public function __construct(
14+
private Config $config,
15+
private Publisher $publisher
16+
) {}
17+
18+
public function execute(Observer $observer): void
19+
{
20+
/** @var \Magento\Catalog\Model\Product $product */
21+
$product = $observer->getProduct();
22+
23+
if($this->config->canEnrich($product) && $this->config->isAsync()) {
24+
$this->publisher->execute($product->getId());
25+
}
26+
}
27+
}

Observer/Product/SaveBefore.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MageOS\CatalogDataAI\Observer\Product;
5+
6+
use Magento\Framework\Event\ObserverInterface;
7+
use Magento\Framework\Event\Observer;
8+
use MageOS\CatalogDataAI\Model\Config;
9+
use MageOS\CatalogDataAI\Model\Product\Enricher;
10+
11+
class SaveBefore implements ObserverInterface
12+
{
13+
public function __construct(
14+
private Config $config,
15+
private Enricher $enricher
16+
) {}
17+
18+
public function execute(Observer $observer): void
19+
{
20+
/** @var \Magento\Catalog\Model\Product $product */
21+
$product = $observer->getProduct();
22+
23+
if($this->config->canEnrich($product) && !$this->config->isAsync()) {
24+
$this->enricher->execute($product);
25+
}
26+
27+
}
28+
}

etc/communication.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Communication/etc/communication.xsd">
3+
<topic name="mageos.product.enrich" request="int"/>
4+
</config>

etc/events.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
33
<event name="catalog_product_save_before">
4-
<observer name="mageos_catalogdataai_enrich_product" instance="MageOS\CatalogDataAI\Observer\Product" />
4+
<observer name="mageos_catalogdataai_enrich_product_realtime" instance="MageOS\CatalogDataAI\Observer\Product\SaveBefore" />
5+
</event>
6+
<event name="catalog_product_save_commit_after">
7+
<observer name="mageos_catalogdataai_enrich_product_async" instance="MageOS\CatalogDataAI\Observer\Product\SaveAfter" />
58
</event>
69
</config>

etc/queue_consumer.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/queue_consumer.xsd">
3+
<consumer name="mageosEnrichProductProcessor" queue="mageos.product.enrich.queue" handler="MageOS\CatalogDataAI\Model\Product\Consumer::execute"/>
4+
</config>

etc/queue_publisher.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
3+
<publisher topic="mageos.product.enrich"/>
4+
</config>

0 commit comments

Comments
 (0)