No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

72 líneas
2.3KB

  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
  5. use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface;
  6. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  7. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  8. use Lc\CaracoleBundle\Resolver\SectionResolver;
  9. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class MerchantSectionPropertyEventSubscriber implements EventSubscriberInterface
  12. {
  13. protected $em;
  14. protected $merchantResolver;
  15. protected $sectionResolver;
  16. public function __construct(EntityManagerInterface $entityManager, MerchantResolver $merchantResolver, SectionResolver $sectionResolver)
  17. {
  18. $this->em = $entityManager;
  19. $this->merchantResolver = $merchantResolver;
  20. $this->sectionResolver = $sectionResolver;
  21. }
  22. public static function getSubscribedEvents()
  23. {
  24. return [
  25. EntityManagerEvent::PRE_CREATE_EVENT => ['setMerchantAndSectionProperties'],
  26. ];
  27. }
  28. public function setMerchantAndSectionProperties(EntityManagerEvent $event)
  29. {
  30. // @TODO : toujours d'actualité ?
  31. /*$entity = $event->getEntity();
  32. $entityRepository = $this->em->getRepository(get_class($entity));
  33. if ($entity instanceof FilterSectionInterface) {
  34. $this->setSectionProperty($entity, $entityRepository);
  35. }
  36. if ($entity instanceof FilterMerchantInterface) {
  37. $this->setMerchantProperty($entity, $entityRepository);
  38. }
  39. if ($entity instanceof FilterMultipleMerchantsInterface) {
  40. $this->setMultipleMerchantProperty($entity, $entityRepository);
  41. }*/
  42. }
  43. private function setSectionProperty($entity)
  44. {
  45. $entity->setSection($this->sectionResolver->getCurrent());
  46. }
  47. private function setMerchantProperty($entity)
  48. {
  49. $entity->setMerchant($this->merchantResolver->getCurrent());
  50. }
  51. private function setMultipleMerchantProperty($entity)
  52. {
  53. if ($entity->getMerchants()->isEmpty()) {
  54. $entity->addMerchant($this->merchantResolver->getCurrent());
  55. }
  56. }
  57. }