Guest User

Untitled

a guest
Jan 1st, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. <?php
  2. namespace src\Integration;
  3.  
  4. class DataProvider
  5. {
  6. private $host;
  7. private $user;
  8. private $password;
  9.  
  10. /**
  11. * @param $host
  12. * @param $user
  13. * @param $password
  14. */
  15. public function __construct($host, $user, $password)
  16. {
  17. $this->host = $host;
  18. $this->user = $user;
  19. $this->password = $password;
  20. }
  21.  
  22. /**
  23. * @param array $request
  24. *
  25. * @return array
  26. */
  27. public function get(array $request)
  28. {
  29. // returns a response from external service
  30. }
  31. }
  32.  
  33. interface CacheInterface
  34. {
  35. public function get(array $input);
  36.  
  37. public function set(CacheItemInterface $cacheItem, array $value, \DateTime $expireAt = null);
  38. }
  39.  
  40. class Cache implements CacheInterface
  41. {
  42. private $cache;
  43.  
  44. public function __construct(CacheItemPoolInterface $cache)
  45. {
  46. $this->cache = $cache;
  47. }
  48.  
  49. protected function getCacheKey(array $input)
  50. {
  51. return json_encode($input);
  52. }
  53.  
  54. public function get(array $input)
  55. {
  56. $cacheKey = $this->getCacheKey($input);
  57. $cacheItem = $this->cache->getItem($cacheKey);
  58. return $cacheItem;
  59. }
  60.  
  61. public function set(CacheItemInterface $cacheItem, array $value, \DateTime $expireAt = null)
  62. {
  63. $cacheItem->set($value);
  64.  
  65. if (null !== $expiresAt) {
  66. $cacheItem->expiresAt($expiresAt);
  67. }
  68. }
  69. }
  70.  
  71. namespace src\Decorator;
  72.  
  73. interface ResponseInterface
  74. {
  75. public function getResponse(array $input);
  76. }
  77.  
  78. namespace src\Decorator;
  79.  
  80. use DateTime;
  81. use Exception;
  82. use Psr\Cache\CacheItemPoolInterface;
  83. use Psr\Log\LoggerInterface;
  84. use src\Integration\DataProvider;
  85.  
  86. class DecoratorManager implements ResponseInterface
  87. {
  88. private $cache;
  89. private $logger;
  90. private $provider;
  91.  
  92. */
  93. * @param CacheItemPoolInterface $cache
  94. */
  95. public function __construct(DataProvider $provider, LoggerInterface $logger)
  96. {
  97. $this->provider = $provider;
  98. $this->logger = $logger;
  99. }
  100.  
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getResponse(array $input)
  105. {
  106. try {
  107. $result = $this->provider->get($input);
  108.  
  109. return $result;
  110. } catch (Exception $e) {
  111. $this->logger->critical('Error');
  112. }
  113.  
  114. return [];
  115. }
  116. }
  117.  
  118. class CacheManager implements ResponseInterface
  119. {
  120. private $response;
  121. private $cache;
  122.  
  123. public function __construct(ResponseInterface $response, CacheInterface $cache)
  124. {
  125. $this->reponse = $response;
  126. $this->cache = $cache;
  127. }
  128.  
  129. public function getResponse(array $input)
  130. {
  131. $cacheItem = $this->cache->get($input);
  132.  
  133. if ($cacheItem->isHit()) {
  134. return $cacheItem->get();
  135. }
  136.  
  137. $result = $this->response->getResponse($input);
  138.  
  139. $this->cache
  140. ->set($cacheItem, $result, (new DateTime())->modify('+1 day'));
  141.  
  142. return $result;
  143. }
  144. }
  145.  
  146. $provider = new DataProvider($host, $user, $pass);
  147. $manager = new DecoratorManager($provider, $logger);
  148. $manager->getResponse($input);
  149. // или добавим кэш
  150. $cache = new Cache($cacheItemPool);
  151. $cacheManager = new CacheManager($manager, $cache);
  152. $cacheManager->getResponse($input);
Add Comment
Please, Sign In to add comment