Skip to content

Commit 3200770

Browse files
authored
Merge pull request #54 from Prokyonn/enhancement/async-producer
Add TriggerProducer messages to make them optionally async
2 parents ec7d20b + ebd7d92 commit 3200770

10 files changed

+238
-13
lines changed

src/Command/SynchronizeProductVariantsCommand.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@
1414
namespace Sulu\SyliusProducerPlugin\Command;
1515

1616
use Doctrine\ORM\EntityManagerInterface;
17-
use Sulu\SyliusProducerPlugin\Producer\ProductVariantMessageProducerInterface;
17+
use Sulu\SyliusProducerPlugin\Message\TriggerProductVariantProducerMessage;
1818
use Sylius\Component\Core\Model\ProductVariantInterface;
1919
use Sylius\Component\Product\Repository\ProductVariantRepositoryInterface;
2020
use Symfony\Component\Console\Helper\ProgressBar;
2121
use Symfony\Component\Console\Input\InputInterface;
2222
use Symfony\Component\Console\Output\OutputInterface;
23+
use Symfony\Component\Messenger\MessageBusInterface;
2324

2425
class SynchronizeProductVariantsCommand extends BaseSynchronizeCommand
2526
{
2627
/** @param ProductVariantRepositoryInterface<ProductVariantInterface> $productVariantRepository */
2728
public function __construct(
2829
private EntityManagerInterface $entityManager,
29-
private ProductVariantMessageProducerInterface $productVariantMessageProducer,
3030
private ProductVariantRepositoryInterface $productVariantRepository,
31+
private MessageBusInterface $messageBus,
3132
) {
3233
parent::__construct($entityManager);
3334
}
@@ -73,7 +74,7 @@ private function syncProductVariants(OutputInterface $output): void
7374
continue;
7475
}
7576

76-
$this->productVariantMessageProducer->synchronize($productVariant);
77+
$this->messageBus->dispatch(new TriggerProductVariantProducerMessage($productVariant->getId()));
7778

7879
$this->entityManager->detach($productVariant);
7980
++$processedItems;

src/Command/SynchronizeProductsCommand.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@
1414
namespace Sulu\SyliusProducerPlugin\Command;
1515

1616
use Doctrine\ORM\EntityManagerInterface;
17-
use Sulu\SyliusProducerPlugin\Producer\ProductMessageProducerInterface;
17+
use Sulu\SyliusProducerPlugin\Message\TriggerProductProducerMessage;
1818
use Sylius\Component\Core\Model\ProductInterface;
1919
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
2020
use Symfony\Component\Console\Helper\ProgressBar;
2121
use Symfony\Component\Console\Input\InputInterface;
2222
use Symfony\Component\Console\Output\OutputInterface;
23+
use Symfony\Component\Messenger\MessageBusInterface;
2324

2425
class SynchronizeProductsCommand extends BaseSynchronizeCommand
2526
{
2627
/** @param ProductRepositoryInterface<ProductInterface> $productRepository */
2728
public function __construct(
2829
private EntityManagerInterface $entityManager,
29-
private ProductMessageProducerInterface $productMessageProducer,
3030
private ProductRepositoryInterface $productRepository,
31+
private MessageBusInterface $messageBus,
3132
) {
3233
parent::__construct($entityManager);
3334
}
@@ -72,8 +73,7 @@ private function syncProducts(OutputInterface $output): void
7273
if (!$product instanceof ProductInterface) {
7374
continue;
7475
}
75-
76-
$this->productMessageProducer->synchronize($product);
76+
$this->messageBus->dispatch(new TriggerProductProducerMessage($product->getId()));
7777

7878
$this->entityManager->detach($product);
7979
++$processedItems;

src/Command/SynchronizeTaxonCommand.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414
namespace Sulu\SyliusProducerPlugin\Command;
1515

1616
use Doctrine\ORM\EntityManagerInterface;
17-
use Sulu\SyliusProducerPlugin\Producer\TaxonMessageProducerInterface;
17+
use Sulu\SyliusProducerPlugin\Message\TriggerTaxonProducerMessage;
1818
use Sylius\Component\Taxonomy\Model\TaxonInterface;
1919
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
2020
use Symfony\Component\Console\Input\InputInterface;
2121
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\Messenger\MessageBusInterface;
2223

2324
class SynchronizeTaxonCommand extends BaseSynchronizeCommand
2425
{
2526
/** @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository */
2627
public function __construct(
2728
private EntityManagerInterface $entityManager,
28-
private TaxonMessageProducerInterface $taxonMessageProducer,
2929
private TaxonRepositoryInterface $taxonRepository,
30+
private MessageBusInterface $messageBus,
3031
) {
3132
parent::__construct($entityManager);
3233
}
@@ -62,7 +63,11 @@ private function syncTaxonTree(OutputInterface $output): void
6263
$taxons = \array_merge($taxons, $this->extractChildrenFlat($rootTaxon));
6364
}
6465

65-
$this->taxonMessageProducer->synchronize($taxons);
66+
$taxonIds = \array_map(
67+
fn (TaxonInterface $taxon) => $taxon->getId(),
68+
$taxons,
69+
);
70+
$this->messageBus->dispatch(new TriggerTaxonProducerMessage($taxonIds));
6671
}
6772

