Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. <?php
  2.  
  3. namespace src\Decorator;
  4.  
  5. use DateTime;
  6. use Exception;
  7. use Psr\Cache\CacheItemPoolInterface;
  8. use Psr\Log\LoggerInterface;
  9. use src\Integration\DataProvider;
  10.  
  11. class DecoratorManager extends DataProvider
  12. {
  13.     public $cache;
  14.     public $logger;
  15.  
  16.     /**
  17.      * @param string $host
  18.      * @param string $user
  19.      * @param string $password
  20.      * @param CacheItemPoolInterface $cache
  21.      */
  22.     public function __construct($host, $user, $password, CacheItemPoolInterface $cache)
  23.     {
  24.         parent::__construct($host, $user, $password);
  25.         $this->cache = $cache;
  26.     }
  27.  
  28.     public function setLogger(LoggerInterface $logger)
  29.     {
  30.         $this->logger = $logger;
  31.     }
  32.  
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function getResponse(array $input)
  37.     {
  38.         try {
  39.             $cacheKey = $this->getCacheKey($input);
  40.             $cacheItem = $this->cache->getItem($cacheKey);
  41.             if ($cacheItem->isHit()) {
  42.                 return $cacheItem->get();
  43.             }
  44.  
  45.             $result = parent::get($input);
  46.  
  47.             $cacheItem
  48.                 ->set($result)
  49.                 ->expiresAt(
  50.                     (new DateTime())->modify('+1 day')
  51.                 );
  52.  
  53.             return $result;
  54.         } catch (Exception $e) {
  55.             $this->logger->critical('Error');
  56.         }
  57.  
  58.         return [];
  59.     }
  60.  
  61.     public function getCacheKey(array $input)
  62.     {
  63.         return json_encode($input);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement