<?php declare(strict_types=1);
namespace Wexo\ProductDownloads\Subscriber;
use Wexo\ProductDownloads\Service\ProductDownloadsService;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class PageSubscriber
*
* @package Mill\ProductDownloadsTab\Subscriber
*/
class PageSubscriber implements EventSubscriberInterface
{
/** @var SystemConfigService */
private $systemConfigService;
/** @var ProductDownloadsService */
private $productDownloadsService;
/**
* @param SystemConfigService $systemConfigService
* @param ProductDownloadsService $productDownloadsService
*/
public function __construct(
SystemConfigService $systemConfigService,
ProductDownloadsService $productDownloadsService
) {
$this->systemConfigService = $systemConfigService;
$this->productDownloadsService = $productDownloadsService;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
/**
* Event-function to extend the product page with product advantages
*
* @param ProductPageLoadedEvent $event
* @throws InconsistentCriteriaIdsException
*/
public function onProductPageLoaded(ProductPageLoadedEvent $event)
{
$salesChannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
if (! (bool) $this->systemConfigService->get('WexoProductDownloads.config.active', $salesChannelId)) {
return;
}
$mediaFiles = [];
$page = $event->getPage();
$product = $page->getProduct();
if (! empty($product)) {
$mediaFiles = $this->productDownloadsService->getProductDownloads(
$event->getSalesChannelContext()->getContext(),
$product
);
}
$product->assign(
[
'wexoProductDownloads' => [
'files' => $mediaFiles,
'forceLinkDownload' => (bool) $this->systemConfigService->get(
'WexoProductDownloads.config.forceLinkDownload',
$salesChannelId
),
'showTabBadge' => (bool) $this->systemConfigService->get(
'WexoProductDownloads.config.showTabBadge',
$salesChannelId
)
]
]
);
}
}