<?php
namespace App\Repository\WebServices;
use App\Serializer\Normalizer\CrossSystemIdMapper;
use App\Services\ApiConsumerService;
use App\Services\CriteriaToRequest;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
abstract class AbstractCacheWsRepository extends AbstractWsRepository {
protected static $_cache=[];
public function __construct(
ApiConsumerService $apiConsumerService,
CriteriaToRequest $criteriaToRequest,
SerializerInterface $serializer,
TranslatorInterface $translator,
SessionInterface $session,
CrossSystemIdMapper $crossSystemIdMapper,
RequestStack $requestStack,
EventDispatcherInterface $eventDispatcher
)
{
$this->global = true;
parent::__construct($apiConsumerService, $criteriaToRequest, $serializer, $translator, $session, $crossSystemIdMapper, $requestStack, $eventDispatcher);
self::$_cache[md5($this->entidad)] = parent::all();
}
public function all($subParameters = '', $cachear = true): ?ArrayCollection
{
if (!$cachear) return parent::all($subParameters, $cachear);
return self::$_cache[md5($this->entidad)];
}
public function find(Criteria $criteria, $cachear = true, $extraparams = []): ?ArrayCollection
{
if (!$cachear) return parent::find($criteria, $cachear, $extraparams);
return self::$_cache[md5($this->entidad)]?self::$_cache[md5($this->entidad)]->matching($criteria):null;
}
public function findById(int $id, $cachear = true, $extraparams = [])
{
if (!$cachear) return parent::findById($id, $cachear, $extraparams);
$criteria = Criteria::create();
$criteria->where(new Comparison('id', Comparison::EQ, $id));
$resultS = self::$_cache[md5($this->entidad)]?self::$_cache[md5($this->entidad)]->matching($criteria):null;
if ($resultS AND $resultS->count() ) return $resultS->first();
return parent::findById($id, false);
}
}//class