mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-04-18 19:09:20 +02:00
Implemented class LazyRequestedEntity
This commit is contained in:
parent
ac24ab950d
commit
8b7b3bc44e
@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Infinito\Domain\RequestManagement\Entity;
|
||||||
|
|
||||||
|
use Infinito\Entity\EntityInterface;
|
||||||
|
use Infinito\Attribut\SlugAttributInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class allows to use the RequestedEntity via LazyLoading
|
||||||
|
* It reduce the ammount of requests which are send to the database.
|
||||||
|
*
|
||||||
|
* @author kevinfrantz
|
||||||
|
*/
|
||||||
|
class LazyRequestedEntity extends RequestedEntity
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var EntityInterface|null Important for lazy loading
|
||||||
|
*/
|
||||||
|
private static $lazyLoadedEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*
|
||||||
|
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntity::loadEntity()
|
||||||
|
*/
|
||||||
|
protected function loadEntity(): ?EntityInterface
|
||||||
|
{
|
||||||
|
return $this->lazyLoadEntity();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return EntityInterface|null
|
||||||
|
*/
|
||||||
|
private function lazyLoadEntity(): ?EntityInterface
|
||||||
|
{
|
||||||
|
if ($this->isLazyLoadNeccessary()) {
|
||||||
|
$entity = parent::loadEntity();
|
||||||
|
self::$lazyLoadedEntity = $entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$lazyLoadedEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Infinito\Domain\RequestManagement\Entity;
|
namespace Infinito\Domain\RequestManagement\Entity;
|
||||||
|
|
||||||
use Infinito\Entity\AbstractEntity;
|
use Infinito\Entity\AbstractEntity;
|
||||||
@ -17,11 +18,10 @@ 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
|
* @todo Move lazy load to an own child class
|
||||||
*/
|
*/
|
||||||
class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
||||||
@ -30,12 +30,6 @@ 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.
|
||||||
@ -45,44 +39,28 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
private $layerRepositoryFactoryService;
|
private $layerRepositoryFactoryService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @throws NotSetException
|
* @throws NotSetException
|
||||||
*/
|
*/
|
||||||
private function validateHasIdentity(): void
|
private function validateHasIdentity(): void
|
||||||
{
|
{
|
||||||
if (! ($this->hasId() || $this->hasSlug())) {
|
if (!($this->hasId() || $this->hasSlug())) {
|
||||||
throw new NotSetException('No identity attribut like id or slug was set!');
|
throw new NotSetException('No identity attribut like id or slug was set!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param EntityInterface|null $entity
|
* @param EntityInterface|null $entity
|
||||||
*
|
*
|
||||||
* @throws NotFoundHttpException
|
* @throws NotFoundHttpException
|
||||||
*/
|
*/
|
||||||
private function validateLoadedEntity(?EntityInterface $entity): void
|
private function validateLoadedEntity(?EntityInterface $entity): void
|
||||||
{
|
{
|
||||||
if (! $entity) {
|
if (!$entity) {
|
||||||
throw new NotFoundHttpException('Entity with {id:"' . $this->id . '",slug:"' . $this->slug . '"} not found');
|
throw new NotFoundHttpException('Entity with {id:"'.$this->id.'",slug:"'.$this->slug.'"} not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @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
|
||||||
@ -95,26 +73,6 @@ 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
|
||||||
@ -125,11 +83,10 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
if ($repository instanceof SourceRepositoryInterface) {
|
if ($repository instanceof SourceRepositoryInterface) {
|
||||||
return $repository->findOneBySlug($this->slug);
|
return $repository->findOneBySlug($this->slug);
|
||||||
}
|
}
|
||||||
throw new NotCorrectInstanceException('To read an entity by slug is just allowed for entitys of type ' . AbstractSource::class);
|
throw new NotCorrectInstanceException('To read an entity by slug is just allowed for entitys of type '.AbstractSource::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return EntityInterface|null
|
* @return EntityInterface|null
|
||||||
*/
|
*/
|
||||||
private function loadById(): ?EntityInterface
|
private function loadById(): ?EntityInterface
|
||||||
@ -140,18 +97,16 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @throws NotFoundHttpException
|
* @throws NotFoundHttpException
|
||||||
*/
|
*/
|
||||||
private function validateLayerRepositoryFactoryService(): void
|
private function validateLayerRepositoryFactoryService(): void
|
||||||
{
|
{
|
||||||
if (! $this->layerRepositoryFactoryService) {
|
if (!$this->layerRepositoryFactoryService) {
|
||||||
throw new NotSetException('The operation is not possible, because the class ' . LayerRepositoryFactoryService::class . ' is not injected!');
|
throw new NotSetException('The operation is not possible, because the class '.LayerRepositoryFactoryService::class.' is not injected!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return RepositoryInterface
|
* @return RepositoryInterface
|
||||||
*/
|
*/
|
||||||
private function getEntityRepository(): RepositoryInterface
|
private function getEntityRepository(): RepositoryInterface
|
||||||
@ -164,7 +119,14 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @return EntityInterface|null
|
||||||
|
*/
|
||||||
|
protected function loadEntity(): ?EntityInterface
|
||||||
|
{
|
||||||
|
return $this->loadEntityBySlugOrId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* @param LayerRepositoryFactoryServiceInterface|null $layerRepositoryFactoryService
|
* @param LayerRepositoryFactoryServiceInterface|null $layerRepositoryFactoryService
|
||||||
*/
|
*/
|
||||||
public function __construct(?LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService = null)
|
public function __construct(?LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService = null)
|
||||||
@ -173,7 +135,6 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::hasIdentity()
|
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::hasIdentity()
|
||||||
@ -184,7 +145,6 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity()
|
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::setIdentity()
|
||||||
@ -205,7 +165,6 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Attribut\ClassAttributInterface::setClass()
|
* @see \Infinito\Attribut\ClassAttributInterface::setClass()
|
||||||
@ -219,16 +178,14 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
|
* @see \Infinito\Domain\RequestManagement\Entity\RequestedEntityInterface::getEntity()
|
||||||
*/
|
*/
|
||||||
public function getEntity(): EntityInterface
|
final public function getEntity(): EntityInterface
|
||||||
{
|
{
|
||||||
$this->validateHasIdentity();
|
$this->validateHasIdentity();
|
||||||
$entity = $this->loadEntityBySlugOrId();
|
$entity = $this->loadEntity();
|
||||||
//$entity = $this->lazyLoadEntity();
|
|
||||||
$this->validateLoadedEntity($entity);
|
$this->validateLoadedEntity($entity);
|
||||||
|
|
||||||
return $entity;
|
return $entity;
|
||||||
@ -244,13 +201,12 @@ class RequestedEntity extends AbstractEntity implements RequestedEntityInterface
|
|||||||
public function setRequestedRight(RequestedRightInterface $requestedRight): void
|
public function setRequestedRight(RequestedRightInterface $requestedRight): void
|
||||||
{
|
{
|
||||||
$this->requestedRight = $requestedRight;
|
$this->requestedRight = $requestedRight;
|
||||||
if (! $this->requestedRight->hasRequestedEntity()) {
|
if (!$this->requestedRight->hasRequestedEntity()) {
|
||||||
$this->requestedRight->setRequestedEntity($this);
|
$this->requestedRight->setRequestedEntity($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
* @see \Infinito\Attribut\ClassAttributInterface::getClass()
|
* @see \Infinito\Attribut\ClassAttributInterface::getClass()
|
||||||
|
@ -7,7 +7,7 @@ use Infinito\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
|
|||||||
/**
|
/**
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
*/
|
*/
|
||||||
final class RequestedEntityService extends RequestedEntity implements RequestedEntityServiceInterface
|
class RequestedEntityService extends LazyRequestedEntity implements RequestedEntityServiceInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService
|
* @param LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService
|
||||||
|
Loading…
x
Reference in New Issue
Block a user