diff --git a/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntity.php b/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntity.php index 63ea10f..203e5b5 100644 --- a/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntity.php +++ b/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntity.php @@ -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); + } } diff --git a/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntityInterface.php b/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntityInterface.php index 63bde68..c74f9ed 100644 --- a/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntityInterface.php +++ b/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntityInterface.php @@ -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; }