|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of the CleverAge/ProcessBundle package. |
| 4 | + * |
| 5 | + * Copyright (C) 2017-2021 Clever-Age |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace CleverAge\ProcessBundle\Transformer; |
| 12 | + |
| 13 | +use CleverAge\ProcessBundle\Registry\TransformerRegistry; |
| 14 | +use Psr\Cache\CacheItemPoolInterface; |
| 15 | +use Psr\Cache\InvalidArgumentException; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 18 | + |
| 19 | +class CachedTransformer implements ConfigurableTransformerInterface |
| 20 | +{ |
| 21 | + |
| 22 | + const CACHE_SEPARATOR = '|'; |
| 23 | + |
| 24 | + use TransformerTrait; |
| 25 | + |
| 26 | + /** @var CacheItemPoolInterface */ |
| 27 | + protected $cache; |
| 28 | + |
| 29 | + /** @var LoggerInterface */ |
| 30 | + protected $logger; |
| 31 | + |
| 32 | + /** |
| 33 | + * CachedTransformer constructor. |
| 34 | + * |
| 35 | + * @param TransformerRegistry $transformerRegistry |
| 36 | + * @param CacheItemPoolInterface $cache |
| 37 | + * @param LoggerInterface $logger |
| 38 | + */ |
| 39 | + public function __construct(TransformerRegistry $transformerRegistry, CacheItemPoolInterface $cache, LoggerInterface $logger) |
| 40 | + { |
| 41 | + $this->transformerRegistry = $transformerRegistry; |
| 42 | + $this->cache = $cache; |
| 43 | + $this->logger = $logger; |
| 44 | + } |
| 45 | + |
| 46 | + public function configureOptions(OptionsResolver $resolver) |
| 47 | + { |
| 48 | + $resolver->setRequired('cache_key'); |
| 49 | + $resolver->setAllowedTypes('cache_key', 'string'); |
| 50 | + $this->configureTransformersOptions($resolver); |
| 51 | + $this->configureTransformersOptions($resolver, 'key_transformers'); |
| 52 | + } |
| 53 | + |
| 54 | + public function transform($value, array $options = []) |
| 55 | + { |
| 56 | + $cacheKey = $this->generateCacheKey($options['cache_key'], $value, $options); |
| 57 | + if ($cacheKey && $this->cache instanceof CacheItemPoolInterface) { |
| 58 | + try { |
| 59 | + $cacheItem = $this->cache->getItem($cacheKey); |
| 60 | + if ($cacheItem->isHit()) { |
| 61 | + |
| 62 | + return $cacheItem->get(); |
| 63 | + } else { |
| 64 | + $newValue = $this->applyTransformers($options['transformers'], $value); |
| 65 | + $cacheItem->set($newValue); |
| 66 | + $success = $this->cache->saveDeferred($cacheItem); |
| 67 | + |
| 68 | + if (!$success) { |
| 69 | + $this->logger->warning('Cannot save cache item', ['cache_key' => $cacheKey]); |
| 70 | + } |
| 71 | + |
| 72 | + return $newValue; |
| 73 | + } |
| 74 | + } catch (InvalidArgumentException $exception) { |
| 75 | + $this->logger->warning('Cannot get cache item', ['cache_key' => $cacheKey, 'message' => $exception->getMessage()]); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $this->applyTransformers($options['transformers'], $value); |
| 80 | + } |
| 81 | + |
| 82 | + public function getCode() |
| 83 | + { |
| 84 | + return 'cached'; |
| 85 | + } |
| 86 | + |
| 87 | + protected function generateCacheKey($cacheKeyRoot, $value, $options) |
| 88 | + { |
| 89 | + $value = $this->applyTransformers($options['key_transformers'], $value); |
| 90 | + |
| 91 | + if (!\is_string($value)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + return \implode(self::CACHE_SEPARATOR, [$cacheKeyRoot, \rawurlencode($value)]); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments