Advertisement
Guest User

Untitled

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