6873
private function extractChildrenFlat(TaxonInterface $rootTaxon): array
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Sulu.
5+
*
6+
* (c) Sulu GmbH
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Sulu\SyliusProducerPlugin\Message;
13+
14+
class TriggerProductProducerMessage
15+
{
16+
public function __construct(
17+
public int $productId,
18+
) {
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Sulu.
5+
*
6+
* (c) Sulu GmbH
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Sulu\SyliusProducerPlugin\Message;
13+
14+
class TriggerProductVariantProducerMessage
15+
{
16+
public function __construct(
17+
public int $productVariantId,
18+
) {
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Sulu.
5+
*
6+
* (c) Sulu GmbH
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Sulu\SyliusProducerPlugin\Message;
13+
14+
class TriggerTaxonProducerMessage
15+
{
16+
/**
17+
* @param int[] $taxonIds
18+
*/
19+
public function __construct(
20+
public array $taxonIds,
21+
) {
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Sulu.
5+
*
6+
* (c) Sulu GmbH
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Sulu\SyliusProducerPlugin\MessageHandler;
13+
14+
use Sulu\SyliusProducerPlugin\Message\TriggerProductProducerMessage;
15+
use Sulu\SyliusProducerPlugin\Producer\ProductMessageProducerInterface;
16+
use Sylius\Component\Core\Model\ProductInterface;
17+
use Sylius\Component\Product\Repository\ProductRepositoryInterface;
18+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
19+
20+
#[AsMessageHandler]
21+
class TriggerProductProducerMessageHandler
22+
{
23+
/**
24+
* @param ProductRepositoryInterface<ProductInterface> $productRepository
25+
*/
26+
public function __construct(
27+
private ProductRepositoryInterface $productRepository,
28+
private ProductMessageProducerInterface $producer,
29+
) {
30+
}
31+
32+
public function __invoke(TriggerProductProducerMessage $message): void
33+
{
34+
/** @var ProductInterface|null $product */
35+
$product = $this->productRepository->find($message->productId);
36+
if (null === $product) {
37+
return;
38+
}
39+
40+
$this->producer->synchronize($product);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Sulu.
5+
*
6+
* (c) Sulu GmbH
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Sulu\SyliusProducerPlugin\MessageHandler;
13+
14+
use Sulu\SyliusProducerPlugin\Message\TriggerProductVariantProducerMessage;
15+
use Sulu\SyliusProducerPlugin\Producer\ProductVariantMessageProducerInterface;
16+
use Sylius\Component\Core\Model\ProductVariantInterface;
17+
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
18+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
19+
20+
#[AsMessageHandler]
21+
class TriggerProductVariantProducerMessageHandler
22+
{
23+
/**
24+
* @param ProductVariantRepositoryInterface<ProductVariantInterface> $productVariantRepository
25+
*/
26+
public function __construct(
27+
private ProductVariantRepositoryInterface $productVariantRepository,
28+
private ProductVariantMessageProducerInterface $producer,
29+
) {
30+
}
31+
32+
public function __invoke(TriggerProductVariantProducerMessage $message): void
33+
{
34+
/** @var ProductVariantInterface|null $product */
35+
$product = $this->productVariantRepository->find($message->productVariantId);
36+
if (!$product) {
37+
return;
38+
}
39+
40+
$this->producer->synchronize($product);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Sulu.
5+
*
6+
* (c) Sulu GmbH
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Sulu\SyliusProducerPlugin\MessageHandler;
13+
14+
use Sulu\SyliusProducerPlugin\Message\TriggerTaxonProducerMessage;
15+
use Sulu\SyliusProducerPlugin\Producer\TaxonMessageProducerInterface;
16+
use Sylius\Component\Core\Model\TaxonInterface;
17+
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
18+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
19+
20+
#[AsMessageHandler]
21+
class TriggerTaxonProducerMessageHandler
22+
{
23+
/**
24+
* @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository
25+
*/
26+
public function __construct(
27+
private TaxonRepositoryInterface $taxonRepository,
28+
private TaxonMessageProducerInterface $producer,
29+
) {
30+
}
31+
32+
public function __invoke(TriggerTaxonProducerMessage $message): void
33+
{
34+
/** @var TaxonInterface[] $taxon */
35+
$taxon = $this->taxonRepository->findBy(['id' => $message->taxonIds]);
36+
if (0 === \count($taxon)) {
37+
return;
38+
}
39+
40+
$this->producer->synchronize($taxon);
41+
}
42+
}

src/Resources/config/services.xml

+33-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@
3030
<argument type="service" id="sulu_sylius_producer.messenger_bus"/>
3131
</service>
3232

33+
<service
34+
id="Sulu\SyliusProducerPlugin\MessageHandler\TriggerProductProducerMessageHandler"
35+
class="Sulu\SyliusProducerPlugin\MessageHandler\TriggerProductProducerMessageHandler"
36+
>
37+
<argument type="service" id="sylius.repository.product"/>
38+
<argument type="service" id="Sulu\SyliusProducerPlugin\Producer\ProductMessageProducerInterface"/>
39+
40+
<tag name="messenger.message_handler"/>
41+
</service>
42+
43+
<service
44+
id="Sulu\SyliusProducerPlugin\MessageHandler\TriggerProductVariantProducerMessageHandler"
45+
class="Sulu\SyliusProducerPlugin\MessageHandler\TriggerProductVariantProducerMessageHandler"
46+
>
47+
<argument type="service" id="sylius.repository.product_variant"/>
48+
<argument type="service" id="Sulu\SyliusProducerPlugin\Producer\ProductVariantMessageProducerInterface"/>
49+
50+
<tag name="messenger.message_handler"/>
51+
</service>
52+
53+
<service
54+
id="Sulu\SyliusProducerPlugin\MessageHandler\TriggerTaxonProducerMessageHandler"
55+
class="Sulu\SyliusProducerPlugin\MessageHandler\TriggerTaxonProducerMessageHandler"
56+
>
57+
<argument type="service" id="sylius.repository.taxon"/>
58+
<argument type="service" id="Sulu\SyliusProducerPlugin\Producer\TaxonMessageProducerInterface"/>
59+
60+
<tag name="messenger.message_handler"/>
61+
</service>
62+
3363
<service id="Sulu\SyliusProducerPlugin\EventSubscriber\ProductEventSubscriber">
3464
<argument type="service" id="Sulu\SyliusProducerPlugin\Producer\ProductMessageProducerInterface"/>
3565

@@ -72,24 +102,24 @@
72102

73103
<service id="Sulu\SyliusProducerPlugin\Command\SynchronizeTaxonCommand">
74104
<argument type="service" id="doctrine.orm.entity_manager"/>
75-
<argument type="service" id="Sulu\SyliusProducerPlugin\Producer\TaxonMessageProducerInterface"/>
76105
<argument type="service" id="sylius.repository.taxon"/>
106+
<argument type="service" id="sulu_sylius_producer.messenger_bus"/>
77107

78108
<tag name="console.command" />
79109
</service>
80110

81111
<service id="Sulu\SyliusProducerPlugin\Command\SynchronizeProductsCommand">
82112
<argument type="service" id="doctrine.orm.entity_manager"/>
83-
<argument type="service" id="Sulu\SyliusProducerPlugin\Producer\ProductMessageProducerInterface"/>
84113
<argument type="service" id="sylius.repository.product"/>
114+
<argument type="service" id="sulu_sylius_producer.messenger_bus"/>
85115

86116
<tag name="console.command" />
87117
</service>
88118

89119
<service id="Sulu\SyliusProducerPlugin\Command\SynchronizeProductVariantsCommand">
90120
<argument type="service" id="doctrine.orm.entity_manager"/>
91-
<argument type="service" id="Sulu\SyliusProducerPlugin\Producer\ProductVariantMessageProducerInterface"/>
92121
<argument type="service" id="sylius.repository.product_variant"/>
122+
<argument type="service" id="sulu_sylius_producer.messenger_bus"/>
93123

94124
<tag name="console.command" />
95125
</service>

0 commit comments

Comments
 (0)