src/App/Manager/AbstractEntityManager.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Traits\Autowired\Vendor\ApiSearchTrait;
  4. use App\Traits\Autowired\Vendor\ManagerRegistryTrait;
  5. use Doctrine\Persistence\ObjectManager;
  6. use Doctrine\Persistence\ObjectRepository;
  7. use GollumSF\RestBundle\Model\ApiList;
  8. use App\Traits\Autowired\Vendor\EventDispatcherTrait;
  9. use App\Traits\Autowired\Vendor\LoggerTrait;
  10. use App\Traits\UserTrait;
  11. abstract class AbstractEntityManager {
  12.     
  13.     use UserTrait;
  14.     use EventDispatcherTrait;
  15.     use LoggerTrait;
  16.     use ManagerRegistryTrait;
  17.     use ApiSearchTrait;
  18.     
  19.     /**
  20.      * @return string
  21.      */
  22.     public function getEntityClass() {
  23.         $className get_class($this);
  24.         $namespace substr($className0strrpos($className'\\'));
  25.         
  26.         $entityNS    substr($namespace0, -strlen('\Manager')).'\\Entity';
  27.         $entityClass =  substr(str_replace($namespace''$className), 0, -strlen('Manager'));
  28.         
  29.         return $entityNS.$entityClass;
  30.     }
  31.     /**
  32.      * @return ObjectManager
  33.      */
  34.     public function getEntityManager() {
  35.         return $this->managerRegistry->getManagerForClass($this->getEntityClass());
  36.     }
  37.     
  38.     /**
  39.      * @return ObjectRepository
  40.      */
  41.     public function getRepository() {
  42.         $className $this->getEntityClass();
  43.         $em $this->getEntityManager($className);
  44.         return $em $em->getRepository($className) : null;
  45.     }
  46.     
  47.     /**
  48.      * @param mixed $entity
  49.      * @return mixed
  50.      */
  51.     public function delete($entity) {
  52.         $em $this->getEntityManager();
  53.         if (is_array($entity)) {
  54.             foreach ($entity as $e) {
  55.                 if (is_object($e)) {
  56.                     $em $this->getEntityManager();
  57.                     $em->remove($e);
  58.                 }
  59.             }
  60.             $em->flush();
  61.         } else {
  62.             if (is_object($entity)) {
  63.                 $em->remove($entity);
  64.                 $em->flush($entity);
  65.             }
  66.         }
  67.         return $entity;
  68.     }
  69.     /**
  70.      * @param mixed $entity
  71.      * @return mixed
  72.      */
  73.     public function update($entity) {
  74.         if (is_object($entity)) {
  75.             $em $this->getEntityManager();
  76.             $em->persist($entity);
  77.             $em->flush($entity);
  78.         }
  79.         return $entity;
  80.     }
  81.     
  82.     /**
  83.      * @param $id
  84.      * @return null|mixed
  85.      */
  86.     public function find($id) {
  87.         return  $this->getRepository()->find($id);
  88.     }
  89.     /**
  90.      * @return object[]
  91.      */
  92.     public function findAll() {
  93.         return  $this->getRepository()->findAll();
  94.     }
  95.     
  96.     /**
  97.      * @param array $criteria
  98.      * @param array|null $orderBy
  99.      * @param null $limit
  100.      * @param null $offset
  101.      * @return array
  102.      */
  103.     public function findBy(array $criteria, array $orderBy null$limit null$offset null) {
  104.         return  $this->getRepository()->findBy($criteria$orderBy$limit$offset);
  105.     }
  106.     
  107.     /**
  108.      * @param array $criteria
  109.      * @return null|object
  110.      */
  111.     public function findOneBy(array $criteria) {
  112.         return  $this->getRepository()->findOneBy($criteria);
  113.     }
  114.     
  115.     public function apiFindBy(\Closure $queryCallback null): ApiList {
  116.         return $this->apiSearch->apiFindBy($this->getEntityClass(), $queryCallback);
  117.     }
  118. }