Implemented LazyLoading of Entity in RequestedEntity

This commit is contained in:
Kevin Frantz 2019-01-26 17:30:53 +01:00
parent 09944d032c
commit edda341d59
2 changed files with 82 additions and 2 deletions

View File

@ -3,14 +3,37 @@
namespace App\Domain\RequestManagement\Entity;
use App\Entity\AbstractEntity;
use App\Entity\EntityInterface;
use App\Attribut\SlugAttribut;
use Doctrine\ORM\EntityManagerInterface;
use App\Attribut\RequestedRightAttribut;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use App\Repository\Source\SourceRepositoryInterface;
use App\Exception\NotCorrectInstanceException;
use App\Entity\Source\AbstractSource;
use App\Exception\NotSetException;
use App\Repository\RepositoryInterface;
use App\Entity\Source\SourceInterface;
/**
* @author kevinfrantz
*/
class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
{
use SlugAttribut;
use SlugAttribut, RequestedRightAttribut;
/**
* @var LayerRepositoryFactoryServiceInterface
*/
private $layerRepositoryFactoryService;
/**
* @param EntityManagerInterface $entityManager
*/
public function __construct(LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService)
{
$this->layerRepositoryFactoryService = $layerRepositoryFactoryService;
}
/**
* {@inheritdoc}
@ -28,4 +51,55 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
$this->setSlug($identity);
$this->id = null;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
*/
public function getEntity(): EntityInterface
{
if ($this->hasSlug()) {
return $this->loadBySlug();
}
if ($this->hasId()) {
return $this->loadById();
}
throw new NotSetException('No identity attribut like id or slug was set!');
}
/**
* @return RepositoryInterface
*/
private function getEntityRepository(): RepositoryInterface
{
$layer = $this->requestedRight->getLayer();
$repository = $this->layerRepositoryFactoryService->getRepository($layer);
return $repository;
}
/**
* @throws NotCorrectInstanceException
*
* @return SourceInterface
*/
private function loadBySlug(): SourceInterface
{
$repository = $this->getEntityRepository();
if ($repository instanceof SourceRepositoryInterface) {
return $repository->findOneBySlug($this->slug);
}
throw new NotCorrectInstanceException('To read an entity by slug is just allowed for entitys of type '.AbstractSource::class);
}
/**
* @return EntityInterface
*/
private function loadById(): EntityInterface
{
$repository = $this->getEntityRepository();
return $repository->find($this->id);
}
}

View File

@ -4,13 +4,14 @@ namespace App\Domain\RequestManagement\Entity;
use App\Entity\EntityInterface;
use App\Attribut\SlugAttributInterface;
use App\Attribut\RequestedRightAttributInterface;
/**
* A requested entity containes the stumb attributes to load an entity.
*
* @author kevinfrantz
*/
interface RequestedEntityInterface extends EntityInterface, SlugAttributInterface
interface RequestedEntityInterface extends EntityInterface, SlugAttributInterface, RequestedRightAttributInterface
{
/**
* Sets the slug or the id.
@ -18,4 +19,9 @@ interface RequestedEntityInterface extends EntityInterface, SlugAttributInterfac
* @param string|int $identity
*/
public function setIdentity($identity): void;
/**
* @return EntityInterface
*/
public function getEntity(): EntityInterface;
}