|
- <?php
-
- namespace Lc\AdminBundle\Manager;
-
- use Doctrine\ORM\EntityManager as DoctrineEntityManager;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\AdminBundle\Event\EntityManager\EntityManagerEvent;
- use Lc\AdminBundle\IModel\EntityInterface;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-
- /**
- * class EntityManager.
- *
- * @author Simon Vieille <simon@deblan.fr>
- */
- class EntityManager
- {
- protected EventDispatcherInterface $eventDispatcher;
-
- protected DoctrineEntityManager $entityManager;
-
- public function __construct(EventDispatcherInterface $eventDispatcher, EntityManagerInterface $entityManager)
- {
- $this->eventDispatcher = $eventDispatcher;
- $this->entityManager = $entityManager;
- }
-
- public function getRepository($className)
- {
- return $this->entityManager->getRepository($this->getEntityName($className));
- }
-
- public function new($className)
- {
- $entityName = $this->getEntityName($className);
- return new $entityName;
- }
-
- public function create(EntityInterface $entity): self
- {
- $this->persist($entity);
- $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT);
-
- return $this;
- }
-
- public function update(EntityInterface $entity): self
- {
- $this->persist($entity);
- $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT);
-
- return $this;
- }
-
- public function delete(EntityInterface $entity): self
- {
- $this->remove($entity);
- $this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT);
-
- return $this;
- }
-
- public function flush(): self
- {
- $this->entityManager->flush();
-
- return $this;
- }
-
- public function clear(): self
- {
- $this->entityManager->clear();
-
- return $this;
- }
-
- public function refresh(EntityInterface $entity): self
- {
- $this->entityManager->refresh($entity);
-
- return $this;
- }
-
- protected function persist(EntityInterface $entity)
- {
- $this->entityManager->persist($entity);
- }
-
- public function getClassMetadata($className){
- return $this->entityManager->getClassMetadata($className);
- }
-
- public function getEntityName($className)
- {
- if (substr($className, -9) === 'Interface') {
- return $this->entityManager->getClassMetadata($className)->getName();
- }else{
- return $className;
- }
-
- }
- }
|