Guest User

Untitled

a guest
Jan 13th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. namespace srcIntegration;
  2.  
  3. class DataProvider
  4. {
  5. private $host;
  6. private $user;
  7. private $password;
  8.  
  9. /**
  10. * @param $host
  11. * @param $user
  12. * @param $password
  13. */
  14. public function __construct($host, $user, $password)
  15. {
  16. $this->host = $host;
  17. $this->user = $user;
  18. $this->password = $password;
  19. }
  20.  
  21. /**
  22. * @param array $request
  23. *
  24. * @return array
  25. */
  26. public function get(array $request)
  27. {
  28. // returns a response from external service
  29. }
  30. }
  31.  
  32. namespace srcDecorator;
  33.  
  34. use DateTime;
  35. use Exception;
  36. use PsrCacheCacheItemPoolInterface;
  37. use PsrLogLoggerInterface;
  38. use srcIntegrationDataProvider;
  39.  
  40. class DecoratorManager extends DataProvider
  41. {
  42. public $cache;
  43. public $logger;
  44.  
  45. /**
  46. * @param string $host
  47. * @param string $user
  48. * @param string $password
  49. * @param CacheItemPoolInterface $cache
  50. */
  51. public function __construct($host, $user, $password, CacheItemPoolInterface $cache)
  52. {
  53. parent::__construct($host, $user, $password);
  54. $this->cache = $cache;
  55. }
  56.  
  57. public function setLogger(LoggerInterface $logger)
  58. {
  59. $this->logger = $logger;
  60. }
  61.  
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getResponse(array $input)
  66. {
  67. try {
  68. $cacheKey = $this->getCacheKey($input);
  69. $cacheItem = $this->cache->getItem($cacheKey);
  70. if ($cacheItem->isHit()) {
  71. return $cacheItem->get();
  72. }
  73.  
  74. $result = parent::get($input);
  75.  
  76. $cacheItem
  77. ->set($result)
  78. ->expiresAt(
  79. (new DateTime())->modify('+1 day')
  80. );
  81.  
  82. return $result;
  83. } catch (Exception $e) {
  84. $this->logger->critical('Error');
  85. }
  86.  
  87. return [];
  88. }
  89.  
  90. public function getCacheKey(array $input)
  91. {
  92. return json_encode($input);
  93. }
  94. }
Add Comment
Please, Sign In to add comment