mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-04-18 19:09:20 +02:00
Implemented LazyLoading for RequestedEntity
This commit is contained in:
parent
271aa36552
commit
ac24ab950d
@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Infinito\Domain\RequestManagement\Entity;
|
namespace Infinito\Domain\RequestManagement\Entity;
|
||||||
|
|
||||||
use Infinito\Entity\AbstractEntity;
|
use Infinito\Entity\AbstractEntity;
|
||||||
@ -18,9 +17,12 @@ use Infinito\Attribut\ClassAttribut;
|
|||||||
use Infinito\Exception\AllreadyDefinedException;
|
use Infinito\Exception\AllreadyDefinedException;
|
||||||
use Infinito\Domain\RequestManagement\Right\RequestedRightInterface;
|
use Infinito\Domain\RequestManagement\Right\RequestedRightInterface;
|
||||||
use Infinito\Domain\RepositoryManagement\LayerRepositoryFactoryService;
|
use Infinito\Domain\RepositoryManagement\LayerRepositoryFactoryService;
|
||||||
|
use Infinito\Attribut\SlugAttributInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
|
* @todo Move lazy load to an own child class
|
||||||
*/
|
*/
|
||||||
class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
||||||
{
|
{
|
||||||
@ -28,6 +30,12 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
RequestedRightAttribut,
|
RequestedRightAttribut,
|
||||||
ClassAttribut{ setClass as private setClassTrait; getClass as private getClassTrait; }
|
ClassAttribut{ setClass as private setClassTrait; getClass as private getClassTrait; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var EntityInterface|NULL Important for lazy loading
|
||||||
|
*/
|
||||||
|
private static $lazyLoadedEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BE AWARE:
|
* BE AWARE:
|
||||||
* This attribut can lead to sideeffects, because it can be defined, or NULL.
|
* This attribut can lead to sideeffects, because it can be defined, or NULL.
|
||||||
@ -37,6 +45,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
private $layerRepositoryFactoryService;
|
private $layerRepositoryFactoryService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @throws NotSetException
|
* @throws NotSetException
|
||||||
*/
|
*/
|
||||||
private function validateHasIdentity(): void
|
private function validateHasIdentity(): void
|
||||||
@ -47,6 +56,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @param EntityInterface|null $entity
|
* @param EntityInterface|null $entity
|
||||||
*
|
*
|
||||||
* @throws NotFoundHttpException
|
* @throws NotFoundHttpException
|
||||||
@ -59,6 +69,20 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
|
* @return EntityInterface The lazy loaded entity
|
||||||
|
*/
|
||||||
|
private function lazyLoadEntity(): ?EntityInterface
|
||||||
|
{
|
||||||
|
if ($this->isLazyLoadNeccessary()) {
|
||||||
|
$entity = $this->loadEntityBySlugOrId();
|
||||||
|
self::$lazyLoadedEntity = $entity;
|
||||||
|
}
|
||||||
|
return self::$lazyLoadedEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
* @return EntityInterface|SourceInterface|null
|
* @return EntityInterface|SourceInterface|null
|
||||||
*/
|
*/
|
||||||
private function loadEntityBySlugOrId(): ?EntityInterface
|
private function loadEntityBySlugOrId(): ?EntityInterface
|
||||||
@ -71,6 +95,26 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
|
* @return bool True if a reload is neccessary
|
||||||
|
*/
|
||||||
|
private function isLazyLoadNeccessary(): bool
|
||||||
|
{
|
||||||
|
if (self::$lazyLoadedEntity) {
|
||||||
|
if ($this->hasId()) {
|
||||||
|
return $this->id !== self::$lazyLoadedEntity->getId();
|
||||||
|
}
|
||||||
|
if ($this->hasSlug()) {
|
||||||
|
if (self::$lazyLoadedEntity instanceof SlugAttributInterface) {
|
||||||
|
return $this->slug !== self::$lazyLoadedEntity->getSlug();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
* @throws NotCorrectInstanceException
|
* @throws NotCorrectInstanceException
|
||||||
*
|
*
|
||||||
* @return SourceInterface|null
|
* @return SourceInterface|null
|
||||||
@ -85,6 +129,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @return EntityInterface|null
|
* @return EntityInterface|null
|
||||||
*/
|
*/
|
||||||
private function loadById(): ?EntityInterface
|
private function loadById(): ?EntityInterface
|
||||||
@ -95,6 +140,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @throws NotFoundHttpException
|
* @throws NotFoundHttpException
|
||||||
*/
|
*/
|
||||||
private function validateLayerRepositoryFactoryService(): void
|
private function validateLayerRepositoryFactoryService(): void
|
||||||
@ -105,6 +151,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @return RepositoryInterface
|
* @return RepositoryInterface
|
||||||
*/
|
*/
|
||||||
private function getEntityRepository(): RepositoryInterface
|
private function getEntityRepository(): RepositoryInterface
|
||||||
@ -117,6 +164,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* @param LayerRepositoryFactoryServiceInterface|null $layerRepositoryFactoryService
|
* @param LayerRepositoryFactoryServiceInterface|null $layerRepositoryFactoryService
|
||||||
*/
|
*/
|
||||||
public function __construct(?LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService = null)
|
public function __construct(?LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService = null)
|
||||||
@ -125,6 +173,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::hasIdentity()
|
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::hasIdentity()
|
||||||
@ -135,6 +184,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity()
|
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity()
|
||||||
@ -155,6 +205,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Attribut\ClassAttributInterface::setClass()
|
* @see \Infinito\Attribut\ClassAttributInterface::setClass()
|
||||||
@ -168,6 +219,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
|
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
|
||||||
@ -176,6 +228,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
{
|
{
|
||||||
$this->validateHasIdentity();
|
$this->validateHasIdentity();
|
||||||
$entity = $this->loadEntityBySlugOrId();
|
$entity = $this->loadEntityBySlugOrId();
|
||||||
|
//$entity = $this->lazyLoadEntity();
|
||||||
$this->validateLoadedEntity($entity);
|
$this->validateLoadedEntity($entity);
|
||||||
|
|
||||||
return $entity;
|
return $entity;
|
||||||
@ -197,6 +250,7 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
*
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Attribut\ClassAttributInterface::getClass()
|
* @see \Infinito\Attribut\ClassAttributInterface::getClass()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user