Optimized draft for requested source

This commit is contained in:
Kevin Frantz 2019-01-14 21:27:06 +01:00
parent 7af1b3e58c
commit ee4dc0d052
3 changed files with 48 additions and 48 deletions

View File

@ -9,9 +9,13 @@ use App\Entity\Source\SourceInterface;
use App\Entity\Attribut\TypeAttribut; use App\Entity\Attribut\TypeAttribut;
use App\Entity\Attribut\LayerAttribut; use App\Entity\Attribut\LayerAttribut;
use App\Entity\Attribut\RecieverAttribut; use App\Entity\Attribut\RecieverAttribut;
use App\Exception\PreconditionFailedException;
use App\Exception\NotSetException;
/** /**
* @author kevinfrantz * @author kevinfrantz
*
* @todo Check out if the performance of this class can be optimized!
*/ */
class RequestedRight implements RequestedRightInterface 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()) { if ($this->requestedSource->hasSlug() || $this->requestedSource->hasId()) {
return false; return;
} }
throw new PreconditionFailedException(get_class($this->requestedSource).' needs to have a defined attribut id or slug!');
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();
} }
/** /**
@ -94,19 +69,19 @@ class RequestedRight implements RequestedRightInterface
*/ */
final public function getSource(): SourceInterface final public function getSource(): SourceInterface
{ {
if ($this->isReloadNeccessary()) { $this->validateRequestedSource();
$this->loadSource(); $this->loadSource();
$this->setSourceIfNotSet(); $this->validateLoad();
}
return $this->source; return $this->source;
} }
private function setSourceIfNotSet(): void private function validateLoad(): void
{ {
if (!isset($this->source)) { if ($this->source) {
$this->source = $this->requestedSource; return;
} }
throw new NotSetException('The Requested Source couldn\'t be found!');
} }
/** /**

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exception;
/**
* @author kevinfrantz
*/
final class PreconditionFailedException extends \Exception
{
}

View File

@ -10,6 +10,8 @@ use App\DBAL\Types\Meta\Right\LayerType;
use App\Domain\SourceManagement\RequestedSource; use App\Domain\SourceManagement\RequestedSource;
use App\DBAL\Types\SystemSlugType; use App\DBAL\Types\SystemSlugType;
use App\Domain\SourceManagement\RequestedSourceInterface; use App\Domain\SourceManagement\RequestedSourceInterface;
use App\Exception\PreconditionFailedException;
use App\Exception\NotSetException;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -42,11 +44,12 @@ class RequestedRightTest extends KernelTestCase
var_dump($this->requestedRight->getLayer()); var_dump($this->requestedRight->getLayer());
} }
public function testUnsavedRequestedSource(): void public function testRequestedSourceWithoutAttributes(): void
{ {
$source = $this->createMock(RequestedSource::class); $requestedSource = $this->createMock(RequestedSource::class);
$this->requestedRight->setRequestedSource($source); $this->requestedRight->setRequestedSource($requestedSource);
$this->assertEquals($source, $this->requestedRight->getSource()); $this->expectException(PreconditionFailedException::class);
$this->requestedRight->getSource();
} }
public function testKnownSource(): void public function testKnownSource(): void
@ -57,8 +60,20 @@ class RequestedRightTest extends KernelTestCase
$sourceResponse1 = $this->requestedRight->getSource(); $sourceResponse1 = $this->requestedRight->getSource();
$this->assertGreaterThan(0, $sourceResponse1->getId()); $this->assertGreaterThan(0, $sourceResponse1->getId());
$requestedSource->setSlug(''); $requestedSource->setSlug('');
$sourceResponse2 = $this->requestedRight->getSource(); $this->expectException(NotSetException::class);
$this->assertInstanceOf(RequestedSourceInterface::class, $sourceResponse2); $this->requestedRight->getSource();
$this->assertFalse($sourceResponse2->hasId()); }
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);
} }
} }