Skip to content

Commit aa13448

Browse files
committed
Merge branch 'feature/issue-1'
2 parents c2afa32 + dc83fab commit aa13448

File tree

5 files changed

+134
-6
lines changed

5 files changed

+134
-6
lines changed

.phpunit.result.cache

-1
This file was deleted.

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"psr/http-factory": "^1.0"
2121
},
2222
"require-dev": {
23-
"cache/array-adapter": "^1.0",
2423
"guzzlehttp/psr7": "^1.7",
2524
"phpunit/phpunit": ">=6.5",
2625
"squizlabs/php_codesniffer": "^3.5",

phpunit.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php" colors="true">
2+
<phpunit bootstrap="vendor/autoload.php" colors="true" cacheResult="false">
33
<testsuites>
44
<testsuite name="all">
55
<directory>tests</directory>

stuff/ArrayCache.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Stuff\Webclient\Extension\Cache;
4+
5+
use DateInterval;
6+
use Psr\SimpleCache\CacheInterface;
7+
8+
class ArrayCache implements CacheInterface
9+
{
10+
11+
/**
12+
* @var array
13+
*/
14+
private $storage = [];
15+
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function get($key, $default = null)
20+
{
21+
if (!$this->has($key)) {
22+
return $default;
23+
}
24+
return $this->storage[$key]['value'];
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function set($key, $value, $ttl = null)
31+
{
32+
$item = [
33+
'value' => $value,
34+
];
35+
$seconds = is_int($ttl) ? $ttl : 0;
36+
if ($ttl instanceof DateInterval) {
37+
if ($ttl->invert === 1) {
38+
return false;
39+
}
40+
$seconds = $ttl->days * 86400 + $ttl->h * 3600 + $ttl->i * 60 + $ttl->s;
41+
}
42+
if ($seconds) {
43+
$item['expired'] = time() + $seconds;
44+
}
45+
$this->storage[$key] = $item;
46+
return true;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public function delete($key)
53+
{
54+
if (array_key_exists($key, $this->storage)) {
55+
unset($this->storage[$key]);
56+
}
57+
return true;
58+
}
59+
60+
/**
61+
* @inheritDoc
62+
*/
63+
public function clear()
64+
{
65+
$this->storage = [];
66+
return true;
67+
}
68+
69+
/**
70+
* @inheritDoc
71+
*/
72+
public function getMultiple($keys, $default = null)
73+
{
74+
$result = [];
75+
foreach ($keys as $key) {
76+
$result[$key] = $this->get($key, $default);
77+
}
78+
return $result;
79+
}
80+
81+
/**
82+
* @inheritDoc
83+
*/
84+
public function setMultiple($values, $ttl = null)
85+
{
86+
$result = true;
87+
foreach ($values as $key => $value) {
88+
if (!$this->set($key, $value, $ttl)) {
89+
$result = false;
90+
}
91+
}
92+
return $result;
93+
}
94+
95+
/**
96+
* @inheritDoc
97+
*/
98+
public function deleteMultiple($keys)
99+
{
100+
$result = true;
101+
foreach ($keys as $key) {
102+
if (!$this->delete($key)) {
103+
$result = false;
104+
}
105+
}
106+
return $result;
107+
}
108+
109+
/**
110+
* @inheritDoc
111+
*/
112+
public function has($key)
113+
{
114+
if (!array_key_exists($key, $this->storage)) {
115+
return false;
116+
}
117+
if (!array_key_exists('value', $this->storage[$key])) {
118+
unset($this->storage[$key]);
119+
return false;
120+
}
121+
$expired = false;
122+
if (array_key_exists('expired', $this->storage[$key])) {
123+
$expired = time() >= $this->storage[$key]['expired'];
124+
}
125+
if ($expired) {
126+
unset($this->storage[$key]);
127+
return false;
128+
}
129+
return true;
130+
}
131+
}

tests/ClientTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Tests\Webclient\Extension\Cache;
66

7-
use Cache\Adapter\PHPArray\ArrayCachePool;
87
use GuzzleHttp\Psr7\Request;
98
use PHPUnit\Framework\TestCase;
109
use Psr\Http\Client\ClientExceptionInterface;
10+
use Stuff\Webclient\Extension\Cache\ArrayCache;
1111
use Stuff\Webclient\Extension\Cache\Handler;
1212
use Stuff\Webclient\Extension\Cache\HttpFactory;
1313
use Webclient\Extension\Cache\Client;
@@ -21,9 +21,8 @@ class ClientTest extends TestCase
2121
*/
2222
public function testClient()
2323
{
24-
$items = [];
2524
$factory = new HttpFactory();
26-
$cache = new ArrayCachePool(null, $items);
25+
$cache = new ArrayCache();
2726
$client = new Client(
2827
new FakeClient(new Handler($factory, $factory)),
2928
$cache,

0 commit comments

Comments
 (0)