Skip to content

Commit

Permalink
OP-225 fix not populated options changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriz83 committed Apr 12, 2024
1 parent ef7c5d1 commit 88bbe99
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
59 changes: 48 additions & 11 deletions src/EventListener/ResourceIndexListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use BitBag\SyliusElasticsearchPlugin\Refresher\ResourceRefresherInterface;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Product\Model\ProductAttribute;
use Sylius\Component\Product\Model\ProductOption;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
Expand All @@ -28,14 +29,18 @@ final class ResourceIndexListener implements ResourceIndexListenerInterface

private RepositoryInterface $attributeRepository;

private RepositoryInterface $productOptionRepository;

public function __construct(
ResourceRefresherInterface $resourceRefresher,
array $persistersMap,
RepositoryInterface $attributeRepository
RepositoryInterface $attributeRepository,
RepositoryInterface $productOptionRepository
) {
$this->resourceRefresher = $resourceRefresher;
$this->persistersMap = $persistersMap;
$this->attributeRepository = $attributeRepository;
$this->productOptionRepository = $productOptionRepository;
}

public function updateIndex(GenericEvent $event): void
Expand All @@ -50,17 +55,49 @@ public function updateIndex(GenericEvent $event): void
$resource = $resource->$method();
}

if ($resource instanceof $config[self::MODEL_KEY]) {
$this->resourceRefresher->refresh($resource, $config[self::SERVICE_ID_KEY]);
}
$this->refreshResource($resource, $config);
}
}

if ($resource instanceof Product
&& ProductAttribute::class === $config[self::MODEL_KEY]
) {
foreach ($this->attributeRepository->findAll() as $attribute) {
$this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]);
}
}
private function refreshResource(
ResourceInterface $resource,
array $config
): void {
if ($resource instanceof $config[self::MODEL_KEY]) {
$this->resourceRefresher->refresh(
$resource,
$config[self::SERVICE_ID_KEY]
);
}

if (!$resource instanceof Product) {
return;
}

$resourceRepository = $this->getResourceRespository($config);

if (null === $resourceRepository) {
return;
}

foreach ($resourceRepository->findAll() as $resourceItem) {
$this->resourceRefresher->refresh(
$resourceItem,
$config[self::SERVICE_ID_KEY]
);
}
}

private function getResourceRespository(array $config): ?RepositoryInterface
{
if (ProductAttribute::class === $config[self::MODEL_KEY]) {
return $this->attributeRepository;
}

if (ProductOption::class === $config[self::MODEL_KEY]) {
return $this->productOptionRepository;
}

return null;
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
</argument>
</argument>
<argument type="service" id="sylius.repository.product_attribute"/>
<argument type="service" id="sylius.repository.product_option"/>
<tag name="kernel.event_listener" event="sylius.product_attribute.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_attribute.post_update" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.option.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.option.post_update" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.option.post_delete" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product.post_update" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product.post_delete" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_variant.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_variant.post_update" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_variant.post_delete" method="updateIndex" />
</service>
<service id="bitbag_sylius_elasticsearch_plugin.event_listener.product_taxon_index" class="BitBag\SyliusElasticsearchPlugin\EventListener\ProductTaxonIndexListener">
<argument type="service" id="bitbag.sylius_elasticsearch_plugin.refresher.resource" />
Expand Down

0 comments on commit 88bbe99

Please sign in to comment.