vendor/crehler/inspirations/src/Subscriber/ProductListingResultSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   25 wrz 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Inspirations\Subscriber;
  13. use Crehler\Inspirations\Entity\Inspiration\Aggregate\InspirationProduct\InspirationProductEntity;
  14. use Crehler\Inspirations\Service\InspirationService;
  15. use Crehler\MojeBambinoEngine\Service\ProductStatusStorefrontService;
  16. use PhpOffice\PhpSpreadsheet\Calculation\Logical\Boolean;
  17. use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
  18. use Shopware\Core\Content\Product\ProductEvents;
  19. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  20. use Shopware\Core\Framework\Context;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  22. use Shopware\Core\Framework\Struct\ArrayStruct;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class ProductListingResultSubscriber implements EventSubscriberInterface
  25. {
  26.     private InspirationService $inspirationService;
  27.     private ProductStatusStorefrontService $productStatusService;
  28.     private bool $skip false;
  29.     public function __construct(InspirationService $inspirationServiceProductStatusStorefrontService $productStatusService)
  30.     {
  31.         $this->inspirationService $inspirationService;
  32.         $this->productStatusService $productStatusService;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             ProductIndexerEvent::class =>['onProductIndexer'1],
  38.             ProductEvents::PRODUCT_LOADED_EVENT => 'handleProductListingResult',
  39.             'arrangement_product.loaded' => 'handleProductInspirationLoaded'
  40.         ];
  41.     }
  42.     public function onProductIndexer($event)
  43.     {
  44.         $this->skip true;
  45.     }
  46.     public function handleProductInspirationLoaded(EntityLoadedEvent $event): void
  47.     {
  48.         /** @var InspirationProductEntity $inspirationProduct */
  49.         foreach ($event->getEntities() as $inspirationProduct) {
  50.             $product $inspirationProduct->getProduct();
  51.             $product->addExtension('productStatus'$this->productStatusService->getProductStatus($product$event->getContext()));
  52.         }
  53.     }
  54.     public function handleProductListingResult(EntityLoadedEvent $event): void
  55.     {
  56.         if ($event->getContext()->getScope() === Context::CRUD_API_SCOPE ||
  57.             $event->getContext()->getScope() === Context::SYSTEM_SCOPE ||
  58.             $this->skip) {
  59.             return;
  60.         }
  61.         $products $event->getEntities();
  62.         $context $event->getContext();
  63.         /** @var SalesChannelProductEntity $product */
  64.         foreach ($products as $product) {
  65.             /** @var ArrayStruct $foreignKeys */
  66.             $foreignKeys $product->getExtension('foreignKeys');
  67.             if (null !== $foreignKeys->offsetGet('inspirationId')) {
  68.                 $inspiration $this->inspirationService->getInspirationById(
  69.                     $foreignKeys->offsetGet('inspirationId'),
  70.                     $context
  71.                 );
  72.                 $product->addExtension('inspiration'$inspiration);
  73.             }
  74.         }
  75.     }
  76. }