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.

87 lines
2.3KB

  1. <?php
  2. namespace Lc\ShopBundle\Manager;
  3. use Doctrine\ORM\EntityManager as DoctrineEntityManager;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Event\EntityManager\EntityManagerEvent;
  6. //use Lc\ShopBundle\IModel\EntityInterface;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. /**
  9. * class EntityManager.
  10. *
  11. * @author Simon Vieille <simon@deblan.fr>
  12. */
  13. class EntityManager
  14. {
  15. protected $eventDispatcher;
  16. protected $entityManager;
  17. public function __construct(EventDispatcherInterface $eventDispatcher, EntityManagerInterface $entityManager)
  18. {
  19. $this->eventDispatcher = $eventDispatcher;
  20. $this->entityManager = $entityManager;
  21. }
  22. public function getRepository($className)
  23. {
  24. return $this->entityManager->getRepository($this->getEntityName($className));
  25. }
  26. public function create($entity): self
  27. {
  28. $this->persist($entity);
  29. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT);
  30. return $this;
  31. }
  32. public function update($entity): self
  33. {
  34. $this->persist($entity);
  35. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT);
  36. return $this;
  37. }
  38. public function delete($entity): self
  39. {
  40. $this->remove($entity);
  41. $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT);
  42. return $this;
  43. }
  44. public function flush(): self
  45. {
  46. $this->entityManager->flush();
  47. return $this;
  48. }
  49. public function clear(): self
  50. {
  51. $this->entityManager->clear();
  52. return $this;
  53. }
  54. protected function persist($entity)
  55. {
  56. $this->entityManager->persist($entity);
  57. }
  58. public function getEntityName($className)
  59. {
  60. if (substr($className, -9) === 'Interface') {
  61. return $this->entityManager->getClassMetadata($className)->getName();
  62. }else{
  63. return $className;
  64. }
  65. }
  66. }