src/Repository/WebServices/AbstractCacheWsRepository.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Repository\WebServices;
  3. use App\Serializer\Normalizer\CrossSystemIdMapper;
  4. use App\Services\ApiConsumerService;
  5. use App\Services\CriteriaToRequest;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Criteria;
  8. use Doctrine\Common\Collections\Expr\Comparison;
  9. use Psr\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\Serializer\SerializerInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. abstract class AbstractCacheWsRepository extends AbstractWsRepository {
  15. protected static $_cache=[];
  16. public function __construct(
  17. ApiConsumerService $apiConsumerService,
  18. CriteriaToRequest $criteriaToRequest,
  19. SerializerInterface $serializer,
  20. TranslatorInterface $translator,
  21. SessionInterface $session,
  22. CrossSystemIdMapper $crossSystemIdMapper,
  23. RequestStack $requestStack,
  24. EventDispatcherInterface $eventDispatcher
  25. )
  26. {
  27. $this->global = true;
  28. parent::__construct($apiConsumerService, $criteriaToRequest, $serializer, $translator, $session, $crossSystemIdMapper, $requestStack, $eventDispatcher);
  29. self::$_cache[md5($this->entidad)] = parent::all();
  30. }
  31. public function all($subParameters = '', $cachear = true): ?ArrayCollection
  32. {
  33. if (!$cachear) return parent::all($subParameters, $cachear);
  34. return self::$_cache[md5($this->entidad)];
  35. }
  36. public function find(Criteria $criteria, $cachear = true, $extraparams = []): ?ArrayCollection
  37. {
  38. if (!$cachear) return parent::find($criteria, $cachear, $extraparams);
  39. return self::$_cache[md5($this->entidad)]?self::$_cache[md5($this->entidad)]->matching($criteria):null;
  40. }
  41. public function findById(int $id, $cachear = true, $extraparams = [])
  42. {
  43. if (!$cachear) return parent::findById($id, $cachear, $extraparams);
  44. $criteria = Criteria::create();
  45. $criteria->where(new Comparison('id', Comparison::EQ, $id));
  46. $resultS = self::$_cache[md5($this->entidad)]?self::$_cache[md5($this->entidad)]->matching($criteria):null;
  47. if ($resultS AND $resultS->count() ) return $resultS->first();
  48. return parent::findById($id, false);
  49. }
  50. }//class