Created
August 7, 2019 13:48
-
-
Save DenSul/27e66ac28a4006758dc206b0605fdb5e to your computer and use it in GitHub Desktop.
skyeng review
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
use src\Integration\DataProviderInterface; | |
use Psr\Cache\CacheItemPoolInterface; | |
class CacheDecorator implements DataProviderInterface | |
{ | |
/** | |
* @var DataProviderInterface | |
*/ | |
protected $instance; | |
/** | |
* @var CacheItemPoolInterface | |
*/ | |
protected $cache; | |
/** | |
* CacheDecorator constructor. | |
* | |
* @param DataProviderInterface $instance | |
* @param CacheItemPoolInterface $cache | |
*/ | |
public function __construct(DataProviderInterface $instance, CacheItemPoolInterface $cache) | |
{ | |
$this->instance = $instance; | |
$this->cache = $cache; | |
} | |
public function get(array $input): ?array | |
{ | |
$result = null; | |
$cacheKey = $this->getCacheKey($input); | |
$cacheItem = $this->cache->getItem($cacheKey); | |
if ($cacheItem->isHit()) { | |
return $cacheItem->get(); | |
} | |
$result = $this->instance->get($input); | |
$cacheItem | |
->set($result) | |
->expiresAt( | |
(new DateTime)->modify('+1 day') | |
); | |
$this->cache->save($cacheItem); | |
return $result; | |
} | |
private function getCacheKey(array $input) | |
{ | |
ksort($input); | |
return md5(implode('.', $input)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
namespace src\Integration; | |
class DataProvider implements DataProviderInterface | |
{ | |
/** | |
* @var ClientInterface | |
*/ | |
private $client; | |
/** | |
* @var string | |
*/ | |
private $host; | |
/** | |
* @var string | |
*/ | |
private $user; | |
/** | |
* @var string | |
*/ | |
private $password; | |
/** | |
* @param $host | |
* @param $user | |
* @param $password | |
*/ | |
public function __construct(ClientInterface $client, string $host, string $user, string $password) | |
{ | |
$this->client = $client; | |
$this->host = $host; | |
$this->user = $user; | |
$this->password = $password; | |
} | |
/** | |
* @param array $request | |
* | |
* @return array | |
*/ | |
public function get(array $request): array | |
{ | |
// returns a response from external service | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
namespace src\Integration; | |
interface DataProviderInterface | |
{ | |
public function get(array $request): ?array; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
use src\Integration\DataProviderInterface; | |
use Psr\Log\LoggerInterface; | |
use Psr\Log\LoggerAwareTrait; | |
class LoggerDecorator implements DataProviderInterface | |
{ | |
use LoggerAwareTrait; | |
/** | |
* @var DataProviderInterface | |
*/ | |
private $instance; | |
/** | |
* @var LoggerInterface | |
*/ | |
private $logger; | |
/** | |
* LoggerDecorator constructor. | |
* | |
* @param DataProviderInterface $instance | |
* @param LoggerInterface $logger | |
*/ | |
public function __construct(DataProviderInterface $instance, LoggerInterface $logger) | |
{ | |
$this->instance = $instance; | |
$this->logger = $logger; | |
} | |
public function get(array $request): ?array | |
{ | |
try { | |
$result = $this->instance->get($request); | |
} catch (Exception $e) { | |
$this->logger->critical('Error'); | |
} catch (RuntimeException $e) { | |
$this->logger->critical('Error'); | |
} //etc catches | |
return $result ?? null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment