mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-11-04 03:07:58 +00:00 
			
		
		
		
	Optimized draft for requested source
This commit is contained in:
		@@ -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!');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,10 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Exception;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 */
 | 
			
		||||
final class PreconditionFailedException extends \Exception
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
@@ -10,6 +10,8 @@ use App\DBAL\Types\Meta\Right\LayerType;
 | 
			
		||||
use App\Domain\SourceManagement\RequestedSource;
 | 
			
		||||
use App\DBAL\Types\SystemSlugType;
 | 
			
		||||
use App\Domain\SourceManagement\RequestedSourceInterface;
 | 
			
		||||
use App\Exception\PreconditionFailedException;
 | 
			
		||||
use App\Exception\NotSetException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
@@ -42,11 +44,12 @@ class RequestedRightTest extends KernelTestCase
 | 
			
		||||
        var_dump($this->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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user