diff --git a/application/symfony/src/Attribut/RequestedEntityAttribut.php b/application/symfony/src/Attribut/RequestedEntityAttribut.php index eefbbff..9536428 100644 --- a/application/symfony/src/Attribut/RequestedEntityAttribut.php +++ b/application/symfony/src/Attribut/RequestedEntityAttribut.php @@ -6,6 +6,8 @@ use App\Domain\RequestManagement\Entity\RequestedEntityInterface; /** * @author kevinfrantz + * + * @see RequestedEntityAttributInterface */ trait RequestedEntityAttribut { @@ -29,4 +31,12 @@ trait RequestedEntityAttribut { $this->requestedEntity = $requestedEntity; } + + /** + * @return bool + */ + public function hasRequestedEntity(): bool + { + return isset($this->requestedEntity); + } } diff --git a/application/symfony/src/Attribut/RequestedEntityAttributInterface.php b/application/symfony/src/Attribut/RequestedEntityAttributInterface.php index d3a3c40..c92a38f 100644 --- a/application/symfony/src/Attribut/RequestedEntityAttributInterface.php +++ b/application/symfony/src/Attribut/RequestedEntityAttributInterface.php @@ -9,6 +9,11 @@ use App\Domain\RequestManagement\Entity\RequestedEntityInterface; */ interface RequestedEntityAttributInterface { + /** + * @return bool + */ + public function hasRequestedEntity(): bool; + /** * @return RequestedEntityInterface */ diff --git a/application/symfony/src/Attribut/RequestedRightAttribut.php b/application/symfony/src/Attribut/RequestedRightAttribut.php index 8cc49c9..7396e26 100644 --- a/application/symfony/src/Attribut/RequestedRightAttribut.php +++ b/application/symfony/src/Attribut/RequestedRightAttribut.php @@ -24,6 +24,14 @@ trait RequestedRightAttribut $this->requestedRight = $requestedRight; } + /** + * @return bool + */ + public function hasRequestedRight(): bool + { + return isset($this->requestedRight); + } + /** * @return RequestedRightInterface */ diff --git a/application/symfony/src/Attribut/RequestedRightAttributInterface.php b/application/symfony/src/Attribut/RequestedRightAttributInterface.php index 9d0b3d0..b29e652 100644 --- a/application/symfony/src/Attribut/RequestedRightAttributInterface.php +++ b/application/symfony/src/Attribut/RequestedRightAttributInterface.php @@ -9,6 +9,11 @@ use App\Domain\RequestManagement\Right\RequestedRightInterface; */ interface RequestedRightAttributInterface { + /** + * @return bool + */ + public function hasRequestedRight(): bool; + /** * @param RequestedRightInterface $requestedRight */ diff --git a/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntity.php b/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntity.php index fe31774..c334090 100644 --- a/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntity.php +++ b/application/symfony/src/Domain/RequestManagement/Entity/RequestedEntity.php @@ -14,6 +14,7 @@ use App\Entity\Source\AbstractSource; use App\Exception\NotSetException; use App\Repository\RepositoryInterface; use App\Entity\Source\SourceInterface; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * @author kevinfrantz @@ -53,19 +54,44 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface } /** + * @throws NotSetException + */ + private function validateHasIdentity(): void + { + 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'); + } + } + + /** + * Sorry for the messed function, but should work ;) * {@inheritdoc} * * @see \App\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity() */ public function getEntity(): EntityInterface { + $this->validateHasIdentity(); if ($this->hasSlug()) { - return $this->loadBySlug(); + $entity = $this->loadBySlug(); + } elseif ($this->hasId()) { + $entity = $this->loadById(); } - if ($this->hasId()) { - return $this->loadById(); - } - throw new NotSetException('No identity attribut like id or slug was set!'); + $this->validateLoadedEntity($entity); + + return $entity; } /** @@ -82,9 +108,9 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface /** * @throws NotCorrectInstanceException * - * @return SourceInterface + * @return SourceInterface|null */ - private function loadBySlug(): SourceInterface + private function loadBySlug(): ?SourceInterface { $repository = $this->getEntityRepository(); if ($repository instanceof SourceRepositoryInterface) { @@ -94,9 +120,9 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface } /** - * @return EntityInterface + * @return EntityInterface|null */ - private function loadById(): EntityInterface + private function loadById(): ?EntityInterface { $repository = $this->getEntityRepository(); @@ -112,7 +138,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface public function setRequestedRight($requestedRight): void { $this->requestedRight = $requestedRight; - if ($this->requestedRight !== $this) { + if (!$this->requestedRight->hasRequestedEntity()) { $this->requestedRight->setRequestedEntity($this); } } diff --git a/application/symfony/src/Domain/RequestManagement/Right/AbstractRequestedRightFacade.php b/application/symfony/src/Domain/RequestManagement/Right/AbstractRequestedRightFacade.php index 8ab41e2..4a6fae5 100644 --- a/application/symfony/src/Domain/RequestManagement/Right/AbstractRequestedRightFacade.php +++ b/application/symfony/src/Domain/RequestManagement/Right/AbstractRequestedRightFacade.php @@ -114,4 +114,14 @@ abstract class AbstractRequestedRightFacade implements RequestedRightInterface { $this->requestedRight->setReciever($reciever); } + + /** + * {@inheritdoc} + * + * @see \App\Attribut\RequestedEntityAttributInterface::hasRequestedEntity() + */ + public function hasRequestedEntity(): bool + { + $this->requestedRight->hasRequestedEntity(); + } } diff --git a/application/symfony/src/Domain/RequestManagement/Right/RequestedRight.php b/application/symfony/src/Domain/RequestManagement/Right/RequestedRight.php index b5285f6..d0c53b3 100644 --- a/application/symfony/src/Domain/RequestManagement/Right/RequestedRight.php +++ b/application/symfony/src/Domain/RequestManagement/Right/RequestedRight.php @@ -27,11 +27,6 @@ class RequestedRight implements RequestedRightInterface */ private $source; - /** - * @var RequestedEntityInterface - */ - private $requestedEntity; - /** * @throws NotCorrectInstanceException */ @@ -40,11 +35,15 @@ class RequestedRight implements RequestedRightInterface $entity = $this->requestedEntity->getEntity(); if ($entity instanceof SourceInterface) { $this->source = $entity; + + return; } if ($entity instanceof MetaInterface) { $this->source = $entity->getSource(); + + return; } - throw new NotCorrectInstanceException('The entity instance can\'t be processed'); + throw new NotCorrectInstanceException('The entity instance '.get_class($entity).' can\'t be processed'); } /** @@ -92,7 +91,7 @@ class RequestedRight implements RequestedRightInterface final public function setRequestedEntity(RequestedEntityInterface $requestedEntity): void { $this->requestedEntity = $requestedEntity; - if ($requestedEntity->getRequestedRight() !== $this) { + if (!$requestedEntity->hasRequestedRight()) { $this->requestedEntity->setRequestedRight($this); } } diff --git a/application/symfony/tests/Unit/Attribut/RightAttributTest.php b/application/symfony/tests/Unit/Attribut/RightAttributTest.php index 08f28fa..b9174d6 100644 --- a/application/symfony/tests/Unit/Attribut/RightAttributTest.php +++ b/application/symfony/tests/Unit/Attribut/RightAttributTest.php @@ -7,6 +7,9 @@ use App\Attribut\RightAttributInterface; use App\Attribut\RightAttribut; use App\Entity\Meta\RightInterface; +/** + * @author kevinfrantz + */ class RightAttributTest extends TestCase { /*** diff --git a/application/symfony/tests/Unit/Domain/RepositoryManagement/LayerRepositoryFactoryServiceTest.php b/application/symfony/tests/Unit/Domain/RepositoryManagement/LayerRepositoryFactoryServiceTest.php index b221a6c..f760768 100644 --- a/application/symfony/tests/Unit/Domain/RepositoryManagement/LayerRepositoryFactoryServiceTest.php +++ b/application/symfony/tests/Unit/Domain/RepositoryManagement/LayerRepositoryFactoryServiceTest.php @@ -7,6 +7,7 @@ use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface; use App\Domain\RepositoryManagement\LayerRepositoryFactoryService; use App\Repository\RepositoryInterface; use App\Exception\NotSetException; +use App\Domain\LayerManagement\LayerClassMap; /** * @author kevinfrantz @@ -27,7 +28,7 @@ class LayerRepositoryFactoryServiceTest extends KernelTestCase public function testGetRepository(): void { - foreach (array_keys(LayerRepositoryFactoryService::LAYER_CLASS_MAP) as $layer) { + foreach (array_keys(LayerClassMap::LAYER_CLASS_MAP) as $layer) { $repositoy = $this->layerRepositoryFactoryService->getRepository($layer); $this->assertInstanceOf(RepositoryInterface::class, $repositoy); } diff --git a/application/symfony/tests/Unit/Domain/RequestManagement/Entity/RequestedEntityTest.php b/application/symfony/tests/Unit/Domain/RequestManagement/Entity/RequestedEntityTest.php index 11dce78..c91c985 100644 --- a/application/symfony/tests/Unit/Domain/RequestManagement/Entity/RequestedEntityTest.php +++ b/application/symfony/tests/Unit/Domain/RequestManagement/Entity/RequestedEntityTest.php @@ -4,6 +4,7 @@ namespace tests\Unit\Domain\RequestManagement\Entity; use PHPUnit\Framework\TestCase; use App\Domain\RequestManagement\Entity\RequestedEntity; +use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface; /** * @author kevinfrantz @@ -12,7 +13,8 @@ class RequestedEntityTest extends TestCase { public function testSetByIdentity(): void { - $requestedEntity = new RequestedEntity(); + $layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class); + $requestedEntity = new RequestedEntity($layerRepositoryFactoryService); $slug = 'test'; $requestedEntity->setIdentity($slug); $this->assertEquals($slug, $requestedEntity->getSlug()); diff --git a/application/symfony/tests/Unit/Domain/RequestManagement/Right/RequestedRightTest.php b/application/symfony/tests/Unit/Domain/RequestManagement/Right/RequestedRightTest.php index fe5b40c..54654a1 100644 --- a/application/symfony/tests/Unit/Domain/RequestManagement/Right/RequestedRightTest.php +++ b/application/symfony/tests/Unit/Domain/RequestManagement/Right/RequestedRightTest.php @@ -5,13 +5,15 @@ namespace tests\Unit\Domain\RequestManagement\Entity; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use App\Domain\RequestManagement\Right\RequestedRightInterface; use App\Domain\RequestManagement\Right\RequestedRight; -use App\Entity\Source\AbstractSource; use App\DBAL\Types\Meta\Right\LayerType; use App\Domain\RequestManagement\Entity\RequestedEntity; use App\DBAL\Types\SystemSlugType; use App\Domain\RequestManagement\Entity\RequestedEntityInterface; use App\Exception\PreconditionFailedException; -use App\Exception\NotSetException; +use App\Entity\Source\PureSource; +use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface; +use App\Domain\RepositoryManagement\LayerRepositoryFactoryService; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * @author kevinfrantz @@ -23,12 +25,17 @@ class RequestedRightTest extends KernelTestCase */ private $requestedRight; + /** + * @var LayerRepositoryFactoryServiceInterface + */ + private $layerRepositoryFactoryService; + public function setUp(): void { self::bootKernel(); $entityManager = self::$container->get('doctrine.orm.default_entity_manager'); - $sourceRepository = $entityManager->getRepository(AbstractSource::class); - $this->requestedRight = new RequestedRight($sourceRepository); + $this->layerRepositoryFactoryService = new LayerRepositoryFactoryService($entityManager); + $this->requestedRight = new RequestedRight(); } public function testLayer(): void @@ -54,24 +61,27 @@ class RequestedRightTest extends KernelTestCase public function testKnownSource(): void { - $requestedSource = new RequestedEntity(); - $requestedSource->setSlug(SystemSlugType::IMPRINT); - $this->requestedRight->setRequestedEntity($requestedSource); + $requestedEntity = new RequestedEntity($this->layerRepositoryFactoryService); + $requestedEntity->setSlug(SystemSlugType::IMPRINT); + $this->requestedRight->setRequestedEntity($requestedEntity); + $this->requestedRight->setLayer(LayerType::SOURCE); $sourceResponse1 = $this->requestedRight->getSource(); $this->assertGreaterThan(0, $sourceResponse1->getId()); - $requestedSource->setSlug(''); - $this->expectException(NotSetException::class); + $requestedEntity->setSlug(''); + $this->expectException(NotFoundHttpException::class); $this->requestedRight->getSource(); } public function testEqualsSlug(): void { $slug = SystemSlugType::IMPRINT; - $requestedSource = $this->createMock(RequestedEntityInterface::class); - $requestedSource->method('getSlug')->willReturn($slug); - $requestedSource->method('hasSlug')->willReturn(true); - $this->assertEquals($slug, $requestedSource->getSlug()); - $this->requestedRight->setRequestedEntity($requestedSource); + $requestedEntityEntity = new PureSource(); + $requestedEntity = $this->createMock(RequestedEntityInterface::class); + $requestedEntity->method('getSlug')->willReturn($slug); + $requestedEntity->method('hasSlug')->willReturn(true); + $requestedEntity->method('getEntity')->willReturn($requestedEntityEntity); + $this->assertEquals($slug, $requestedEntity->getSlug()); + $this->requestedRight->setRequestedEntity($requestedEntity); $responseSource1 = $this->requestedRight->getSource(); $responseSource2 = $this->requestedRight->getSource(); $this->assertEquals($responseSource1, $responseSource2); diff --git a/application/symfony/tests/Unit/Domain/RequestManagement/User/RequestedUserRightTest.php b/application/symfony/tests/Unit/Domain/RequestManagement/User/RequestedUserRightTest.php index 90c0ff8..b09b38b 100644 --- a/application/symfony/tests/Unit/Domain/RequestManagement/User/RequestedUserRightTest.php +++ b/application/symfony/tests/Unit/Domain/RequestManagement/User/RequestedUserRightTest.php @@ -15,8 +15,8 @@ use App\Domain\UserManagement\UserSourceDirector; use App\Repository\Source\SourceRepositoryInterface; use App\Domain\RequestManagement\Entity\RequestedEntityInterface; use App\DBAL\Types\SystemSlugType; -use App\Exception\NotSetException; use App\Exception\SetNotPossibleException; +use App\Exception\NotCorrectInstanceException; /** * @author kevinfrantz @@ -61,7 +61,7 @@ class RequestedUserTest extends TestCase $requestedSource->method('getSlug')->willReturn(SystemSlugType::IMPRINT); $requestedSource->method('hasSlug')->willReturn(true); $sourceRepository = $this->createMock(SourceRepositoryInterface::class); - $requestedRight = new RequestedRight($sourceRepository); + $requestedRight = new RequestedRight(); $user = $this->createMock(User::class); $userSourceDirector = new UserSourceDirector($sourceRepository, $user); $requestedUserRightFacade = new RequestedUser($userSourceDirector, $requestedRight); @@ -70,7 +70,7 @@ class RequestedUserTest extends TestCase $this->assertNull($requestedUserRightFacade->setRequestedEntity($requestedSource)); $this->assertEquals($layer, $requestedRight->getLayer()); $this->assertEquals($type, $requestedRight->getCrud()); - $this->expectException(NotSetException::class); + $this->expectException(NotCorrectInstanceException::class); $this->assertNotInstanceOf(RequestedEntityInterface::class, $requestedRight->getSource()); }