Advertisement
Guest User

SkyEng Test

a guest
Mar 28th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. namespace src\Integration;
  5.  
  6. interface DataProviderInterface
  7. {
  8.     public function get(array $request) : array;
  9. }
  10.  
  11. <?php
  12. declare(strict_types=1);
  13.  
  14. namespace src\Integration;
  15.  
  16. class DataProvider implements DataProviderInterface
  17. {
  18.     protected $host;
  19.     protected $user;
  20.     protected $password;
  21.  
  22.     /**
  23.      * @param $host
  24.      * @param $user
  25.      * @param $password
  26.      */
  27.     public function __construct(string $host, string $user, string $password)
  28.     {
  29.         $this->host = $host;
  30.         $this->user = $user;
  31.         $this->password = $password;
  32.     }
  33.  
  34.     /**
  35.      * @param array $request
  36.      *
  37.      * @return array
  38.      */
  39.     public function get(array $request) : array
  40.     {
  41.         // returns a response from external service
  42.     }
  43. }
  44.  
  45. <?php
  46. declare(strict_types=1);
  47.  
  48. namespace src\Decorator;
  49.  
  50. use Exception;
  51. use Psr\Cache\CacheItemPoolInterface;
  52. use Psr\Log\LoggerInterface;
  53. use src\Integration\DataProviderInterface;
  54.  
  55. class DataProviderCachingDecorator implements DataProviderInterface
  56. {
  57.     protected $provider;
  58.     protected $cache;
  59.     protected $logger;
  60.  
  61.     public function __construct(DataProviderInterface $provider, CacheItemPoolInterface $cache)
  62.     {
  63.         $this->provider = $provider;
  64.         $this->cache = $cache;
  65.     }
  66.  
  67.     public function setLogger(LoggerInterface $logger)
  68.     {
  69.         $this->logger = $logger;
  70.     }
  71.  
  72.     public function get(array $request) : array
  73.     {
  74.         $result = [];
  75.         try {
  76.             $cacheKey = $this->getCacheKey($request);
  77.             $cacheItem = $this->cache->getItem($cacheKey);
  78.             if ($cacheItem->isHit()) {
  79.                 $result = $cacheItem->get();
  80.             } else {
  81.                 $result = $this->provider->get($request);
  82.                 $cacheItem
  83.                     ->set($result)
  84.                     ->expiresAfter(3600 * 24); // expires after a day
  85.             }
  86.         } catch (Exception $e) {
  87.             if ($this->logger) {
  88.                 $this->logger->critical(implode(', ', [ __FILE__, __LINE__, $e->getMessage() ]));
  89.             } else {
  90.                 throw $e;
  91.             }
  92.         }
  93.         return $result;
  94.     }
  95.  
  96.     public function getCacheKey(array $input)
  97.     {
  98.         return md5(json_encode($input));
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement