@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * @param DateInterval $ttl |
42 | 42 | * @return CacheStorageInterface |
43 | 43 | */ |
44 | - public function withTtl(DateInterval $ttl){ |
|
44 | + public function withTtl(DateInterval $ttl) { |
|
45 | 45 | $new = clone $this; |
46 | 46 | $new->ttl = $ttl; |
47 | 47 | return $new; |
@@ -53,11 +53,11 @@ discard block |
||
53 | 53 | * @param null|int $ttl |
54 | 54 | * @return int |
55 | 55 | */ |
56 | - public function getTtlTimestamp($ttl = null){ |
|
56 | + public function getTtlTimestamp($ttl = null) { |
|
57 | 57 | |
58 | - if (null === $ttl){ |
|
58 | + if (null === $ttl) { |
|
59 | 59 | $dateTime = new DateTime(); |
60 | - $dateTime->add( $this->ttl ); |
|
60 | + $dateTime->add($this->ttl); |
|
61 | 61 | $ttl = $dateTime->getTimestamp(); |
62 | 62 | } |
63 | 63 | |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return DateInterval |
71 | 71 | */ |
72 | - public function getTtl(){ |
|
72 | + public function getTtl() { |
|
73 | 73 | return $this->ttl; |
74 | 74 | } |
75 | 75 |
@@ -38,13 +38,13 @@ discard block |
||
38 | 38 | public function set($key, $value, $ttl = null) |
39 | 39 | { |
40 | 40 | //convert to timestamp from now. |
41 | - if ($ttl instanceof \DateInterval){ |
|
41 | + if ($ttl instanceof \DateInterval) { |
|
42 | 42 | $dateTime = new \DateTime(); |
43 | - $dateTime->add( $ttl ); |
|
43 | + $dateTime->add($ttl); |
|
44 | 44 | $ttl = $dateTime->getTimestamp() - time(); |
45 | 45 | } |
46 | 46 | |
47 | - if (!is_int($ttl) || $ttl === null){ |
|
47 | + if (!is_int($ttl) || $ttl === null) { |
|
48 | 48 | throw new InvalidArgumentException('$ttl can only be an instance of \DateInterval, int or null'); |
49 | 49 | } |
50 | 50 | |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | { |
88 | 88 | $this->_isValidKey($key); |
89 | 89 | |
90 | - if ($this->cache->has($key)){ |
|
90 | + if ($this->cache->has($key)) { |
|
91 | 91 | return $this->cache->get($key); |
92 | 92 | } |
93 | 93 | |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | $this->_checkTraversable($keys); |
129 | 129 | $result = []; |
130 | 130 | |
131 | - foreach ((array)$keys as $key){ |
|
131 | + foreach ((array)$keys as $key) { |
|
132 | 132 | $cachedItem = $this->cache->get($key); |
133 | 133 | $result[$key] = (null !== $cachedItem) ? $cachedItem : $default; |
134 | 134 | } |
@@ -155,11 +155,11 @@ discard block |
||
155 | 155 | $this->_checkTraversable($values); |
156 | 156 | $results = []; |
157 | 157 | |
158 | - foreach ((array)$values as $key => $value){ |
|
158 | + foreach ((array)$values as $key => $value) { |
|
159 | 159 | $results[] = $this->cache->set($key, $value, $ttl); |
160 | 160 | } |
161 | 161 | |
162 | - if ($this->_hasFailure($results)){ |
|
162 | + if ($this->_hasFailure($results)) { |
|
163 | 163 | return false; |
164 | 164 | } |
165 | 165 | |
@@ -182,11 +182,11 @@ discard block |
||
182 | 182 | $this->_checkTraversable($keys); |
183 | 183 | $results = []; |
184 | 184 | |
185 | - foreach ((array)$keys as $key){ |
|
185 | + foreach ((array)$keys as $key) { |
|
186 | 186 | $results[] = $this->cache->delete($key); |
187 | 187 | } |
188 | 188 | |
189 | - if ($this->_hasFailure($results)){ |
|
189 | + if ($this->_hasFailure($results)) { |
|
190 | 190 | return false; |
191 | 191 | } |
192 | 192 | |
@@ -209,15 +209,15 @@ discard block |
||
209 | 209 | * @param $key |
210 | 210 | * @throws InvalidArgumentException |
211 | 211 | */ |
212 | - private function _isValidKey($key){ |
|
213 | - if (!is_string($key)){ |
|
212 | + private function _isValidKey($key) { |
|
213 | + if (!is_string($key)) { |
|
214 | 214 | throw new InvalidArgumentException('provided key must be a valid string'); |
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
218 | 218 | private function isAssoc(array $arr) |
219 | 219 | { |
220 | - if (array() === $arr){ |
|
220 | + if (array() === $arr) { |
|
221 | 221 | return false; |
222 | 222 | } |
223 | 223 | return array_keys($arr) !== range(0, count($arr) - 1); |
@@ -230,13 +230,13 @@ discard block |
||
230 | 230 | * @throws InvalidArgumentException |
231 | 231 | * @internal |
232 | 232 | */ |
233 | - private function _checkTraversable($data){ |
|
233 | + private function _checkTraversable($data) { |
|
234 | 234 | |
235 | - if (is_array($data)){ |
|
235 | + if (is_array($data)) { |
|
236 | 236 | return; |
237 | 237 | } |
238 | 238 | |
239 | - if ($data instanceof \Traversable ){ |
|
239 | + if ($data instanceof \Traversable) { |
|
240 | 240 | return; |
241 | 241 | } |
242 | 242 | |
@@ -250,8 +250,8 @@ discard block |
||
250 | 250 | * @return bool |
251 | 251 | * @internal |
252 | 252 | */ |
253 | - private function _hasFailure(array $results){ |
|
254 | - if (in_array(false, $results)){ |
|
253 | + private function _hasFailure(array $results) { |
|
254 | + if (in_array(false, $results)) { |
|
255 | 255 | return true; |
256 | 256 | } |
257 | 257 | return false; |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
25 | 25 | * |
26 | 26 | * @param string $key The key of the item to store. |
27 | - * @param mixed $value The value of the item to store, must be serializable. |
|
27 | + * @param string $value The value of the item to store, must be serializable. |
|
28 | 28 | * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
29 | 29 | * the driver supports TTL then the library may set a default value |
30 | 30 | * for it or let the driver take care of that. |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | * Fetches a value from the cache. |
76 | 76 | * |
77 | 77 | * @param string $key The unique key of this item in the cache. |
78 | - * @param mixed $default Default value to return if the key does not exist. |
|
78 | + * @param string $default Default value to return if the key does not exist. |
|
79 | 79 | * |
80 | 80 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
81 | 81 | * |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | /** |
114 | 114 | * Obtains multiple cache items by their unique keys. |
115 | 115 | * |
116 | - * @param \iterable $keys A list of keys that can obtained in a single operation. |
|
116 | + * @param string[] $keys A list of keys that can obtained in a single operation. |
|
117 | 117 | * @param mixed $default Default value to return for keys that do not exist. |
118 | 118 | * |
119 | 119 | * @return \iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
140 | 140 | * |
141 | 141 | * @param \iterable $values A list of key => value pairs for a multiple-set operation. |
142 | - * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
142 | + * @param integer $ttl Optional. The TTL value of this item. If no value is sent and |
|
143 | 143 | * the driver supports TTL then the library may set a default value |
144 | 144 | * for it or let the driver take care of that. |
145 | 145 | * |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | /** |
169 | 169 | * Deletes multiple cache items in a single operation. |
170 | 170 | * |
171 | - * @param \iterable $keys A list of string-based keys to be deleted. |
|
171 | + * @param string[] $keys A list of string-based keys to be deleted. |
|
172 | 172 | * |
173 | 173 | * @return bool True if the items were successfully removed. False if there was an error. |
174 | 174 | * |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | /** |
206 | 206 | * Check that provided key is valid. |
207 | 207 | * |
208 | - * @param $key |
|
208 | + * @param string $key |
|
209 | 209 | * @throws InvalidArgumentException |
210 | 210 | */ |
211 | 211 | private function _isValidKey($key){ |
@@ -86,7 +86,7 @@ |
||
86 | 86 | * Test that clear is called. |
87 | 87 | */ |
88 | 88 | public function testClear(){ |
89 | - $this->assertEquals($this->cacheStorage->clear(), true); |
|
89 | + $this->assertEquals($this->cacheStorage->clear(), true); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | /** |
@@ -29,8 +29,8 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function setUp() |
31 | 31 | { |
32 | - $this->testDir = __DIR__ . '/cacheTest'; |
|
33 | - $this->cacheStorage = new FileStorage( $this->testDir, new \DateInterval('P1M')); |
|
32 | + $this->testDir = __DIR__.'/cacheTest'; |
|
33 | + $this->cacheStorage = new FileStorage($this->testDir, new \DateInterval('P1M')); |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | /** |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | /** |
46 | 46 | * Test that CacheException is thrown if directory is not valid/writable. |
47 | 47 | */ |
48 | - public function testUnwritableDirectory(){ |
|
48 | + public function testUnwritableDirectory() { |
|
49 | 49 | |
50 | 50 | $this->expectException(CacheException::class); |
51 | 51 | $this->cacheStorage = new FileStorage('php://memory', new \DateInterval('P1M')); |
@@ -54,67 +54,67 @@ discard block |
||
54 | 54 | /** |
55 | 55 | * Test that set is called. |
56 | 56 | */ |
57 | - public function testSet(){ |
|
58 | - $actual = $this->cacheStorage->set('key','value',60*60); |
|
57 | + public function testSet() { |
|
58 | + $actual = $this->cacheStorage->set('key', 'value', 60 * 60); |
|
59 | 59 | $this->assertEquals(true, $actual); |
60 | 60 | } |
61 | 61 | |
62 | 62 | /** |
63 | 63 | * Test has when not value is found. |
64 | 64 | */ |
65 | - public function testHasNoValue(){ |
|
65 | + public function testHasNoValue() { |
|
66 | 66 | $this->assertEquals($this->cacheStorage->has('someRandomKey'), false); |
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
70 | 70 | * Test has when value is found. |
71 | 71 | */ |
72 | - public function testHas(){ |
|
73 | - $this->cacheStorage->set('foo','bar'); |
|
72 | + public function testHas() { |
|
73 | + $this->cacheStorage->set('foo', 'bar'); |
|
74 | 74 | $this->assertEquals($this->cacheStorage->has('foo'), true); |
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
78 | 78 | * Test has when value is found but has expired. |
79 | 79 | */ |
80 | - public function testHasExpired(){ |
|
81 | - $this->cacheStorage->set('expired','bar', -1200); |
|
80 | + public function testHasExpired() { |
|
81 | + $this->cacheStorage->set('expired', 'bar', -1200); |
|
82 | 82 | $this->assertEquals($this->cacheStorage->has('expired'), false); |
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
86 | 86 | * Test that clear is called. |
87 | 87 | */ |
88 | - public function testClear(){ |
|
88 | + public function testClear() { |
|
89 | 89 | $this->assertEquals($this->cacheStorage->clear(), true); |
90 | 90 | } |
91 | 91 | |
92 | 92 | /** |
93 | 93 | * Test that get returns a found key. |
94 | 94 | */ |
95 | - public function testGet(){ |
|
95 | + public function testGet() { |
|
96 | 96 | $expected = 'bat'; |
97 | - $this->cacheStorage->set('baz',$expected); |
|
97 | + $this->cacheStorage->set('baz', $expected); |
|
98 | 98 | $actual = $this->cacheStorage->get('baz'); |
99 | 99 | $this->assertEquals($expected, $actual); |
100 | 100 | } |
101 | 101 | |
102 | - public function testDelete(){ |
|
102 | + public function testDelete() { |
|
103 | 103 | $this->cacheStorage->delete('baz'); |
104 | 104 | } |
105 | 105 | |
106 | - public function testCreateCacheItem(){ |
|
106 | + public function testCreateCacheItem() { |
|
107 | 107 | |
108 | 108 | $reflector = new \ReflectionClass(FileStorage::class); |
109 | 109 | $method = $reflector->getMethod('_createCacheItem'); |
110 | 110 | $method->setAccessible(true); |
111 | 111 | |
112 | - $key= 'my-key_+'; |
|
112 | + $key = 'my-key_+'; |
|
113 | 113 | $value = 'my-value'; |
114 | 114 | $ttl = 60 * 60 * 7; |
115 | 115 | |
116 | 116 | $expected = [ |
117 | - 'file' => $this->testDir . DIRECTORY_SEPARATOR . implode('.',[$key, FileStorage::EXTENSION]), |
|
117 | + 'file' => $this->testDir.DIRECTORY_SEPARATOR.implode('.', [$key, FileStorage::EXTENSION]), |
|
118 | 118 | 'data' => json_encode([$ttl, $value]) |
119 | 119 | ]; |
120 | 120 | |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | $this->assertEquals($expected, $actual); |
123 | 123 | } |
124 | 124 | |
125 | - public function testCreateCacheItemInvalidKey(){ |
|
125 | + public function testCreateCacheItemInvalidKey() { |
|
126 | 126 | |
127 | 127 | $this->expectException(InvalidArgumentException::class); |
128 | 128 | $reflector = new \ReflectionClass(FileStorage::class); |
@@ -134,18 +134,18 @@ discard block |
||
134 | 134 | $method->invokeArgs($this->cacheStorage, [$key, $value, $ttl]); |
135 | 135 | } |
136 | 136 | |
137 | - public function testFetchCacheNoItem(){ |
|
137 | + public function testFetchCacheNoItem() { |
|
138 | 138 | $reflector = new \ReflectionClass(FileStorage::class); |
139 | 139 | $method = $reflector->getMethod('_fetchCacheFile'); |
140 | 140 | $method->setAccessible(true); |
141 | - $key= 'unknown'; |
|
141 | + $key = 'unknown'; |
|
142 | 142 | $actual = $method->invokeArgs($this->cacheStorage, [$key]); |
143 | 143 | $this->assertEquals(false, $actual); |
144 | 144 | } |
145 | 145 | |
146 | - public function testFetchCacheValid(){ |
|
146 | + public function testFetchCacheValid() { |
|
147 | 147 | |
148 | - $key= 'my-item'; |
|
148 | + $key = 'my-item'; |
|
149 | 149 | $ttl = 60 * 60; |
150 | 150 | $value = 'some-value'; |
151 | 151 | |
@@ -163,8 +163,8 @@ discard block |
||
163 | 163 | $this->assertEquals($expected, $actual); |
164 | 164 | } |
165 | 165 | |
166 | - public function testFetchCacheExpired(){ |
|
167 | - $key= 'my-item'; |
|
166 | + public function testFetchCacheExpired() { |
|
167 | + $key = 'my-item'; |
|
168 | 168 | $ttl = -1500; |
169 | 169 | $value = 'some-value'; |
170 | 170 | $this->cacheStorage->set($key, $value, $ttl); |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | { |
183 | 183 | $dir = $this->testDir; |
184 | 184 | |
185 | - if (is_dir( $dir)){ |
|
185 | + if (is_dir($dir)) { |
|
186 | 186 | array_map('unlink', glob("$dir/*.*")); |
187 | 187 | } |
188 | 188 | \rmdir($dir); |
@@ -99,10 +99,10 @@ |
||
99 | 99 | * @return bool |
100 | 100 | */ |
101 | 101 | public function has($key){ |
102 | - if ($this->_fetchCacheFile($key)){ |
|
103 | - return true; |
|
104 | - } |
|
105 | - return false; |
|
102 | + if ($this->_fetchCacheFile($key)){ |
|
103 | + return true; |
|
104 | + } |
|
105 | + return false; |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -45,13 +45,13 @@ discard block |
||
45 | 45 | */ |
46 | 46 | public function __construct($directory, DateInterval $ttl) |
47 | 47 | { |
48 | - if (!is_string($directory)){ |
|
48 | + if (!is_string($directory)) { |
|
49 | 49 | throw new InvalidArgumentException('Directory must be a valid string'); |
50 | 50 | } |
51 | 51 | |
52 | 52 | $this->dir = $directory; |
53 | 53 | |
54 | - if(!@mkdir($this->dir, 0755) && !is_dir($this->dir)){ |
|
54 | + if (!@mkdir($this->dir, 0755) && !is_dir($this->dir)) { |
|
55 | 55 | throw new CacheException('unable to create directory'); |
56 | 56 | } |
57 | 57 | |
@@ -75,9 +75,9 @@ discard block |
||
75 | 75 | $ttl = time() + $this->getTtlTimestamp($ttl); |
76 | 76 | $cacheItem = $this->_createCacheItem($key, $value, $ttl); |
77 | 77 | |
78 | - if (null !== $value){ |
|
79 | - \file_put_contents($cacheItem['file'],$cacheItem['data']); |
|
80 | - if (!file_exists($cacheItem['file'])){ |
|
78 | + if (null !== $value) { |
|
79 | + \file_put_contents($cacheItem['file'], $cacheItem['data']); |
|
80 | + if (!file_exists($cacheItem['file'])) { |
|
81 | 81 | return false; |
82 | 82 | } |
83 | 83 | } |
@@ -98,8 +98,8 @@ discard block |
||
98 | 98 | * |
99 | 99 | * @return bool |
100 | 100 | */ |
101 | - public function has($key){ |
|
102 | - if ($this->_fetchCacheFile($key)){ |
|
101 | + public function has($key) { |
|
102 | + if ($this->_fetchCacheFile($key)) { |
|
103 | 103 | return true; |
104 | 104 | } |
105 | 105 | return false; |
@@ -113,8 +113,8 @@ discard block |
||
113 | 113 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
114 | 114 | * |
115 | 115 | */ |
116 | - public function get($key){ |
|
117 | - if ($this->has($key)){ |
|
116 | + public function get($key) { |
|
117 | + if ($this->has($key)) { |
|
118 | 118 | return $this->previousRequest[1]; |
119 | 119 | } |
120 | 120 | return null; |
@@ -137,14 +137,14 @@ discard block |
||
137 | 137 | * |
138 | 138 | * @return bool True on success and false on failure. |
139 | 139 | */ |
140 | - public function clear(){ |
|
141 | - if (is_dir( $this->dir )){ |
|
140 | + public function clear() { |
|
141 | + if (is_dir($this->dir)) { |
|
142 | 142 | array_map('unlink', glob("$this->dir/*.*")); |
143 | 143 | } |
144 | 144 | |
145 | 145 | $data = glob("$this->dir/*.*"); |
146 | 146 | |
147 | - if (!empty($data)){ |
|
147 | + if (!empty($data)) { |
|
148 | 148 | return false; |
149 | 149 | } |
150 | 150 | |
@@ -158,20 +158,20 @@ discard block |
||
158 | 158 | * @return bool|mixed |
159 | 159 | * @internal |
160 | 160 | */ |
161 | - private function _fetchCacheFile($key){ |
|
161 | + private function _fetchCacheFile($key) { |
|
162 | 162 | |
163 | - $cacheFile = implode('.',[$key, FileStorage::EXTENSION]); |
|
164 | - $cacheLocation = $this->dir . DIRECTORY_SEPARATOR . $cacheFile; |
|
163 | + $cacheFile = implode('.', [$key, FileStorage::EXTENSION]); |
|
164 | + $cacheLocation = $this->dir.DIRECTORY_SEPARATOR.$cacheFile; |
|
165 | 165 | $this->previousRequest = false; |
166 | 166 | |
167 | - if (file_exists($cacheLocation)){ |
|
167 | + if (file_exists($cacheLocation)) { |
|
168 | 168 | |
169 | 169 | $data = \file_get_contents($cacheLocation); |
170 | 170 | $cacheData = json_decode($data, TRUE); |
171 | 171 | $this->previousRequest = $cacheData; |
172 | 172 | |
173 | 173 | //cache has expired |
174 | - if ( $cacheData[0] < time() ){ |
|
174 | + if ($cacheData[0] < time()) { |
|
175 | 175 | \unlink($cacheLocation); |
176 | 176 | $this->previousRequest = false; |
177 | 177 | } |
@@ -188,16 +188,16 @@ discard block |
||
188 | 188 | * @return array |
189 | 189 | * @throws InvalidArgumentException |
190 | 190 | */ |
191 | - private function _createCacheItem($key, $value, $ttl){ |
|
191 | + private function _createCacheItem($key, $value, $ttl) { |
|
192 | 192 | |
193 | - if (preg_match('/[\\/\\*\\\\\?.]+/i', $key)){ |
|
193 | + if (preg_match('/[\\/\\*\\\\\?.]+/i', $key)) { |
|
194 | 194 | throw new InvalidArgumentException('$key must be a valid string'); |
195 | 195 | } |
196 | 196 | |
197 | - $filename = implode('.',[(string)$key, FileStorage::EXTENSION]); |
|
197 | + $filename = implode('.', [(string)$key, FileStorage::EXTENSION]); |
|
198 | 198 | |
199 | 199 | return [ |
200 | - 'file' => $this->dir . DIRECTORY_SEPARATOR . $filename, |
|
200 | + 'file' => $this->dir.DIRECTORY_SEPARATOR.$filename, |
|
201 | 201 | 'data' => json_encode([$ttl, $value]) |
202 | 202 | ]; |
203 | 203 | } |