Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

EntityManager.php 3.3KB

3 år sedan
3 år sedan
3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, 'setCreadtedBy')) {
  44. $entity->setCreadtedBy($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 flush(): self
  67. {
  68. $this->entityManager->flush();
  69. return $this;
  70. }
  71. public function clear(): self
  72. {
  73. $this->entityManager->clear();
  74. return $this;
  75. }
  76. protected function persist($entity)
  77. {
  78. $this->entityManager->persist($entity);
  79. }
  80. public function getEntityName($className)
  81. {
  82. if (substr($className, -9) === 'Interface') {
  83. return $this->entityManager->getClassMetadata($className)->getName();
  84. } else {
  85. return $className;
  86. }
  87. }
  88. }