mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-10 06:27:24 +01:00
Implemented LazyLoading of Entity in RequestedEntity
This commit is contained in:
parent
09944d032c
commit
edda341d59
@ -3,14 +3,37 @@
|
|||||||
namespace App\Domain\RequestManagement\Entity;
|
namespace App\Domain\RequestManagement\Entity;
|
||||||
|
|
||||||
use App\Entity\AbstractEntity;
|
use App\Entity\AbstractEntity;
|
||||||
|
use App\Entity\EntityInterface;
|
||||||
use App\Attribut\SlugAttribut;
|
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
|
* @author kevinfrantz
|
||||||
*/
|
*/
|
||||||
class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
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}
|
* {@inheritdoc}
|
||||||
@ -28,4 +51,55 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
$this->setSlug($identity);
|
$this->setSlug($identity);
|
||||||
$this->id = null;
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,14 @@ namespace App\Domain\RequestManagement\Entity;
|
|||||||
|
|
||||||
use App\Entity\EntityInterface;
|
use App\Entity\EntityInterface;
|
||||||
use App\Attribut\SlugAttributInterface;
|
use App\Attribut\SlugAttributInterface;
|
||||||
|
use App\Attribut\RequestedRightAttributInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A requested entity containes the stumb attributes to load an entity.
|
* A requested entity containes the stumb attributes to load an entity.
|
||||||
*
|
*
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
*/
|
*/
|
||||||
interface RequestedEntityInterface extends EntityInterface, SlugAttributInterface
|
interface RequestedEntityInterface extends EntityInterface, SlugAttributInterface, RequestedRightAttributInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Sets the slug or the id.
|
* Sets the slug or the id.
|
||||||
@ -18,4 +19,9 @@ interface RequestedEntityInterface extends EntityInterface, SlugAttributInterfac
|
|||||||
* @param string|int $identity
|
* @param string|int $identity
|
||||||
*/
|
*/
|
||||||
public function setIdentity($identity): void;
|
public function setIdentity($identity): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return EntityInterface
|
||||||
|
*/
|
||||||
|
public function getEntity(): EntityInterface;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user