-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update documents after any product update on it
- Loading branch information
Showing
17 changed files
with
395 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Webgriffe\SyliusElasticsearchPlugin\EventSubscriber\ProductEventSubscriber; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator) { | ||
$services = $containerConfigurator->services(); | ||
|
||
$services->set('webgriffe.sylius_elasticsearch_plugin.event_subscriber.product', ProductEventSubscriber::class) | ||
->args([ | ||
service('webgriffe_sylius_elasticsearch_plugin.command_bus'), | ||
service('logger'), | ||
service('sylius.repository.channel'), | ||
]) | ||
->tag('kernel.event_subscriber') | ||
; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\Client\Enum; | ||
|
||
enum Action: string | ||
{ | ||
case CREATE = 'create'; | ||
case DELETE = 'delete'; | ||
case INDEX = 'index'; | ||
case UPDATE = 'update'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\Client\ValueObject; | ||
|
||
use Webgriffe\SyliusElasticsearchPlugin\Client\Enum\Action; | ||
|
||
final readonly class BulkAction | ||
{ | ||
public function __construct( | ||
private Action $action, | ||
private string $index, | ||
private array $payload, | ||
private ?string $type = null, | ||
private null|string|int $id = null, | ||
) { | ||
} | ||
|
||
public function getAction(): Action | ||
{ | ||
return $this->action; | ||
} | ||
|
||
public function getIndex(): string | ||
{ | ||
return $this->index; | ||
} | ||
|
||
public function getType(): ?string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function getPayload(): array | ||
{ | ||
return $this->payload; | ||
} | ||
|
||
public function getId(): int|string|null | ||
{ | ||
return $this->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\EventSubscriber; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Throwable; | ||
use Webgriffe\SyliusElasticsearchPlugin\DocumentType\ProductDocumentType; | ||
use Webgriffe\SyliusElasticsearchPlugin\Message\RemoveDocumentIfExists; | ||
use Webgriffe\SyliusElasticsearchPlugin\Message\UpsertDocument; | ||
|
||
final readonly class ProductEventSubscriber implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private MessageBusInterface $messageBus, | ||
private LoggerInterface $logger, | ||
private ChannelRepositoryInterface $channelRepository, | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'sylius.product.post_create' => 'onProductPostCreateOrUpdate', | ||
'sylius.product.post_update' => 'onProductPostCreateOrUpdate', | ||
'sylius.product.post_delete' => 'onProductPostDelete', | ||
]; | ||
} | ||
|
||
public function onProductPostCreateOrUpdate(GenericEvent $event): void | ||
{ | ||
$product = $event->getSubject(); | ||
if (!$product instanceof ProductInterface) { | ||
return; | ||
} | ||
$productId = $product->getId(); | ||
if (!is_int($productId) && !is_string($productId)) { | ||
return; | ||
} | ||
|
||
try { | ||
/** @var array<array-key, ChannelInterface> $allChannels */ | ||
$allChannels = $this->channelRepository->findAll(); | ||
$productChannels = $product->getChannels(); | ||
foreach ($allChannels as $channel) { | ||
$channelId = $channel->getId(); | ||
if (!is_int($channelId) && !is_string($channelId)) { | ||
continue; | ||
} | ||
if ($productChannels->contains($channel)) { | ||
$this->messageBus->dispatch(new UpsertDocument( | ||
$channelId, | ||
ProductDocumentType::CODE, | ||
$productId, | ||
)); | ||
|
||
continue; | ||
} | ||
$this->messageBus->dispatch(new RemoveDocumentIfExists( | ||
$channelId, | ||
ProductDocumentType::CODE, | ||
$productId, | ||
)); | ||
} | ||
} catch (Throwable $throwable) { | ||
$this->logger->error($throwable->getMessage(), $throwable->getTrace()); | ||
} | ||
} | ||
|
||
public function onProductPostDelete(GenericEvent $event): void | ||
{ | ||
$product = $event->getSubject(); | ||
if (!$product instanceof ProductInterface) { | ||
return; | ||
} | ||
$productId = $product->getId(); | ||
if (!is_int($productId) && !is_string($productId)) { | ||
return; | ||
} | ||
|
||
try { | ||
/** @var array<array-key, ChannelInterface> $allChannels */ | ||
$allChannels = $this->channelRepository->findAll(); | ||
foreach ($allChannels as $channel) { | ||
$channelId = $channel->getId(); | ||
if (!is_int($channelId) && !is_string($channelId)) { | ||
continue; | ||
} | ||
$this->messageBus->dispatch(new RemoveDocumentIfExists( | ||
$channelId, | ||
ProductDocumentType::CODE, | ||
$productId, | ||
)); | ||
} | ||
} catch (Throwable $throwable) { | ||
$this->logger->error($throwable->getMessage(), $throwable->getTrace()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.