Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace src\Integration;
- class DataProvider
- {
- private $host;
- private $user;
- private $password;
- /**
- * TODO-R: типы параметров
- * @param $host
- * @param $user
- * @param $password
- */
- public function __construct($host, $user, $password)
- {
- $this->host = $host;
- $this->user = $user;
- $this->password = $password;
- }
- /**
- * @param array $request
- * @return array
- */
- public function get(array $request) // TODO-R: :array
- {
- // returns a response from external service
- }
- }
- <?php
- namespace src\Decorator;
- use DateTime;
- use Exception;
- use Psr\Cache\CacheItemPoolInterface;
- use Psr\Log\LoggerInterface;
- use src\Integration\DataProvider; // TODO-R: неймспейсы желательно привемсти к единому виду
- /**
- * TODO-R: Слово decorator уже есть в namespace
- * И это не является декоратором.
- * Паттерн декоратор реализуется по другому.
- *
- */
- class DecoratorManager extends DataProvider
- {
- // TODO-R: не должны быть public.
- public $cache;
- public $logger;
- /**
- * @param string $host
- * @param string $user
- * @param string $password
- * @param CacheItemPoolInterface $cache
- * TODO-R: почему кеш задается в конструкторе? Либо сеттер, аналогично логгеру, либо вообще DI использовать и получать заранее настоенные объекты
- */
- public function __construct($host, $user, $password, CacheItemPoolInterface $cache)
- {
- parent::__construct($host, $user, $password);
- $this->cache = $cache;
- }
- public function setLogger(LoggerInterface $logger)
- {
- $this->logger = $logger;
- }
- /**
- * {@inheritdoc}
- */
- public function getResponse(array $input)
- {
- try {
- $cacheKey = $this->getCacheKey($input);
- $cacheItem = $this->cache->getItem($cacheKey);
- if ($cacheItem->isHit()) {
- return $cacheItem->get();
- }
- $result = parent::get($input);
- $cacheItem
- ->set($result)
- ->expiresAt(
- (new DateTime())->modify('+1 day') // TODO-R: обычно TTL устанавливается в секундах.
- );
- return $result;
- } catch (Exception $e) {
- // TODO-R: хотелось бы сообщение от ошибке и трейс
- $this->logger->critical('Error');
- }
- return [];
- }
- public function getCacheKey(array $input)
- {
- // TODO-R: нет контроля длины ключа. Лучше добавить хеширование. В memcache можно получить ошибку и не записать данные
- return json_encode($input);
- }
- }
Add Comment
Please, Sign In to add comment