custom/plugins/NetiNextEasyCouponDesigns/src/Subscriber/Account.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCouponDesigns\Subscriber;
  4. use NetInventors\NetiNextEasyCoupon\Core\Content\EasyCoupon\EasyCouponEntity;
  5. use NetInventors\NetiNextEasyCoupon\Storefront\Page\Account\VoucherListingPageLoadedEvent;
  6. use NetInventors\NetiNextEasyCouponDesigns\Core\Content\Design\Aggregate\Variant\Aggregate\Type\TypeEntity;
  7. use NetInventors\NetiNextEasyCouponDesigns\Core\Content\EasyCoupon\Aggregate\Media\MediaEntity;
  8. use NetInventors\NetiNextEasyCouponDesigns\Struct\PluginConfigStruct;
  9. use Shopware\Core\Checkout\Document\DocumentCollection;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class Account implements EventSubscriberInterface
  15. {
  16.     private EntityRepositoryInterface $voucherRepository;
  17.     private PluginConfigStruct              $pluginConfig;
  18.     private EntityRepositoryInterface $ecMediaRepository;
  19.     public function __construct(
  20.         EntityRepositoryInterface $voucherRepository,
  21.         PluginConfigStruct              $pluginConfig,
  22.         EntityRepositoryInterface $ecMediaRepository
  23.     ) {
  24.         $this->voucherRepository $voucherRepository;
  25.         $this->pluginConfig      $pluginConfig;
  26.         $this->ecMediaRepository $ecMediaRepository;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             VoucherListingPageLoadedEvent::class => 'onVoucherListing',
  32.         ];
  33.     }
  34.     public function onVoucherListing(VoucherListingPageLoadedEvent $event): void
  35.     {
  36.         if (!$this->pluginConfig->isActive() || !$this->pluginConfig->isAccountDownloadEnabled()) {
  37.             return;
  38.         }
  39.         $page               $event->getPage();
  40.         $vouchers           $page->getVouchers();
  41.         $voucherIds         = [];
  42.         $availableDocuments = [];
  43.         $availableMedia     = [];
  44.         /** @var EasyCouponEntity $voucher */
  45.         foreach ($vouchers as $voucher) {
  46.             $voucherIds[] = $voucher->getId();
  47.         }
  48.         $criteria = new Criteria();
  49.         $criteria->addFilter(new EqualsAnyFilter('id'$voucherIds));
  50.         $criteria->addAssociation('documents.documentMediaFile');
  51.         $result $this->voucherRepository->search($criteria$event->getContext());
  52.         /** @var EasyCouponEntity $element */
  53.         foreach ($result->getElements() as $element) {
  54.             $documentsAvailable false;
  55.             if (null === $element->getExtension('documents')->first()) {
  56.                 continue;
  57.             }
  58.             $documents $element->getExtension('documents');
  59.             if (!$documents instanceof DocumentCollection) {
  60.                 continue;
  61.             }
  62.             foreach ($documents as $document) {
  63.                 $typesArray = (array) ($document->getConfig()['variantTypes'] ?? []);
  64.                 if (\in_array(TypeEntity::CODE_STANDARD_PDF$typesArrayfalse)) {
  65.                     $documentsAvailable true;
  66.                     break;
  67.                 }
  68.             }
  69.             if ($documentsAvailable) {
  70.                 $elementId                      $element->getId();
  71.                 $availableDocuments[$elementId] = $elementId;
  72.             }
  73.         }
  74.         $page->assign([ 'availableDesigns' => $availableDocuments ]);
  75.         $criteria = (new Criteria())->addFilter(new EqualsAnyFilter('easyCouponId'$voucherIds));
  76.         $ecMedia $this->ecMediaRepository->search($criteria$event->getContext());
  77.         /** @var MediaEntity $ecMedium */
  78.         foreach ($ecMedia->getElements() as $ecMedium) {
  79.             $types   $ecMedium->get('types');
  80.             $ecId    $ecMedium->get('easyCouponId');
  81.             $mediaId $ecMedium->get('mediaId');
  82.             if (!\is_string($types) || !\is_string($ecId) || !\is_string($mediaId)) {
  83.                 continue;
  84.             }
  85.             $typesArray explode(','$types);
  86.             if (!\in_array(TypeEntity::CODE_STANDARD_PDF$typesArrayfalse)) {
  87.                 continue;
  88.             }
  89.             $availableMedia[$ecId][] = $mediaId;
  90.         }
  91.         $page->assign([ 'availableMedia' => $availableMedia ]);
  92.     }
  93. }