2019-04-15 01:37:17 +02:00

117 lines
3.4 KiB
PHP

<?php
namespace Infinito\Domain\RequestManagement\Right;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Attribut\LayerAttribut;
use Infinito\Attribut\RecieverAttribut;
use Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface;
use Infinito\Attribut\RequestedEntityAttribut;
use Infinito\Entity\Meta\MetaInterface;
use Infinito\Domain\RequestManagement\Entity\RequestedEntity;
use Infinito\Attribut\ActionTypeAttribut;
use Infinito\Exception\Core\NotCorrectInstanceCoreException;
use Infinito\Exception\Core\NoIdentityCoreException;
use Infinito\Exception\Collection\ContainsElementException;
/**
* @author kevinfrantz
*
* @todo Check out if the performance of this class can be optimized!
*/
class RequestedRight implements RequestedRightInterface
{
use LayerAttribut, RecieverAttribut, RequestedEntityAttribut, ActionTypeAttribut{
setActionType as private setActionTypeTrait;
}
/**
* @var SourceInterface
*/
private $source;
/**
* @throws NotCorrectInstanceCoreException
*/
private function loadSource(): void
{
$entity = $this->requestedEntity->getEntity();
if ($entity instanceof SourceInterface) {
$this->source = $entity;
return;
}
if ($entity instanceof MetaInterface) {
$this->source = $entity->getSource();
return;
}
throw new NotCorrectInstanceCoreException('The entity instance '.get_class($entity).' can\'t be processed');
}
/**
* @throws NoIdentityCoreException If the source has no id or slug
*/
private function validateRequestedEntity(): void
{
if ($this->requestedEntity->hasIdentity()) {
return;
}
throw new NoIdentityCoreException(get_class($this->requestedEntity).' needs to have a defined id or slug attribut!');
}
/**
* @param RequestedEntity|null $requestedEntity
*/
public function __construct(?RequestedEntity $requestedEntity = null)
{
if ($requestedEntity) {
$this->setRequestedEntity($requestedEntity);
}
}
/**
* This function declares the attribute actionType as inmutable
* {@inheritdoc}
*
* @see \Infinito\Attribut\ActionTypeAttributInterface::setActionType()
*/
public function setActionType(string $actionType): void
{
if (isset($this->actionType)) {
throw new ContainsElementException("The action type is allready set! Origine: $this->actionType New: $actionType");
}
$this->setActionTypeTrait($actionType);
}
/**
* Uses some kind of Lazy loading.
*
* @see https://en.wikipedia.org/wiki/Lazy_loading
* {@inheritdoc}
* @see \Infinito\Domain\RequestManagement\Right\RequestedRightInterface::getSource()
*/
final public function getSource(): SourceInterface
{
$this->validateRequestedEntity();
$this->loadSource();
return $this->source;
}
/**
* Overriding is neccessary to declare the correct relation.
*
* {@inheritdoc}
*
* @see \Infinito\Domain\RequestManagement\Right\RequestedRightInterface::setRequestedEntity()
*/
final public function setRequestedEntity(RequestedEntityInterface $requestedEntity): void
{
$this->requestedEntity = $requestedEntity;
if (!$requestedEntity->hasRequestedRight()) {
$this->requestedEntity->setRequestedRight($this);
}
}
}