Implemened class attribut in requested entity

This commit is contained in:
Kevin Frantz 2019-02-03 13:40:13 +01:00
parent 11d8ac9927
commit 4ac7b3a19b
3 changed files with 136 additions and 50 deletions

View File

@ -1,5 +1,4 @@
<?php
namespace App\Domain\RequestManagement\Entity;
use App\Entity\AbstractEntity;
@ -15,86 +14,100 @@ use App\Exception\NotSetException;
use App\Repository\RepositoryInterface;
use App\Entity\Source\SourceInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Attribut\ClassAttribut;
use App\Exception\AllreadyDefinedException;
use App\Domain\RequestManagement\Right\RequestedRightInterface;
use App\Exception\NotDefinedException;
/**
*
* @author kevinfrantz
*/
class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
{
use SlugAttribut, RequestedRightAttribut;
use SlugAttribut,
RequestedRightAttribut,
ClassAttribut{ setClass as private setClassTrait; getClass as private getClassTrait;}
/**
*
* @var LayerRepositoryFactoryServiceInterface
*/
private $layerRepositoryFactoryService;
/**
* @param EntityManagerInterface $entityManager
*/
public function __construct(LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService)
{
$this->layerRepositoryFactoryService = $layerRepositoryFactoryService;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity()
* @return bool True if an identity attribut is defined
*/
public function setIdentity($identity): void
private function hasIdentity(): bool
{
if (is_numeric($identity)) {
$this->setId($identity);
$this->slug = null;
return;
}
$this->setSlug($identity);
$this->id = null;
return $this->hasId() || $this->hasSlug();
}
/**
*
* @throws NotSetException
*/
private function validateHasIdentity(): void
{
if (!($this->hasId() || $this->hasSlug())) {
if (! ($this->hasId() || $this->hasSlug())) {
throw new NotSetException('No identity attribut like id or slug was set!');
}
}
/**
*
* @param EntityInterface|null $entity
*
* @throws NotFoundHttpException
*/
private function validateLoadedEntity(?EntityInterface $entity): void
{
if (!$entity) {
throw new NotFoundHttpException('Entity with {id:"'.$this->id.'",slug:"'.$this->slug.'"} not found');
if (! $entity) {
throw new NotFoundHttpException('Entity with {id:"' . $this->id . '",slug:"' . $this->slug . '"} not found');
}
}
/**
* Sorry for the messed function, but should work ;)
* {@inheritdoc}
*
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
* @return EntityInterface|SourceInterface|null
*/
public function getEntity(): EntityInterface
private function loadEntityBySlugOrId(): ?EntityInterface
{
$this->validateHasIdentity();
if ($this->hasSlug()) {
$entity = $this->loadBySlug();
} elseif ($this->hasId()) {
$entity = $this->loadById();
return $this->loadBySlug();
}
$this->validateLoadedEntity($entity);
return $entity;
return $this->loadById();
}
/**
*
* @throws NotCorrectInstanceException
*
* @return SourceInterface|null
*/
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|null
*/
private function loadById(): ?EntityInterface
{
$repository = $this->getEntityRepository();
return $repository->find($this->id);
}
/**
*
* @return RepositoryInterface
*/
private function getEntityRepository(): RepositoryInterface
@ -106,40 +119,91 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
}
/**
* @throws NotCorrectInstanceException
*
* @return SourceInterface|null
* @param EntityManagerInterface $entityManager
*/
private function loadBySlug(): ?SourceInterface
public function __construct(LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService)
{
$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);
$this->layerRepositoryFactoryService = $layerRepositoryFactoryService;
}
/**
* @return EntityInterface|null
*
* {@inheritdoc}
*
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity()
*/
private function loadById(): ?EntityInterface
public function setIdentity($identity): void
{
$repository = $this->getEntityRepository();
if ($this->hasClass()) {
throw new AllreadyDefinedException('A identity can\'t be set if a class is allready defined!');
}
if (is_numeric($identity)) {
$this->setId($identity);
$this->slug = null;
return $repository->find($this->id);
return;
}
$this->setSlug($identity);
$this->id = null;
}
/**
*
* {@inheritdoc}
*
* @see \App\Attribut\ClassAttributInterface::setClass()
*/
public function setClass(string $class): void
{
if ($this->hasIdentity()) {
throw new AllreadyDefinedException('A class can\'t be manual defined, if an identity is allready set!');
}
$this->setClassTrait($class);
}
/**
*
* {@inheritdoc}
*
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
*/
public function getEntity(): EntityInterface
{
$this->validateHasIdentity();
$entity = $this->loadEntityBySlugOrId();
$this->validateLoadedEntity($entity);
return $entity;
}
/**
* Overriding is neccessary to declare the correct relation
*
* {@inheritdoc}
*
* @see \App\Attribut\RequestedRightAttributInterface::setRequestedRight()
*/
public function setRequestedRight($requestedRight): void
public function setRequestedRight(RequestedRightInterface $requestedRight): void
{
$this->requestedRight = $requestedRight;
if (!$this->requestedRight->hasRequestedEntity()) {
if (! $this->requestedRight->hasRequestedEntity()) {
$this->requestedRight->setRequestedEntity($this);
}
}
/**
*
* {@inheritdoc}
*
* @see \App\Attribut\ClassAttributInterface::getClass()
*/
public function getClass(): string
{
if ($this->hasClass()) {
return $this->getClassTrait();
}
return get_class($this->getEntity());
}
}

View File

@ -5,13 +5,14 @@ namespace App\Domain\RequestManagement\Entity;
use App\Entity\EntityInterface;
use App\Attribut\SlugAttributInterface;
use App\Attribut\RequestedRightAttributInterface;
use App\Attribut\ClassAttributInterface;
/**
* A requested entity containes the stumb attributes to load an entity.
*
* @author kevinfrantz
*/
interface RequestedEntityInterface extends EntityInterface, SlugAttributInterface, RequestedRightAttributInterface
interface RequestedEntityInterface extends EntityInterface, SlugAttributInterface, RequestedRightAttributInterface, ClassAttributInterface
{
/**
* Sets the slug or the id.

View File

@ -11,6 +11,8 @@ use App\Domain\RequestManagement\Right\RequestedRightInterface;
use App\DBAL\Types\Meta\Right\LayerType;
use App\Repository\RepositoryInterface;
use App\Entity\EntityInterface;
use App\Entity\Source\AbstractSource;
use App\Exception\AllreadyDefinedException;
/**
* @author kevinfrantz
@ -66,5 +68,24 @@ class RequestedEntityTest extends TestCase
$requestedEntity->setRequestedRight($requestedRight);
$entityResult = $requestedEntity->getEntity();
$this->assertEquals($entityMock, $entityResult);
$this->assertEquals(get_class($entityMock), $requestedEntity->getClass());
$this->expectException(AllreadyDefinedException::class);
$requestedEntity->setClass(AbstractSource::class);
}
public function testSetClass():void{
$class = AbstractSource::class;
$entityMock = $this->createMock(EntityInterface::class);
$repository = $this->createMock(RepositoryInterface::class);
$repository->method('find')->willReturn($entityMock);
$layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class);
$layerRepositoryFactoryService->method('getRepository')->willReturn($repository);
$requestedEntity = new RequestedEntity($layerRepositoryFactoryService);
$this->assertFalse($requestedEntity->hasClass());
$this->assertNull($requestedEntity->setClass($class));
$this->assertTrue($requestedEntity->hasClass());
$this->assertEquals($class, $requestedEntity->getClass());
$this->expectException(AllreadyDefinedException::class);
$requestedEntity->setIdentity('123343');
}
}