vendor/bitbag/elasticsearch-plugin/src/EventListener/ResourceIndexListener.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3. * This file has been created by developers from BitBag.
  4. * Feel free to contact us once you face any issues or want to start
  5. * another great project.
  6. * You can find more information about us on https://bitbag.io and write us
  7. * an email on hello@bitbag.io.
  8. */
  9. declare(strict_types=1);
  10. namespace BitBag\SyliusElasticsearchPlugin\EventListener;
  11. use BitBag\SyliusElasticsearchPlugin\Refresher\ResourceRefresherInterface;
  12. use Sylius\Component\Core\Model\Product;
  13. use Sylius\Component\Product\Model\ProductAttribute;
  14. use Sylius\Component\Resource\Model\ResourceInterface;
  15. use Sylius\Component\Resource\Repository\RepositoryInterface;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Webmozart\Assert\Assert;
  18. final class ResourceIndexListener implements ResourceIndexListenerInterface
  19. {
  20. private ResourceRefresherInterface $resourceRefresher;
  21. private array $persistersMap;
  22. private RepositoryInterface $attributeRepository;
  23. public function __construct(
  24. ResourceRefresherInterface $resourceRefresher,
  25. array $persistersMap,
  26. RepositoryInterface $attributeRepository
  27. ) {
  28. $this->resourceRefresher = $resourceRefresher;
  29. $this->persistersMap = $persistersMap;
  30. $this->attributeRepository = $attributeRepository;
  31. }
  32. public function updateIndex(GenericEvent $event): void
  33. {
  34. $resource = $event->getSubject();
  35. Assert::isInstanceOf($resource, ResourceInterface::class);
  36. foreach ($this->persistersMap as $config) {
  37. $method = $config[self::GET_PARENT_METHOD_KEY] ?? null;
  38. if (null !== $method && method_exists($resource, $method)) {
  39. $resource = $resource->$method();
  40. }
  41. if ($resource instanceof $config[self::MODEL_KEY]) {
  42. $this->resourceRefresher->refresh($resource, $config[self::SERVICE_ID_KEY]);
  43. }
  44. if ($resource instanceof Product
  45. && ProductAttribute::class === $config[self::MODEL_KEY]
  46. ) {
  47. foreach ($this->attributeRepository->findAll() as $attribute) {
  48. $this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]);
  49. }
  50. }
  51. }
  52. }
  53. }