Declared function hasIdentity public

This commit is contained in:
Kevin Frantz 2019-02-03 13:57:49 +01:00
parent 4ac7b3a19b
commit c3b8e1a92d
3 changed files with 27 additions and 32 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
namespace App\Domain\RequestManagement\Entity; namespace App\Domain\RequestManagement\Entity;
use App\Entity\AbstractEntity; use App\Entity\AbstractEntity;
@ -17,59 +18,44 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Attribut\ClassAttribut; use App\Attribut\ClassAttribut;
use App\Exception\AllreadyDefinedException; use App\Exception\AllreadyDefinedException;
use App\Domain\RequestManagement\Right\RequestedRightInterface; 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, use SlugAttribut,
RequestedRightAttribut, RequestedRightAttribut,
ClassAttribut{ setClass as private setClassTrait; getClass as private getClassTrait;} ClassAttribut{ setClass as private setClassTrait; getClass as private getClassTrait; }
/** /**
*
* @var LayerRepositoryFactoryServiceInterface * @var LayerRepositoryFactoryServiceInterface
*/ */
private $layerRepositoryFactoryService; private $layerRepositoryFactoryService;
/** /**
*
* @return bool True if an identity attribut is defined
*/
private function hasIdentity(): bool
{
return $this->hasId() || $this->hasSlug();
}
/**
*
* @throws NotSetException * @throws NotSetException
*/ */
private function validateHasIdentity(): void 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!'); throw new NotSetException('No identity attribut like id or slug was set!');
} }
} }
/** /**
*
* @param EntityInterface|null $entity * @param EntityInterface|null $entity
* *
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
private function validateLoadedEntity(?EntityInterface $entity): void private function validateLoadedEntity(?EntityInterface $entity): void
{ {
if (! $entity) { if (!$entity) {
throw new NotFoundHttpException('Entity with {id:"' . $this->id . '",slug:"' . $this->slug . '"} not found'); throw new NotFoundHttpException('Entity with {id:"'.$this->id.'",slug:"'.$this->slug.'"} not found');
} }
} }
/** /**
*
* @return EntityInterface|SourceInterface|null * @return EntityInterface|SourceInterface|null
*/ */
private function loadEntityBySlugOrId(): ?EntityInterface private function loadEntityBySlugOrId(): ?EntityInterface
@ -77,11 +63,11 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
if ($this->hasSlug()) { if ($this->hasSlug()) {
return $this->loadBySlug(); return $this->loadBySlug();
} }
return $this->loadById(); return $this->loadById();
} }
/** /**
*
* @throws NotCorrectInstanceException * @throws NotCorrectInstanceException
* *
* @return SourceInterface|null * @return SourceInterface|null
@ -92,11 +78,10 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
if ($repository instanceof SourceRepositoryInterface) { if ($repository instanceof SourceRepositoryInterface) {
return $repository->findOneBySlug($this->slug); return $repository->findOneBySlug($this->slug);
} }
throw new NotCorrectInstanceException('To read an entity by slug is just allowed for entitys of type ' . AbstractSource::class); throw new NotCorrectInstanceException('To read an entity by slug is just allowed for entitys of type '.AbstractSource::class);
} }
/** /**
*
* @return EntityInterface|null * @return EntityInterface|null
*/ */
private function loadById(): ?EntityInterface private function loadById(): ?EntityInterface
@ -107,7 +92,6 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
*
* @return RepositoryInterface * @return RepositoryInterface
*/ */
private function getEntityRepository(): RepositoryInterface private function getEntityRepository(): RepositoryInterface
@ -119,7 +103,6 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
*
* @param EntityManagerInterface $entityManager * @param EntityManagerInterface $entityManager
*/ */
public function __construct(LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService) public function __construct(LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService)
@ -128,7 +111,16 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
* {@inheritdoc}
* *
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::hasIdentity()
*/
public function hasIdentity(): bool
{
return $this->hasId() || $this->hasSlug();
}
/**
* {@inheritdoc} * {@inheritdoc}
* *
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity() * @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity()
@ -149,7 +141,6 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
*
* {@inheritdoc} * {@inheritdoc}
* *
* @see \App\Attribut\ClassAttributInterface::setClass() * @see \App\Attribut\ClassAttributInterface::setClass()
@ -163,7 +154,6 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
*
* {@inheritdoc} * {@inheritdoc}
* *
* @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity() * @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
@ -178,7 +168,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
} }
/** /**
* Overriding is neccessary to declare the correct relation * Overriding is neccessary to declare the correct relation.
* *
* {@inheritdoc} * {@inheritdoc}
* *
@ -187,13 +177,12 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
public function setRequestedRight(RequestedRightInterface $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} * {@inheritdoc}
* *
* @see \App\Attribut\ClassAttributInterface::getClass() * @see \App\Attribut\ClassAttributInterface::getClass()

View File

@ -21,6 +21,11 @@ interface RequestedEntityInterface extends EntityInterface, SlugAttributInterfac
*/ */
public function setIdentity($identity): void; public function setIdentity($identity): void;
/**
* @return bool True if an identity attribut is defined
*/
public function hasIdentity(): bool;
/** /**
* @return EntityInterface * @return EntityInterface
*/ */

View File

@ -72,8 +72,9 @@ class RequestedEntityTest extends TestCase
$this->expectException(AllreadyDefinedException::class); $this->expectException(AllreadyDefinedException::class);
$requestedEntity->setClass(AbstractSource::class); $requestedEntity->setClass(AbstractSource::class);
} }
public function testSetClass():void{ public function testSetClass(): void
{
$class = AbstractSource::class; $class = AbstractSource::class;
$entityMock = $this->createMock(EntityInterface::class); $entityMock = $this->createMock(EntityInterface::class);
$repository = $this->createMock(RepositoryInterface::class); $repository = $this->createMock(RepositoryInterface::class);