vendor/ibexa/product-catalog/src/lib/Search/Common/EventSubscriber/ProductEventSubscriber.php line 44

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\ProductCatalog\Search\Common\EventSubscriber;
  8. use Ibexa\Contracts\ProductCatalog\Local\Events\CreateProductAvailabilityEvent;
  9. use Ibexa\Contracts\ProductCatalog\Local\Events\DecreaseProductAvailabilityEvent;
  10. use Ibexa\Contracts\ProductCatalog\Local\Events\DeleteProductAvailabilityEvent;
  11. use Ibexa\Contracts\ProductCatalog\Local\Events\IncreaseProductAvailabilityEvent;
  12. use Ibexa\Contracts\ProductCatalog\Local\Events\UpdateProductAvailabilityEvent;
  13. use Ibexa\Contracts\ProductCatalog\Values\ContentAwareProductInterface;
  14. use Ibexa\Contracts\ProductCatalog\Values\ProductInterface;
  15. use Ibexa\Core\Search\Common\EventSubscriber\AbstractSearchEventSubscriber;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. final class ProductEventSubscriber extends AbstractSearchEventSubscriber implements EventSubscriberInterface
  18. {
  19. public static function getSubscribedEvents(): array
  20. {
  21. return [
  22. CreateProductAvailabilityEvent::class => 'onCreateAvailability',
  23. DeleteProductAvailabilityEvent::class => 'onDeleteAvailability',
  24. UpdateProductAvailabilityEvent::class => 'onUpdateAvailability',
  25. IncreaseProductAvailabilityEvent::class => 'onIncreaseAvailability',
  26. DecreaseProductAvailabilityEvent::class => 'onDecreaseAvailability',
  27. ];
  28. }
  29. public function onCreateAvailability(CreateProductAvailabilityEvent $event): void
  30. {
  31. $this->indexProduct($event->getCreateStruct()->getProduct());
  32. }
  33. public function onDeleteAvailability(DeleteProductAvailabilityEvent $event): void
  34. {
  35. $this->indexProduct($event->getProduct());
  36. }
  37. public function onUpdateAvailability(UpdateProductAvailabilityEvent $event): void
  38. {
  39. $this->indexProduct($event->getUpdateStruct()->getProduct());
  40. }
  41. public function onIncreaseAvailability(IncreaseProductAvailabilityEvent $event): void
  42. {
  43. $this->indexProduct($event->getProduct());
  44. }
  45. public function onDecreaseAvailability(DecreaseProductAvailabilityEvent $event): void
  46. {
  47. $this->indexProduct($event->getProduct());
  48. }
  49. private function indexProduct(ProductInterface $product): void
  50. {
  51. if (!$product instanceof ContentAwareProductInterface) {
  52. return;
  53. }
  54. $content = $product->getContent();
  55. $contentId = $content->getVersionInfo()->getContentInfo()->id;
  56. $this->searchHandler->indexContent(
  57. $this->persistenceHandler->contentHandler()->load(
  58. $contentId,
  59. $content->getVersionInfo()->versionNo
  60. )
  61. );
  62. $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
  63. $contentId
  64. );
  65. foreach ($locations as $location) {
  66. $this->searchHandler->indexLocation($location);
  67. }
  68. }
  69. }