vendor/store.shopware.com/cogicontest/src/Subscriber/ContestCmsSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace Cogi\CogiContest\Subscriber;
  3. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  4. use Shopware\Core\Content\Cms\CmsPageEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Core\System\Salutation\SalutationCollection;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class ContestCmsSubscriber implements EventSubscriberInterface {
  14.     /**
  15.      * @var SalesChannelContext
  16.      */
  17.     protected $salesChannelContext;
  18.     /**
  19.      * @var EntityRepositoryInterface
  20.      */
  21.     private $contestRepository;
  22.     /**
  23.      * @var EntityRepositoryInterface
  24.      */
  25.     private $contestMemberRepository;
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private $salutationsRepository;
  30.     /**
  31.      * @var SystemConfigService
  32.      */
  33.     private $systemConfigService;
  34.     private $config;
  35.     public function __construct(
  36.         EntityRepositoryInterface $contestRepository,
  37.         EntityRepositoryInterface $contestMemberRepository,
  38.         EntityRepositoryInterface $salutationsRepository,
  39.         SystemConfigService $systemConfigService
  40.     ) {
  41.         $this->contestRepository $contestRepository;
  42.         $this->contestMemberRepository $contestMemberRepository;
  43.         $this->salutationsRepository $salutationsRepository;
  44.         $this->systemConfigService $systemConfigService;
  45.         $this->config $this->systemConfigService->getDomain('CogiContest.config');
  46.     }
  47.     public static function getSubscribedEvents(): array {
  48.         return [
  49.             CmsPageEvents::SLOT_LOADED_EVENT => 'onSlotLoadedEvent'
  50.         ];
  51.     }
  52.     public function onSlotLoadedEvent(EntityLoadedEvent $event){
  53.         $entities $event->getEntities();
  54.         /** @var  CmsSlotEntity $entity */
  55.         foreach ($entities as $entity){
  56.             if($entity->getType() === 'contest'){
  57.                 try{
  58.                     $criteria = new Criteria([$entity->getConfig()['contestId']['value']]);
  59.                     $contest $this->contestRepository->search($criteria$event->getContext())->getEntities();
  60.                     $entity->setData(new ArrayStruct(['contest' => $contest]));
  61.                     $salutationCriteria = new Criteria();
  62.                     /** @var SalutationCollection $salutations */
  63.                     $salutations $this->salutationsRepository->search($salutationCriteria$event->getContext())->getEntities();
  64.                     $entity->setExtensions(['Salutations' => $salutations]);
  65.                 } catch (\Exception $e){
  66.                     return $e;
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }