Skip to content

Instantly share code, notes, and snippets.

@miholeus
Created December 27, 2017 07:58
Show Gist options
  • Save miholeus/8acdfc072c79ff9a9d22748070676a53 to your computer and use it in GitHub Desktop.
Save miholeus/8acdfc072c79ff9a9d22748070676a53 to your computer and use it in GitHub Desktop.
data integration example v2
<?php
namespace src\Integration;
class DataProvider
{
private $host;
private $user;
private $password;
/**
* @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)
{
// returns a response from external service
}
}
interface CacheInterface
{
public function get(array $input);
public function set(CacheItemInterface $cacheItem, array $value, \DateTime $expireAt = null);
}
class Cache implements CacheInterface
{
private $cache;
public function __construct(CacheItemPoolInterface $cache)
{
$this->cache = $cache;
}
protected function getCacheKey(array $input)
{
return json_encode($input);
}
public function get(array $input)
{
$cacheKey = $this->getCacheKey($input);
$cacheItem = $this->cache->getItem($cacheKey);
return $cacheItem;
}
public function set(CacheItemInterface $cacheItem, array $value, \DateTime $expireAt = null)
{
$cacheItem->set($value);
if (null !== $expiresAt) {
$cacheItem->expiresAt($expiresAt);
}
}
}
namespace src\Decorator;
interface ResponseInterface
{
public function getResponse(array $input);
}
namespace src\Decorator;
use DateTime;
use Exception;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use src\Integration\DataProvider;
class DecoratorManager implements ResponseInterface
{
private $cache;
private $logger;
private $provider;
*/
* @param CacheItemPoolInterface $cache
*/
public function __construct(DataProvider $provider, LoggerInterface $logger)
{
$this->provider = $provider;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function getResponse(array $input)
{
try {
$result = $this->provider->get($input);
return $result;
} catch (Exception $e) {
$this->logger->critical('Error');
}
return [];
}
}
class CacheManager implements ResponseInterface
{
private $response;
private $cache;
public function __construct(ResponseInterface $response, CacheInterface $cache)
{
$this->reponse = $response;
$this->cache = $cache;
}
public function getResponse(array $input)
{
$cacheItem = $this->cache->get($input);
if ($cacheItem->isHit()) {
return $cacheItem->get();
}
$result = $this->response->getResponse($input);
$this->cache
->set($cacheItem, $result, (new DateTime())->modify('+1 day'));
return $result;
}
}
$provider = new DataProvider($host, $user, $pass);
$manager = new DecoratorManager($provider, $logger);
$manager->getResponse($input);
// или добавим кэш
$cache = new Cache($cacheItemPool);
$cacheManager = new CacheManager($manager, $cache);
$cacheManager->getResponse($input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment