vendor/store.shopware.com/millproductdownloadstab/src/Subscriber/PageSubscriber.php line 89

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Mill\ProductDownloadsTab\Subscriber;
  3. use Mill\ProductDownloadsTab\Service\ProductDownloadsService;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9.  * Class PageSubscriber
  10.  *
  11.  * @package Mill\ProductDownloadsTab\Subscriber
  12.  */
  13. class PageSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var SystemConfigService
  17.      */
  18.     private $systemConfigService;
  19.     /**
  20.      * @var ProductDownloadsService
  21.      */
  22.     private $productDownloadsService;
  23.     /**
  24.      * @param SystemConfigService $systemConfigService
  25.      * @param ProductDownloadsService $productDownloadsService
  26.      */
  27.     public function __construct(
  28.         SystemConfigService $systemConfigService,
  29.         ProductDownloadsService $productDownloadsService
  30.     )
  31.     {
  32.         $this->systemConfigService $systemConfigService;
  33.         $this->productDownloadsService $productDownloadsService;
  34.     }
  35.     /**
  36.      * {@inheritDoc}
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             ProductPageLoadedEvent::class => ['onProductPageLoaded'100]
  42.         ];
  43.     }
  44.     /**
  45.      * Event-function to extend the product page with product advantages
  46.      *
  47.      * @param ProductPageLoadedEvent $event
  48.      * @throws InconsistentCriteriaIdsException
  49.      */
  50.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  51.     {
  52.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  53.         if (!(bool) $this->systemConfigService->get('MillProductDownloadsTab.config.active'$salesChannelId)) {
  54.             return;
  55.         }
  56.         $page $event->getPage();
  57.         $product $page->getProduct();
  58.         $mediaFiles = [];
  59.         if (!empty($product)) {
  60.             $mediaFiles $this->productDownloadsService->getProductDownloads($event->getSalesChannelContext()->getContext(), $product);
  61.         }
  62.         $page->assign(
  63.             [
  64.                 'MillProductDownloadsTab' => [
  65.                     'files' => $mediaFiles
  66.                 ]
  67.             ]
  68.         );
  69.     }
  70. }