custom/static-plugins/GlobusSW6/src/Subscriber/Biocide/ProductBiocideExtensionSubscriber.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber\Biocide;
  3. use GlobusSW6\Service\BiocideApi\BiocideApiService;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Content\Product\ProductCollection;
  6. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use GlobusSW6\Service\IaneoBusinessExtension;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  10. class ProductBiocideExtensionSubscriber implements EventSubscriberInterface
  11. {
  12.     /** @var LoggerInterface  */
  13.     private $logger;
  14.     /** @var IaneoBusinessExtension */
  15.     private $ianeoBusinessExtension;
  16.     /** @var BiocideApiService */
  17.     private $biocideApiService;
  18.     /**
  19.      * ProductBiocideExtensionSubscriber constructor.
  20.      * @param LoggerInterface $logger
  21.      * @param IaneoBusinessExtension $ianeoBusinessExtension
  22.      * @param BiocideApiService $biocideApiService
  23.      */
  24.     public function __construct(LoggerInterface $loggerIaneoBusinessExtension $ianeoBusinessExtensionBiocideApiService $biocideApiService)
  25.     {
  26.         $this->logger $logger;
  27.         $this->ianeoBusinessExtension $ianeoBusinessExtension;
  28.         $this->biocideApiService $biocideApiService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             'product.search.result.loaded' => 'onProductLoaded'
  34.         ];
  35.     }
  36.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  37.     {
  38.         /** @var ProductCollection $products */
  39.         $products $event->getResult()->getEntities();
  40.         if ($products === null) {
  41.             return;
  42.         }
  43.         if ($event->getContext()->getSource() instanceOf SalesChannelApiSource)
  44.         {
  45.             foreach ($products as $product)
  46.             {
  47.                 try {
  48.                     $biocide $this->biocideApiService->getBiocideByProductId($product->getId(), $event->getContext());
  49.                     if($biocide) {
  50.                         $this->ianeoBusinessExtension->addSingleAttributeToProduct($product$biocide'biocide');
  51.                     }
  52.                 } catch(\Exception $exception) {
  53.                     $this->logger->error(__CLASS__ ":" __FUNCTION__ ":" __LINE__ ":\$exception->getMessage(): " $exception->getMessage());
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }