vendor/crehler/arrangements/src/Subscriber/MediaIntegrationSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Dariusz Indyk <d.indyk@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   05.01.2021
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Arrangements\Subscriber;
  13. use Crehler\Arrangements\Entity\Arrangement\Aggregate\ArrangementProduct\ArrangementProductEntity;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. class MediaIntegrationSubscriber implements EventSubscriberInterface
  19. {
  20.     private EventDispatcherInterface $eventDispatcher;
  21.     public function __construct(EventDispatcherInterface $eventDispatcher)
  22.     {
  23.         $this->eventDispatcher $eventDispatcher;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.               'arrangement_media.loaded' => ['onArrangementMediaLoaded'],
  29.               'arrangement_product.loaded' => ['onArrangementProductLoaded'],
  30.               'arrangement_media.written' => ['onArrangementMediaWritten']
  31.         ];
  32.     }
  33.     public function onArrangementMediaLoaded(EntityLoadedEvent $event): void
  34.     {
  35.         $mediaDefinition $event->getDefinition();
  36.         $newEvent = new EntityLoadedEvent($mediaDefinition$event->getEntities(), $event->getContext());
  37.         $this->eventDispatcher->dispatch($newEvent'crehler.custom_media_wrapped.loaded');
  38.     }
  39.     public function onArrangementProductLoaded(EntityLoadedEvent $event): void
  40.     {
  41.         $productMediaDefinition $event->getDefinition()->getField('product')->getReferenceDefinition()->getField('cover')->getReferenceDefinition();
  42.         $mediaDefinition $productMediaDefinition->getField('media')->getReferenceDefinition();
  43.         $productMedias = [];
  44.         $medias = [];
  45.         /** @var ArrangementProductEntity $entity */
  46.         foreach ($event->getEntities() as $entity) {
  47.             if (!$entity->getProduct() || !$entity->getProduct()->getCover() || !$entity->getProduct()->getCover()->getMedia()) {
  48.                 continue;
  49.             }
  50.             $productMedias[] = $entity->getProduct()->getCover();
  51.             $medias[] = $entity->getProduct()->getCover()->getMedia();
  52.         }
  53.         if (!empty($medias)) {
  54.             $newEvent = new EntityLoadedEvent($productMediaDefinition$productMedias$event->getContext());
  55.             $this->eventDispatcher->dispatch($newEvent'crehler.custom_media_wrapped.loaded');
  56.             $newEvent = new EntityLoadedEvent($mediaDefinition$medias$event->getContext());
  57.             $this->eventDispatcher->dispatch($newEvent'crehler.custom_media.loaded');
  58.         }
  59.     }
  60.     public function onArrangementMediaWritten(EntityWrittenEvent $event)
  61.     {
  62.         foreach ($event->getWriteResults() as $writeResult) {
  63.             if (isset($writeResult->getPayload()['mediaId'])) {
  64.                 $this->eventDispatcher->dispatch($event'crehler.image_booster.generate_media');
  65.             }
  66.         }
  67.     }
  68. }