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 <?php
namespace App\Domain\RequestManagement\Entity; namespace App\Domain\RequestManagement\Entity;
use App\Entity\AbstractEntity; use App\Entity\AbstractEntity;
@ -15,45 +14,38 @@ use App\Exception\NotSetException;
use App\Repository\RepositoryInterface; use App\Repository\RepositoryInterface;
use App\Entity\Source\SourceInterface; use App\Entity\Source\SourceInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 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 * @author kevinfrantz
*/ */
class RequestedEntity extends AbstractEntity implements RequestedEntityInterface class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
{ {
use SlugAttribut, RequestedRightAttribut; use SlugAttribut,
RequestedRightAttribut,
ClassAttribut{ setClass as private setClassTrait; getClass as private getClassTrait;}
/** /**
*
* @var LayerRepositoryFactoryServiceInterface * @var LayerRepositoryFactoryServiceInterface
*/ */
private $layerRepositoryFactoryService; 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)) { return $this->hasId() || $this->hasSlug();
$this->setId($identity);
$this->slug = null;
return;
}
$this->setSlug($identity);
$this->id = null;
} }
/** /**
*
* @throws NotSetException * @throws NotSetException
*/ */
private function validateHasIdentity(): void private function validateHasIdentity(): void
@ -64,6 +56,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
*
* @param EntityInterface|null $entity * @param EntityInterface|null $entity
* *
* @throws NotFoundHttpException * @throws NotFoundHttpException
@ -76,36 +69,19 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
* 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()) { if ($this->hasSlug()) {
$entity = $this->loadBySlug(); return $this->loadBySlug();
} elseif ($this->hasId()) {
$entity = $this->loadById();
} }
$this->validateLoadedEntity($entity); return $this->loadById();
return $entity;
}
/**
* @return RepositoryInterface
*/
private function getEntityRepository(): RepositoryInterface
{
$layer = $this->requestedRight->getLayer();
$repository = $this->layerRepositoryFactoryService->getRepository($layer);
return $repository;
} }
/** /**
*
* @throws NotCorrectInstanceException * @throws NotCorrectInstanceException
* *
* @return SourceInterface|null * @return SourceInterface|null
@ -120,6 +96,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
*
* @return EntityInterface|null * @return EntityInterface|null
*/ */
private function loadById(): ?EntityInterface private function loadById(): ?EntityInterface
@ -129,17 +106,104 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
return $repository->find($this->id); return $repository->find($this->id);
} }
/**
*
* @return RepositoryInterface
*/
private function getEntityRepository(): RepositoryInterface
{
$layer = $this->requestedRight->getLayer();
$repository = $this->layerRepositoryFactoryService->getRepository($layer);
return $repository;
}
/**
*
* @param EntityManagerInterface $entityManager
*/
public function __construct(LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService)
{
$this->layerRepositoryFactoryService = $layerRepositoryFactoryService;
}
/**
*
* {@inheritdoc}
*
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity()
*/
public function setIdentity($identity): void
{
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;
}
$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 * Overriding is neccessary to declare the correct relation
*
* {@inheritdoc} * {@inheritdoc}
* *
* @see \App\Attribut\RequestedRightAttributInterface::setRequestedRight() * @see \App\Attribut\RequestedRightAttributInterface::setRequestedRight()
*/ */
public function setRequestedRight($requestedRight): void public function setRequestedRight(RequestedRightInterface $requestedRight): void
{ {
$this->requestedRight = $requestedRight; $this->requestedRight = $requestedRight;
if (! $this->requestedRight->hasRequestedEntity()) { if (! $this->requestedRight->hasRequestedEntity()) {
$this->requestedRight->setRequestedEntity($this); $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\Entity\EntityInterface;
use App\Attribut\SlugAttributInterface; use App\Attribut\SlugAttributInterface;
use App\Attribut\RequestedRightAttributInterface; use App\Attribut\RequestedRightAttributInterface;
use App\Attribut\ClassAttributInterface;
/** /**
* 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, RequestedRightAttributInterface interface RequestedEntityInterface extends EntityInterface, SlugAttributInterface, RequestedRightAttributInterface, ClassAttributInterface
{ {
/** /**
* Sets the slug or the id. * 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\DBAL\Types\Meta\Right\LayerType;
use App\Repository\RepositoryInterface; use App\Repository\RepositoryInterface;
use App\Entity\EntityInterface; use App\Entity\EntityInterface;
use App\Entity\Source\AbstractSource;
use App\Exception\AllreadyDefinedException;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -66,5 +68,24 @@ class RequestedEntityTest extends TestCase
$requestedEntity->setRequestedRight($requestedRight); $requestedEntity->setRequestedRight($requestedRight);
$entityResult = $requestedEntity->getEntity(); $entityResult = $requestedEntity->getEntity();
$this->assertEquals($entityMock, $entityResult); $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');
} }
} }