122 lines
2.9 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;
/**
* @author kevinfrantz
*/
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);
}
/**
* @return bool
*/
private function isIdEquals(): bool
{
2019-01-13 21:52:09 +01:00
if (!$this->requestedSource->hasId() || !$this->source->hasId()) {
2019-01-13 17:02:15 +01:00
return false;
}
return $this->requestedSource->getId() === $this->source->getId();
}
/**
* @return bool
*/
private function isSlugEquals(): bool
{
2019-01-13 21:52:09 +01:00
if (!$this->requestedSource->hasSlug() || !$this->source->hasSlug()) {
2019-01-13 17:02:15 +01:00
return false;
}
return $this->requestedSource->getSlug() === $this->source->getSlug();
}
2019-01-13 21:52:09 +01:00
/**
* @return bool Returns true if the source is not set!
*/
private function isSourceNotSet(): bool
{
return !isset($this->source);
}
2019-01-13 17:02:15 +01:00
/**
* @return bool Tells if a reload of the source is neccessary
*/
private function isReloadNeccessary(): bool
{
2019-01-13 21:52:09 +01:00
return $this->isSourceNotSet() || $this->isIdEquals() || $this->isSlugEquals();
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
{
if ($this->isReloadNeccessary()) {
$this->loadSource();
2019-01-13 21:52:09 +01:00
$this->setSourceIfNotSet();
2019-01-13 17:02:15 +01:00
}
return $this->source;
}
2019-01-13 21:52:09 +01:00
private function setSourceIfNotSet(): void
{
if (!isset($this->source)) {
$this->source = $this->requestedSource;
}
}
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;
}
}