|
- <?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 MerchantSectionPropertyEventSubscriber 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::PRE_CREATE_EVENT => ['setMerchantAndSectionProperties'],
- ];
- }
-
- public function setMerchantAndSectionProperties(EntityManagerEvent $event)
- {
- // @TODO : toujours d'actualité ?
- /*$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());
- }
- }
-
-
- }
|