Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

EntityManager.php 3.7KB

pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Lc\ShopBundle\Manager;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\UserInterface;
  5. use Lc\ShopBundle\Event\EntityManager\EntityManagerEvent;
  6. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  7. use Symfony\Component\Security\Core\Security;
  8. /**
  9. * class EntityManager.
  10. *
  11. * @author Simon Vieille <simon@deblan.fr>
  12. */
  13. class EntityManager
  14. {
  15. protected $eventDispatcher;
  16. protected $entityManager;
  17. protected $security;
  18. protected $userSystem = null;
  19. public function __construct(EventDispatcherInterface $eventDispatcher, EntityManagerInterface $entityManager,
  20. Security $security)
  21. {
  22. $this->eventDispatcher = $eventDispatcher;
  23. $this->entityManager = $entityManager;
  24. $this->security = $security;
  25. }
  26. public function getUserSystem()
  27. {
  28. if ($this->userSystem === null) {
  29. $this->userSystem = $this->getRepository(UserInterface::class)->findOneByDevAlias('system');
  30. }
  31. return $this->userSystem;
  32. }
  33. public function getRepository($className)
  34. {
  35. return $this->entityManager->getRepository($this->getEntityName($className));
  36. }
  37. public function create($entity): self
  38. {
  39. if ($this->security->getUser() === null) {
  40. if(method_exists($entity, 'setUpdatedBy')) {
  41. $entity->setUpdatedBy($this->getUserSystem());
  42. }
  43. if(method_exists($entity, 'setCreatedBy')) {
  44. $entity->setCreatedBy($this->getUserSystem());
  45. }
  46. }
  47. $this->persist($entity);
  48. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT);
  49. return $this;
  50. }
  51. public function update($entity): self
  52. {
  53. if ($this->security->getUser() === null) {
  54. $entity->setUpdatedBy($this->getUserSystem());
  55. }
  56. $this->persist($entity);
  57. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT);
  58. return $this;
  59. }
  60. public function delete($entity): self
  61. {
  62. $this->remove($entity);
  63. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT);
  64. return $this;
  65. }
  66. public function remove($entity): self
  67. {
  68. $this->entityManager->remove($entity) ;
  69. return $this ;
  70. }
  71. public function flush(): self
  72. {
  73. $this->entityManager->flush();
  74. return $this;
  75. }
  76. public function clear(): self
  77. {
  78. $this->entityManager->clear();
  79. return $this;
  80. }
  81. public function persist($entity)
  82. {
  83. $this->entityManager->persist($entity);
  84. }
  85. public function refresh($entity)
  86. {
  87. $this->entityManager->refresh($entity);
  88. }
  89. public function getClassMetadata($className)
  90. {
  91. return $this->entityManager->getClassMetadata($className) ;
  92. }
  93. public function getEntityName($className)
  94. {
  95. if (substr($className, -9) === 'Interface') {
  96. return $this->entityManager->getClassMetadata($className)->getName();
  97. }
  98. else {
  99. return $className;
  100. }
  101. }
  102. }