custom/static-plugins/GlobusSW6/src/Subscriber/Mdr/ProductMdrExtensionSubscriber.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber\Mdr;
  3. use Shopware\Core\Content\Product\ProductCollection;
  4. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use GlobusSW6\Service\MdrApi\MdrApiService;
  8. use Psr\Log\LoggerInterface;
  9. use GlobusSW6\Service\IaneoBusinessExtension;
  10. class ProductMdrExtensionSubscriber implements EventSubscriberInterface
  11. {
  12.     /** @var LoggerInterface  */
  13.     private $logger;
  14.     /** @var IaneoBusinessExtension */
  15.     private $ianeoBusinessExtension;
  16.     /** @var MdrApiService */
  17.     private $mdrApiService;
  18.     /**
  19.      * ProductMdrExtensionSubscriber constructor.
  20.      * @param LoggerInterface $logger
  21.      * @param IaneoBusinessExtension $ianeoBusinessExtension
  22.      * @param MdrApiService $mdrApiService
  23.      */
  24.     public function __construct(LoggerInterface $loggerIaneoBusinessExtension $ianeoBusinessExtensionMdrApiService $mdrApiService)
  25.     {
  26.         $this->logger $logger;
  27.         $this->ianeoBusinessExtension $ianeoBusinessExtension;
  28.         $this->mdrApiService $mdrApiService;
  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.                 try {
  47.                     $mdr $this->mdrApiService->getMdrByProductId($product->getId(), $event->getContext());
  48.                     if ($mdr) {
  49.                         $this->ianeoBusinessExtension->addSingleAttributeToProduct($product$mdr'mdr');
  50.                     }
  51.                 } catch (\Exception $exception) {
  52.                     $this->logger->error(__CLASS__ ":" __FUNCTION__ ":" __LINE__ ":\$exception->getMessage(): " $exception->getMessage());
  53.                 }
  54.             }
  55.         }
  56.     }
  57. }