chadicus /
marvel-api-client
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Chadicus\Marvel\Api\Cache; |
||
| 4 | |||
| 5 | use Psr\Http\Message\RequestInterface; |
||
| 6 | use Psr\Http\Message\ResponseInterface; |
||
| 7 | use Zend\Diactoros\Request; |
||
| 8 | use Zend\Diactoros\Response; |
||
| 9 | use DominionEnterprises\Util\Arrays; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Concrete implementation of Cache using an array. |
||
| 13 | */ |
||
| 14 | final class ArrayCache extends AbstractCache implements CacheInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Array containing the cached responses. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $cache; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Construct a new instance of ArrayCache. |
||
| 25 | * |
||
| 26 | * @param integer $defaultTimeToLive The default time to live in seconds. |
||
| 27 | * |
||
| 28 | * @throws \InvalidArgumentException Throw if $defaultTimeToLive is not an integer between 0 and 86400. |
||
| 29 | */ |
||
| 30 | public function __construct(int $defaultTimeToLive = CacheInterface::MAX_TTL) |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 31 | { |
||
| 32 | $this->setDefaultTTL($defaultTimeToLive); |
||
| 33 | $this->cache = []; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Store the api $response as the cached result of the api $request. |
||
| 38 | * |
||
| 39 | * @param RequestInterface $request The request for which the response will be cached. |
||
| 40 | * @param ResponseInterface $response The reponse to cache. |
||
| 41 | * @param integer $timeToLive The time in seconds that the cache should live. |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | * |
||
| 45 | * @throws \InvalidArgumentException Throw if $timeToLive is not an integer between 0 and 86400. |
||
| 46 | */ |
||
| 47 | public function set(RequestInterface $request, ResponseInterface $response, int $timeToLive = null) |
||
|
0 ignored issues
–
show
|
|||
| 48 | { |
||
| 49 | $timeToLive = self::ensureTTL($timeToLive ?: $this->getDefaultTTL()); |
||
| 50 | |||
| 51 | $this->cache[(string)$request->getUri()] = ['response' => $response, 'expires' => time() + $timeToLive]; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Retrieve the cached results of the api $request. |
||
| 56 | * |
||
| 57 | * @param RequestInterface $request A request for which the response may be cached. |
||
| 58 | * |
||
| 59 | * @return ResponseInterface|null |
||
| 60 | */ |
||
| 61 | public function get(RequestInterface $request) |
||
| 62 | { |
||
| 63 | $id = (string)$request->getUri(); |
||
| 64 | $cache = Arrays::get($this->cache, $id); |
||
| 65 | if ($cache === null) { |
||
| 66 | return null; |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($cache['expires'] >= time()) { |
||
| 70 | return $cache['response']; |
||
| 71 | } |
||
| 72 | |||
| 73 | unset($this->cache[$id]); |
||
| 74 | return null; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Clears this cache. |
||
| 79 | * |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public function clear() |
||
| 83 | { |
||
| 84 | $this->cache = []; |
||
| 85 | } |
||
| 86 | } |
||
| 87 |