<?php
namespace App\Manager;
use App\Traits\Autowired\Vendor\ApiSearchTrait;
use App\Traits\Autowired\Vendor\ManagerRegistryTrait;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use GollumSF\RestBundle\Model\ApiList;
use App\Traits\Autowired\Vendor\EventDispatcherTrait;
use App\Traits\Autowired\Vendor\LoggerTrait;
use App\Traits\UserTrait;
abstract class AbstractEntityManager {
use UserTrait;
use EventDispatcherTrait;
use LoggerTrait;
use ManagerRegistryTrait;
use ApiSearchTrait;
/**
* @return string
*/
public function getEntityClass() {
$className = get_class($this);
$namespace = substr($className, 0, strrpos($className, '\\'));
$entityNS = substr($namespace, 0, -strlen('\Manager')).'\\Entity';
$entityClass = substr(str_replace($namespace, '', $className), 0, -strlen('Manager'));
return $entityNS.$entityClass;
}
/**
* @return ObjectManager
*/
public function getEntityManager() {
return $this->managerRegistry->getManagerForClass($this->getEntityClass());
}
/**
* @return ObjectRepository
*/
public function getRepository() {
$className = $this->getEntityClass();
$em = $this->getEntityManager($className);
return $em ? $em->getRepository($className) : null;
}
/**
* @param mixed $entity
* @return mixed
*/
public function delete($entity) {
$em = $this->getEntityManager();
if (is_array($entity)) {
foreach ($entity as $e) {
if (is_object($e)) {
$em = $this->getEntityManager();
$em->remove($e);
}
}
$em->flush();
} else {
if (is_object($entity)) {
$em->remove($entity);
$em->flush($entity);
}
}
return $entity;
}
/**
* @param mixed $entity
* @return mixed
*/
public function update($entity) {
if (is_object($entity)) {
$em = $this->getEntityManager();
$em->persist($entity);
$em->flush($entity);
}
return $entity;
}
/**
* @param $id
* @return null|mixed
*/
public function find($id) {
return $this->getRepository()->find($id);
}
/**
* @return object[]
*/
public function findAll() {
return $this->getRepository()->findAll();
}
/**
* @param array $criteria
* @param array|null $orderBy
* @param null $limit
* @param null $offset
* @return array
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) {
return $this->getRepository()->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* @param array $criteria
* @return null|object
*/
public function findOneBy(array $criteria) {
return $this->getRepository()->findOneBy($criteria);
}
public function apiFindBy(\Closure $queryCallback = null): ApiList {
return $this->apiSearch->apiFindBy($this->getEntityClass(), $queryCallback);
}
}