Advertisement
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;
- public $cache;
- public $logger;
- /**
- * @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)
- {
- try {
- $cacheKey = $this->getCacheKey($input);
- $cacheItem = $this->cache->getItem($cacheKey);
- if ($cacheItem->isHit()) {
- return $cacheItem->get();
- }
- $result = [];// returns a response from external service
- $cacheItem
- ->set($result)
- ->expiresAt(
- (new DateTime())->modify('+1 day')
- );
- return $result;
- } catch (Exception $e) {
- $this->logger->critical('Error');
- }
- return [];
- }
- public function setLogger(LoggerInterface $logger)
- {
- $this->logger = $logger;
- }
- public function getCacheKey(array $input)
- {
- return json_encode($input);
- }
- }
- <?php
- namespace src\Decorator;
- use DateTime;
- use Exception;
- use Psr\Cache\CacheItemPoolInterface;
- use Psr\Log\LoggerInterface;
- use src\Integration\DataProvider;
- class DecoratorManager
- {
- public $data;
- /**
- * @param string $host
- * @param string $user
- * @param string $password
- */
- public function __construct(Data $data)
- {
- $this->data = $data;
- }
- /**
- * {@inheritdoc}
- */
- public function getResponse()
- {
- return array_map(function ($item) {
- return $item;
- }, $this->data);
- }
- }
- $dataProvider = new DataProvider('host', 'user', 'password');
- $data = $dataProvider->get($request);
- $dataDecorator = new DecoratorManager($data);
- $decoratedData = $dataDecorator->getResponse();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement