for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace MetroMarkets\FFBundle\Providers;
use GuzzleHttp\Client;
use MetroMarkets\FFBundle\Contract\ProviderInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
class RestProvider implements ProviderInterface
{
/** @var Client */
private $client;
/** @var string */
private $endpoint;
/** @var AdapterInterface */
private $cache;
/** @var int */
private $cacheTTL;
public function __construct(string $endpoint, AdapterInterface $cache = null, int $cacheTTL = 60)
$this->client = new Client();
$this->endpoint = $endpoint;
$this->cache = $cache;
$this->cacheTTL = $cacheTTL;
}
public function isEnabled(string $key, string $identifier = null): bool
if (!$this->cache) {
return $this->fetchData($key);
return $this->getCachedResponse($key);
return $this->getCachedResponse($key)
null
boolean
private function getCachedResponse(string $key)
$cacheItem = $this->cache->getItem($key);
if ($cacheItem->isHit()) {
return $cacheItem->get();
$result = $this->fetchData($key);
$cacheItem->set($result);
$cacheItem->expiresAfter($this->cacheTTL);
$this->cache->save($cacheItem);
private function fetchData(string $key)
$response = $this->client->get($this->endpoint . '?key=' . $key);
return \json_decode($response->getBody()->getContents(), true);