refresh product on variant save

+ added possible to populate parent on another resource action (add new / edit)
This commit is contained in:
christopherhero
2021-06-18 02:46:16 +02:00
parent 8f058a52f4
commit 2089ca6e72
3 changed files with 50 additions and 7 deletions

View File

@@ -17,7 +17,7 @@ use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
final class ResourceIndexListener
final class ResourceIndexListener implements ResourceIndexListenerInterface
{
/** @var ResourceRefresherInterface */
private $resourceRefresher;
@@ -36,10 +36,15 @@ final class ResourceIndexListener
$resource = $event->getSubject();
Assert::isInstanceOf($resource, ResourceInterface::class);
foreach ($this->persistersMap as $config) {
$method = $config[self::GET_PARENT_METHOD_KEY] ?? null;
foreach ($this->persistersMap as $objectPersisterId => $modelClass) {
if ($resource instanceof $modelClass) {
$this->resourceRefresher->refresh($resource, $objectPersisterId);
if (null !== $method && method_exists($resource, $method)) {
$resource = $resource->$method();
}
if ($resource instanceof $config[self::MODEL_KEY]) {
$this->resourceRefresher->refresh($resource, $config[self::SERVICE_ID_KEY]);
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/
namespace BitBag\SyliusElasticsearchPlugin\EventListener;
use Symfony\Component\EventDispatcher\GenericEvent;
interface ResourceIndexListenerInterface
{
public const GET_PARENT_METHOD_KEY = 'getParentMethod';
public const MODEL_KEY = 'model';
public const SERVICE_ID_KEY = 'serviceId';
public function updateIndex(GenericEvent $event): void;
}

View File

@@ -5,9 +5,19 @@
<service id="bitbag_sylius_elasticsearch_plugin.event_listener.resource_index" class="BitBag\SyliusElasticsearchPlugin\EventListener\ResourceIndexListener">
<argument type="service" id="bitbag.sylius_elasticsearch_plugin.refresher.resource" />
<argument type="collection">
<argument key="fos_elastica.object_persister.bitbag_attribute_taxons.default">%sylius.model.product_attribute.class%</argument>
<argument key="fos_elastica.object_persister.bitbag_option_taxons.default">%sylius.model.product_option.class%</argument>
<argument key="fos_elastica.object_persister.bitbag_shop_product.default">%sylius.model.product.class%</argument>
<argument type="collection">
<argument key="model">%sylius.model.product_attribute.class%</argument>
<argument key="serviceId">fos_elastica.object_persister.bitbag_attribute_taxons.default</argument>
</argument>
<argument type="collection">
<argument key="model">%sylius.model.product_option.class%</argument>
<argument key="serviceId">fos_elastica.object_persister.bitbag_option_taxons.default</argument>
</argument>
<argument type="collection">
<argument key="getParentMethod">getProduct</argument>
<argument key="model">%sylius.model.product.class%</argument>
<argument key="serviceId">fos_elastica.object_persister.bitbag_shop_product.default</argument>
</argument>
</argument>
<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" />
@@ -15,6 +25,8 @@
<tag name="kernel.event_listener" event="sylius.option.post_update" 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_variant.post_create" method="updateIndex" />
<tag name="kernel.event_listener" event="sylius.product_variant.post_update" method="updateIndex" />
</service>
<service id="bitbag_sylius_elasticsearch_plugin.event_listener.order_products" class="BitBag\SyliusElasticsearchPlugin\EventListener\OrderProductsListener" public="true">
<argument type="service" id="bitbag.sylius_elasticsearch_plugin.refresher.resource" />