custom/static-plugins/product-downloads/src/Subscriber/PageSubscriber.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\ProductDownloads\Subscriber;
  3. use Wexo\ProductDownloads\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.     /** @var SystemConfigService */
  16.     private $systemConfigService;
  17.     /** @var ProductDownloadsService */
  18.     private $productDownloadsService;
  19.     /**
  20.      * @param SystemConfigService $systemConfigService
  21.      * @param ProductDownloadsService $productDownloadsService
  22.      */
  23.     public function __construct(
  24.         SystemConfigService $systemConfigService,
  25.         ProductDownloadsService $productDownloadsService
  26.     ) {
  27.         $this->systemConfigService $systemConfigService;
  28.         $this->productDownloadsService $productDownloadsService;
  29.     }
  30.     /**
  31.      * {@inheritDoc}
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  37.         ];
  38.     }
  39.     /**
  40.      * Event-function to extend the product page with product advantages
  41.      *
  42.      * @param ProductPageLoadedEvent $event
  43.      * @throws InconsistentCriteriaIdsException
  44.      */
  45.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  46.     {
  47.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  48.         if (! (bool) $this->systemConfigService->get('WexoProductDownloads.config.active'$salesChannelId)) {
  49.             return;
  50.         }
  51.         $mediaFiles = [];
  52.         $page $event->getPage();
  53.         $product $page->getProduct();
  54.         if (! empty($product)) {
  55.             $mediaFiles $this->productDownloadsService->getProductDownloads(
  56.                 $event->getSalesChannelContext()->getContext(),
  57.                 $product
  58.             );
  59.         }
  60.         $product->assign(
  61.             [
  62.                 'wexoProductDownloads' => [
  63.                     'files'                   => $mediaFiles,
  64.                     'forceLinkDownload'       => (bool) $this->systemConfigService->get(
  65.                         'WexoProductDownloads.config.forceLinkDownload',
  66.                         $salesChannelId
  67.                     ),
  68.                     'showTabBadge'            => (bool) $this->systemConfigService->get(
  69.                         'WexoProductDownloads.config.showTabBadge',
  70.                         $salesChannelId
  71.                     )
  72.                 ]
  73.             ]
  74.         );
  75.     }
  76. }