From ee4dc0d05299fa6f427137caba8424b3303c73af Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Mon, 14 Jan 2019 21:27:06 +0100 Subject: [PATCH] Optimized draft for requested source --- .../RightRequestManagement/RequestedRight.php | 57 ++++++------------- .../Exception/PreconditionFailedException.php | 10 ++++ .../RequestedRightTest.php | 29 +++++++--- 3 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 application/symfony/src/Exception/PreconditionFailedException.php diff --git a/application/symfony/src/Domain/RightManagement/RightRequestManagement/RequestedRight.php b/application/symfony/src/Domain/RightManagement/RightRequestManagement/RequestedRight.php index ec324c7..6474465 100644 --- a/application/symfony/src/Domain/RightManagement/RightRequestManagement/RequestedRight.php +++ b/application/symfony/src/Domain/RightManagement/RightRequestManagement/RequestedRight.php @@ -9,9 +9,13 @@ use App\Entity\Source\SourceInterface; use App\Entity\Attribut\TypeAttribut; use App\Entity\Attribut\LayerAttribut; use App\Entity\Attribut\RecieverAttribut; +use App\Exception\PreconditionFailedException; +use App\Exception\NotSetException; /** * @author kevinfrantz + * + * @todo Check out if the performance of this class can be optimized! */ class RequestedRight implements RequestedRightInterface { @@ -46,43 +50,14 @@ class RequestedRight implements RequestedRightInterface } /** - * @return bool + * @throws PreconditionFailedException If the source has no id or slug */ - private function isIdEquals(): bool + private function validateRequestedSource(): void { - if (!$this->requestedSource->hasId() || !$this->source->hasId()) { - return false; + if ($this->requestedSource->hasSlug() || $this->requestedSource->hasId()) { + return; } - - return $this->requestedSource->getId() === $this->source->getId(); - } - - /** - * @return bool - */ - private function isSlugEquals(): bool - { - if (!$this->requestedSource->hasSlug() || !$this->source->hasSlug()) { - return false; - } - - return $this->requestedSource->getSlug() === $this->source->getSlug(); - } - - /** - * @return bool Returns true if the source is not set! - */ - private function isSourceNotSet(): bool - { - return !isset($this->source); - } - - /** - * @return bool Tells if a reload of the source is neccessary - */ - private function isReloadNeccessary(): bool - { - return $this->isSourceNotSet() || $this->isIdEquals() || $this->isSlugEquals(); + throw new PreconditionFailedException(get_class($this->requestedSource).' needs to have a defined attribut id or slug!'); } /** @@ -94,19 +69,19 @@ class RequestedRight implements RequestedRightInterface */ final public function getSource(): SourceInterface { - if ($this->isReloadNeccessary()) { - $this->loadSource(); - $this->setSourceIfNotSet(); - } + $this->validateRequestedSource(); + $this->loadSource(); + $this->validateLoad(); return $this->source; } - private function setSourceIfNotSet(): void + private function validateLoad(): void { - if (!isset($this->source)) { - $this->source = $this->requestedSource; + if ($this->source) { + return; } + throw new NotSetException('The Requested Source couldn\'t be found!'); } /** diff --git a/application/symfony/src/Exception/PreconditionFailedException.php b/application/symfony/src/Exception/PreconditionFailedException.php new file mode 100644 index 0000000..3a3c084 --- /dev/null +++ b/application/symfony/src/Exception/PreconditionFailedException.php @@ -0,0 +1,10 @@ +requestedRight->getLayer()); } - public function testUnsavedRequestedSource(): void + public function testRequestedSourceWithoutAttributes(): void { - $source = $this->createMock(RequestedSource::class); - $this->requestedRight->setRequestedSource($source); - $this->assertEquals($source, $this->requestedRight->getSource()); + $requestedSource = $this->createMock(RequestedSource::class); + $this->requestedRight->setRequestedSource($requestedSource); + $this->expectException(PreconditionFailedException::class); + $this->requestedRight->getSource(); } public function testKnownSource(): void @@ -57,8 +60,20 @@ class RequestedRightTest extends KernelTestCase $sourceResponse1 = $this->requestedRight->getSource(); $this->assertGreaterThan(0, $sourceResponse1->getId()); $requestedSource->setSlug(''); - $sourceResponse2 = $this->requestedRight->getSource(); - $this->assertInstanceOf(RequestedSourceInterface::class, $sourceResponse2); - $this->assertFalse($sourceResponse2->hasId()); + $this->expectException(NotSetException::class); + $this->requestedRight->getSource(); + } + + public function testEqualsSlug(): void + { + $slug = SystemSlugType::IMPRINT; + $requestedSource = $this->createMock(RequestedSourceInterface::class); + $requestedSource->method('getSlug')->willReturn($slug); + $requestedSource->method('hasSlug')->willReturn(true); + $this->assertEquals($slug, $requestedSource->getSlug()); + $this->requestedRight->setRequestedSource($requestedSource); + $responseSource1 = $this->requestedRight->getSource(); + $responseSource2 = $this->requestedRight->getSource(); + $this->assertEquals($responseSource1, $responseSource2); } }