|
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
- use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface;
- use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
- use Lc\CaracoleBundle\Resolver\MerchantResolver;
- use Lc\CaracoleBundle\Resolver\SectionResolver;
- use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
- class CreateEntityEventSubscriber implements EventSubscriberInterface
- {
- protected $em;
- protected $merchantResolver;
- protected $sectionResolver;
-
- public function __construct(EntityManagerInterface $entityManager, MerchantResolver $merchantResolver, SectionResolver $sectionResolver)
- {
- $this->em = $entityManager;
- $this->merchantResolver = $merchantResolver;
- $this->sectionResolver = $sectionResolver;
-
- }
-
- public static function getSubscribedEvents()
- {
- return [
- EntityManagerEvent::CREATE_EVENT => ['createEntity'],
- ];
- }
-
- public function createEntity(EntityManagerEvent $event)
- {
- $entity = $event->getEntity();
- $entityRepository = $this->em->getRepository(get_class($entity));
-
- if ($entity instanceof FilterSectionInterface) {
- $this->setSectionProperty($entity, $entityRepository);
- }
-
- if ($entity instanceof FilterMerchantInterface) {
- $this->setMerchantProperty($entity, $entityRepository);
- }
-
- if ($entity instanceof FilterMultipleMerchantsInterface) {
- $this->setMultipleMerchantProperty($entity, $entityRepository);
- }
-
-
- }
-
- private function setSectionProperty($entity)
- {
- $entity->setSection($this->sectionResolver->getCurrent());
- }
-
- private function setMerchantProperty($entity)
- {
- $entity->setMerchant($this->merchantResolver->getCurrent());
- }
-
- private function setMultipleMerchantProperty($entity)
- {
- if ($entity->getMerchants()->isEmpty()) {
- $entity->addMerchant($this->merchantResolver->getCurrent());
- }
- }
-
-
- }
|