<?php
declare(strict_types=1);
namespace NetInventors\NetiNextEasyCouponDesigns\Subscriber;
use NetInventors\NetiNextEasyCoupon\Core\Content\EasyCoupon\EasyCouponEntity;
use NetInventors\NetiNextEasyCoupon\Storefront\Page\Account\VoucherListingPageLoadedEvent;
use NetInventors\NetiNextEasyCouponDesigns\Core\Content\Design\Aggregate\Variant\Aggregate\Type\TypeEntity;
use NetInventors\NetiNextEasyCouponDesigns\Core\Content\EasyCoupon\Aggregate\Media\MediaEntity;
use NetInventors\NetiNextEasyCouponDesigns\Struct\PluginConfigStruct;
use Shopware\Core\Checkout\Document\DocumentCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Account implements EventSubscriberInterface
{
private EntityRepositoryInterface $voucherRepository;
private PluginConfigStruct $pluginConfig;
private EntityRepositoryInterface $ecMediaRepository;
public function __construct(
EntityRepositoryInterface $voucherRepository,
PluginConfigStruct $pluginConfig,
EntityRepositoryInterface $ecMediaRepository
) {
$this->voucherRepository = $voucherRepository;
$this->pluginConfig = $pluginConfig;
$this->ecMediaRepository = $ecMediaRepository;
}
public static function getSubscribedEvents(): array
{
return [
VoucherListingPageLoadedEvent::class => 'onVoucherListing',
];
}
public function onVoucherListing(VoucherListingPageLoadedEvent $event): void
{
if (!$this->pluginConfig->isActive() || !$this->pluginConfig->isAccountDownloadEnabled()) {
return;
}
$page = $event->getPage();
$vouchers = $page->getVouchers();
$voucherIds = [];
$availableDocuments = [];
$availableMedia = [];
/** @var EasyCouponEntity $voucher */
foreach ($vouchers as $voucher) {
$voucherIds[] = $voucher->getId();
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('id', $voucherIds));
$criteria->addAssociation('documents.documentMediaFile');
$result = $this->voucherRepository->search($criteria, $event->getContext());
/** @var EasyCouponEntity $element */
foreach ($result->getElements() as $element) {
$documentsAvailable = false;
if (null === $element->getExtension('documents')->first()) {
continue;
}
$documents = $element->getExtension('documents');
if (!$documents instanceof DocumentCollection) {
continue;
}
foreach ($documents as $document) {
$typesArray = (array) ($document->getConfig()['variantTypes'] ?? []);
if (\in_array(TypeEntity::CODE_STANDARD_PDF, $typesArray, false)) {
$documentsAvailable = true;
break;
}
}
if ($documentsAvailable) {
$elementId = $element->getId();
$availableDocuments[$elementId] = $elementId;
}
}
$page->assign([ 'availableDesigns' => $availableDocuments ]);
$criteria = (new Criteria())->addFilter(new EqualsAnyFilter('easyCouponId', $voucherIds));
$ecMedia = $this->ecMediaRepository->search($criteria, $event->getContext());
/** @var MediaEntity $ecMedium */
foreach ($ecMedia->getElements() as $ecMedium) {
$types = $ecMedium->get('types');
$ecId = $ecMedium->get('easyCouponId');
$mediaId = $ecMedium->get('mediaId');
if (!\is_string($types) || !\is_string($ecId) || !\is_string($mediaId)) {
continue;
}
$typesArray = explode(',', $types);
if (!\in_array(TypeEntity::CODE_STANDARD_PDF, $typesArray, false)) {
continue;
}
$availableMedia[$ecId][] = $mediaId;
}
$page->assign([ 'availableMedia' => $availableMedia ]);
}
}