Guest User

Untitled

a guest
Jun 29th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 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. * TODO-R: типы параметров
  12. * @param $host
  13. * @param $user
  14. * @param $password
  15. */
  16. public function __construct($host, $user, $password)
  17. {
  18. $this->host = $host;
  19. $this->user = $user;
  20. $this->password = $password;
  21. }
  22.  
  23. /**
  24. * @param array $request
  25. * @return array
  26. */
  27. public function get(array $request) // TODO-R: :array
  28. {
  29. // returns a response from external service
  30. }
  31. }
  32.  
  33. <?php
  34.  
  35. namespace src\Decorator;
  36.  
  37. use DateTime;
  38. use Exception;
  39. use Psr\Cache\CacheItemPoolInterface;
  40. use Psr\Log\LoggerInterface;
  41. use src\Integration\DataProvider; // TODO-R: неймспейсы желательно привемсти к единому виду
  42.  
  43. /**
  44. * TODO-R: Слово decorator уже есть в namespace
  45. * И это не является декоратором.
  46. * Паттерн декоратор реализуется по другому.
  47. *
  48. */
  49. class DecoratorManager extends DataProvider
  50. {
  51. // TODO-R: не должны быть public.
  52. public $cache;
  53. public $logger;
  54.  
  55. /**
  56. * @param string $host
  57. * @param string $user
  58. * @param string $password
  59. * @param CacheItemPoolInterface $cache
  60. * TODO-R: почему кеш задается в конструкторе? Либо сеттер, аналогично логгеру, либо вообще DI использовать и получать заранее настоенные объекты
  61. */
  62. public function __construct($host, $user, $password, CacheItemPoolInterface $cache)
  63. {
  64. parent::__construct($host, $user, $password);
  65. $this->cache = $cache;
  66. }
  67.  
  68. public function setLogger(LoggerInterface $logger)
  69. {
  70. $this->logger = $logger;
  71. }
  72.  
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getResponse(array $input)
  77. {
  78. try {
  79. $cacheKey = $this->getCacheKey($input);
  80. $cacheItem = $this->cache->getItem($cacheKey);
  81. if ($cacheItem->isHit()) {
  82. return $cacheItem->get();
  83. }
  84.  
  85. $result = parent::get($input);
  86.  
  87. $cacheItem
  88. ->set($result)
  89. ->expiresAt(
  90. (new DateTime())->modify('+1 day') // TODO-R: обычно TTL устанавливается в секундах.
  91. );
  92.  
  93. return $result;
  94. } catch (Exception $e) {
  95. // TODO-R: хотелось бы сообщение от ошибке и трейс
  96. $this->logger->critical('Error');
  97. }
  98.  
  99. return [];
  100. }
  101.  
  102. public function getCacheKey(array $input)
  103. {
  104. // TODO-R: нет контроля длины ключа. Лучше добавить хеширование. В memcache можно получить ошибку и не записать данные
  105. return json_encode($input);
  106. }
  107. }
Add Comment
Please, Sign In to add comment