| Total Complexity | 6 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class RestProvider implements ProviderInterface |
||
| 10 | { |
||
| 11 | /** @var Client */ |
||
| 12 | private $client; |
||
| 13 | |||
| 14 | /** @var string */ |
||
| 15 | private $endpoint; |
||
| 16 | |||
| 17 | /** @var AdapterInterface */ |
||
| 18 | private $cache; |
||
| 19 | |||
| 20 | /** @var int */ |
||
| 21 | private $cacheTTL; |
||
| 22 | |||
| 23 | public function __construct(string $endpoint, AdapterInterface $cache = null, int $cacheTTL = 60) |
||
| 24 | { |
||
| 25 | $this->client = new Client(); |
||
| 26 | $this->endpoint = $endpoint; |
||
| 27 | $this->cache = $cache; |
||
| 28 | $this->cacheTTL = $cacheTTL; |
||
| 29 | } |
||
| 30 | |||
| 31 | public function isEnabled(string $key, string $identifier = null): bool |
||
| 32 | { |
||
| 33 | if (!$this->cache) { |
||
| 34 | return $this->fetchData($key); |
||
| 35 | } |
||
| 36 | |||
| 37 | return $this->getCachedResponse($key); |
||
|
|
|||
| 38 | } |
||
| 39 | |||
| 40 | private function getCachedResponse(string $key) |
||
| 41 | { |
||
| 42 | $cacheItem = $this->cache->getItem($key); |
||
| 43 | |||
| 44 | if ($cacheItem->isHit()) { |
||
| 45 | return $cacheItem->get(); |
||
| 46 | } |
||
| 47 | |||
| 48 | $result = $this->fetchData($key); |
||
| 49 | |||
| 50 | $cacheItem->set($result); |
||
| 51 | $cacheItem->expiresAfter($this->cacheTTL); |
||
| 52 | $this->cache->save($cacheItem); |
||
| 53 | |||
| 54 | } |
||
| 55 | |||
| 56 | private function fetchData(string $key) |
||
| 61 | } |
||
| 62 | } |