<?php
namespace Cogi\CogiContest\Subscriber;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\CmsPageEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\Salutation\SalutationCollection;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContestCmsSubscriber implements EventSubscriberInterface {
/**
* @var SalesChannelContext
*/
protected $salesChannelContext;
/**
* @var EntityRepositoryInterface
*/
private $contestRepository;
/**
* @var EntityRepositoryInterface
*/
private $contestMemberRepository;
/**
* @var EntityRepositoryInterface
*/
private $salutationsRepository;
/**
* @var SystemConfigService
*/
private $systemConfigService;
private $config;
public function __construct(
EntityRepositoryInterface $contestRepository,
EntityRepositoryInterface $contestMemberRepository,
EntityRepositoryInterface $salutationsRepository,
SystemConfigService $systemConfigService
) {
$this->contestRepository = $contestRepository;
$this->contestMemberRepository = $contestMemberRepository;
$this->salutationsRepository = $salutationsRepository;
$this->systemConfigService = $systemConfigService;
$this->config = $this->systemConfigService->getDomain('CogiContest.config');
}
public static function getSubscribedEvents(): array {
return [
CmsPageEvents::SLOT_LOADED_EVENT => 'onSlotLoadedEvent'
];
}
public function onSlotLoadedEvent(EntityLoadedEvent $event){
$entities = $event->getEntities();
/** @var CmsSlotEntity $entity */
foreach ($entities as $entity){
if($entity->getType() === 'contest'){
try{
$criteria = new Criteria([$entity->getConfig()['contestId']['value']]);
$contest = $this->contestRepository->search($criteria, $event->getContext())->getEntities();
$entity->setData(new ArrayStruct(['contest' => $contest]));
$salutationCriteria = new Criteria();
/** @var SalutationCollection $salutations */
$salutations = $this->salutationsRepository->search($salutationCriteria, $event->getContext())->getEntities();
$entity->setExtensions(['Salutations' => $salutations]);
} catch (\Exception $e){
return $e;
}
}
}
}
}