vendor/ibexa/product-catalog/src/bundle/EventSubscriber/PriceSearchEventSubscriber.php line 90

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\ProductCatalog\EventSubscriber;
  8. use Ibexa\Contracts\Core\Persistence\Handler as PersistenceHandler;
  9. use Ibexa\Contracts\Core\Search\Handler as SearchHandler;
  10. use Ibexa\Contracts\ProductCatalog\Events\CreatePriceEvent;
  11. use Ibexa\Contracts\ProductCatalog\Events\DeletePriceEvent;
  12. use Ibexa\Contracts\ProductCatalog\Events\ExecutePriceStructsEvent;
  13. use Ibexa\Contracts\ProductCatalog\Events\UpdatePriceEvent;
  14. use Ibexa\Contracts\ProductCatalog\Values\ContentAwareProductInterface;
  15. use Ibexa\Contracts\ProductCatalog\Values\ProductInterface;
  16. use Ibexa\ProductCatalog\Config\ConfigProviderInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. final class PriceSearchEventSubscriber implements EventSubscriberInterface
  19. {
  20. private ConfigProviderInterface $configProvider;
  21. private SearchHandler $searchHandler;
  22. private PersistenceHandler $persistenceHandler;
  23. public function __construct(
  24. ConfigProviderInterface $configProvider,
  25. SearchHandler $searchHandler,
  26. PersistenceHandler $persistenceHandler
  27. ) {
  28. $this->configProvider = $configProvider;
  29. $this->searchHandler = $searchHandler;
  30. $this->persistenceHandler = $persistenceHandler;
  31. }
  32. public static function getSubscribedEvents(): array
  33. {
  34. return [
  35. CreatePriceEvent::class => ['onCreatePrice', -10],
  36. DeletePriceEvent::class => ['onDeletePrice', -10],
  37. ExecutePriceStructsEvent::class => ['onExecutePriceStructs', -10],
  38. UpdatePriceEvent::class => ['onUpdatePrice', -10],
  39. ];
  40. }
  41. public function onCreatePrice(CreatePriceEvent $event): void
  42. {
  43. if (!$this->isLocalProductCatalog()) {
  44. return;
  45. }
  46. $this->updateIndex($event->getPrice()->getProduct());
  47. }
  48. public function onDeletePrice(DeletePriceEvent $event): void
  49. {
  50. if (!$this->isLocalProductCatalog()) {
  51. return;
  52. }
  53. $this->updateIndex($event->getDeleteStruct()->getPrice()->getProduct());
  54. }
  55. public function onExecutePriceStructs(ExecutePriceStructsEvent $event): void
  56. {
  57. if (!$this->isLocalProductCatalog()) {
  58. return;
  59. }
  60. $alreadyUpdatedProducts = [];
  61. foreach ($event->getPriceStructs() as $struct) {
  62. $product = $struct->getProduct();
  63. if (!($product instanceof ContentAwareProductInterface)) {
  64. continue;
  65. }
  66. if (in_array($product->getCode(), $alreadyUpdatedProducts, true)) {
  67. continue;
  68. }
  69. $this->updateIndex($product);
  70. $alreadyUpdatedProducts[] = $product->getCode();
  71. }
  72. }
  73. public function onUpdatePrice(UpdatePriceEvent $event): void
  74. {
  75. if (!$this->isLocalProductCatalog()) {
  76. return;
  77. }
  78. $this->updateIndex($event->getPrice()->getProduct());
  79. }
  80. private function updateIndex(ProductInterface $product): void
  81. {
  82. if (!($product instanceof ContentAwareProductInterface)) {
  83. return;
  84. }
  85. $versionInfo = $product->getContent()->getVersionInfo();
  86. $contentId = $versionInfo->getContentInfo()->id;
  87. $versionNo = $versionInfo->versionNo;
  88. $this->searchHandler->indexContent(
  89. $this->persistenceHandler->contentHandler()->load($contentId, $versionNo)
  90. );
  91. $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentId);
  92. foreach ($locations as $location) {
  93. $this->searchHandler->indexLocation($location);
  94. }
  95. }
  96. private function isLocalProductCatalog(): bool
  97. {
  98. return $this->configProvider->getEngineType() === 'local';
  99. }
  100. }