97 lines
2.5 KiB
PHP
Raw Normal View History

2019-01-13 17:02:15 +01:00
<?php
namespace App\Domain\RightManagement\RightRequestManagement;
use Doctrine\ORM\EntityManager;
use App\Repository\Source\SourceRepository;
use App\Domain\SourceManagement\RequestedSourceInterface;
use App\Entity\Source\SourceInterface;
use App\Entity\Attribut\TypeAttribut;
use App\Entity\Attribut\LayerAttribut;
use App\Entity\Attribut\RecieverAttribut;
2019-01-14 21:27:06 +01:00
use App\Exception\PreconditionFailedException;
use App\Exception\NotSetException;
2019-01-13 17:02:15 +01:00
/**
* @author kevinfrantz
2019-01-14 21:27:06 +01:00
*
* @todo Check out if the performance of this class can be optimized!
2019-01-13 17:02:15 +01:00
*/
class RequestedRight implements RequestedRightInterface
{
use TypeAttribut, LayerAttribut, RecieverAttribut;
/**
* @var SourceRepository
*/
private $sourceRepository;
/**
* @var SourceInterface
*/
private $source;
/**
* @var RequestedSourceInterface
*/
private $requestedSource;
/**
* @param EntityManager $entityManager
*/
public function __construct(SourceRepository $sourceRepository)
{
$this->sourceRepository = $sourceRepository;
}
private function loadSource(): void
{
$this->source = $this->sourceRepository->findOneByIdOrSlug($this->requestedSource);
}
/**
2019-01-14 21:27:06 +01:00
* @throws PreconditionFailedException If the source has no id or slug
2019-01-13 17:02:15 +01:00
*/
2019-01-14 21:27:06 +01:00
private function validateRequestedSource(): void
2019-01-13 17:02:15 +01:00
{
2019-01-14 21:27:06 +01:00
if ($this->requestedSource->hasSlug() || $this->requestedSource->hasId()) {
return;
2019-01-13 17:02:15 +01:00
}
2019-01-14 21:27:06 +01:00
throw new PreconditionFailedException(get_class($this->requestedSource).' needs to have a defined attribut id or slug!');
2019-01-13 17:02:15 +01:00
}
/**
* Uses some kind of Lazy loading.
*
* @see https://en.wikipedia.org/wiki/Lazy_loading
* {@inheritdoc}
* @see \App\Domain\RightManagement\RightRequestManagement\RequestedRightInterface::getSource()
*/
final public function getSource(): SourceInterface
{
2019-01-14 21:27:06 +01:00
$this->validateRequestedSource();
$this->loadSource();
$this->validateLoad();
2019-01-13 17:02:15 +01:00
return $this->source;
}
2019-01-14 21:27:06 +01:00
private function validateLoad(): void
2019-01-13 21:52:09 +01:00
{
2019-01-14 21:27:06 +01:00
if ($this->source) {
return;
2019-01-13 21:52:09 +01:00
}
2019-01-14 21:27:06 +01:00
throw new NotSetException('The Requested Source couldn\'t be found!');
2019-01-13 21:52:09 +01:00
}
2019-01-13 17:02:15 +01:00
/**
* {@inheritdoc}
*
* @see \App\Domain\RightManagement\RightRequestManagement\RequestedRightInterface::setRequestedSource()
*/
final public function setRequestedSource(RequestedSourceInterface $requestedSource)
{
$this->requestedSource = $requestedSource;
}
}