You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MerchantSectionPropertyEventSubscriber.php 2.2KB

преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. $entity = $event->getEntity();
  31. $entityRepository = $this->em->getRepository(get_class($entity));
  32. if ($entity instanceof FilterSectionInterface) {
  33. $this->setSectionProperty($entity, $entityRepository);
  34. }
  35. if ($entity instanceof FilterMerchantInterface) {
  36. $this->setMerchantProperty($entity, $entityRepository);
  37. }
  38. if ($entity instanceof FilterMultipleMerchantsInterface) {
  39. $this->setMultipleMerchantProperty($entity, $entityRepository);
  40. }
  41. }
  42. private function setSectionProperty($entity)
  43. {
  44. $entity->setSection($this->sectionResolver->getCurrent());
  45. }
  46. private function setMerchantProperty($entity)
  47. {
  48. $entity->setMerchant($this->merchantResolver->getCurrent());
  49. }
  50. private function setMultipleMerchantProperty($entity)
  51. {
  52. if ($entity->getMerchants()->isEmpty()) {
  53. $entity->addMerchant($this->merchantResolver->getCurrent());
  54. }
  55. }
  56. }