vendor/payneticsteam/paynetics-bundle/src/Paynetics/EventSubscriber/EntityCacheLoadSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. namespace Paynetics\EventSubscriber;
  3. use Paynetics\Annotation\Cache\Settings;
  4. use Paynetics\Cache\CacheableInterface;
  5. use Paynetics\Cache\RedisCacheRequest;
  6. use Paynetics\Event\EntityCacheEvent;
  7. use Paynetics\Extractor\AnnotationExtractor;
  8. use Psr\Cache\InvalidArgumentException;
  9. use ReflectionException;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. /**
  13.  * Class EntityCacheLoadSubscriber
  14.  * @package Paynetics\EventSubscriber
  15.  */
  16. class EntityCacheLoadSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var AnnotationExtractor
  20.      */
  21.     private $extractor;
  22.     /**
  23.      * @var ContainerInterface
  24.      */
  25.     private $container;
  26.     /**
  27.      * @var RedisCacheRequest
  28.      */
  29.     private $cache;
  30.     /**
  31.      * EntityCacheLoadSubscriber constructor.
  32.      * @param AnnotationExtractor $extractor
  33.      * @param ContainerInterface $container
  34.      * @param RedisCacheRequest $cache
  35.      */
  36.     public function __construct(AnnotationExtractor $extractorContainerInterface $containerRedisCacheRequest $cache)
  37.     {
  38.         $this->extractor $extractor;
  39.         $this->container $container;
  40.         $this->cache $cache;
  41.     }
  42.     /**
  43.      * @inheritDoc
  44.      */
  45.     public static function getSubscribedEvents():array
  46.     {
  47.         return [
  48.             EntityCacheEvent::class => 'onEntityLoaded'
  49.         ];
  50.     }
  51.     /**
  52.      * @param EntityCacheEvent $event
  53.      * @throws InvalidArgumentException
  54.      * @throws ReflectionException
  55.      */
  56.     public function onEntityLoaded(EntityCacheEvent $event): void
  57.     {
  58.         $config $this->container->getParameter('cache');
  59.         if (!$config['enable']) {
  60.             return;
  61.         }
  62.         $entity $event->getEntity();
  63.         if (!$entity instanceof CacheableInterface) {
  64.             return;
  65.         }
  66.         [$identity$options$fields] = $this->prepare($entity);
  67.         $this->cache->set($identityjson_encode($fieldsJSON_THROW_ON_ERROR512), $options['ex'] ?? 0);
  68.     }
  69.     /**
  70.      * @param $entity
  71.      * @return array
  72.      * @throws InvalidArgumentException
  73.      * @throws ReflectionException
  74.      */
  75.     private function prepare($entity): array
  76.     {
  77.         $data $this->extractor->extract($entity);
  78.         /**
  79.          * @var $settings Settings
  80.          */
  81.         $settings $data['settings'];
  82.         $fields $data['fields'];
  83.         $identity "{$data['identity']}:{$fields['identity']}";
  84.         unset($fields['identity']);
  85.         $options = [];
  86.         if ($settings->expiration 0) {
  87.             $options['ex'] = $settings->expiration;
  88.         }
  89.         return [$identity$options$fields];
  90.     }
  91. }