Passed
Pull Request — main (#2)
by Sílvio
09:07 queued 06:24
created
src/CacheStore/ArrayCacheStore.php 2 patches
Indentation   +128 added lines, -128 removed lines patch added patch discarded remove patch
@@ -13,10 +13,10 @@  discard block
 block discarded – undo
13 13
 class ArrayCacheStore implements CacheerInterface
14 14
 {
15 15
 
16
-  /**
17
-  * @param array $arrayStore
18
-  */
19
-  private array $arrayStore = [];
16
+    /**
17
+     * @param array $arrayStore
18
+     */
19
+    private array $arrayStore = [];
20 20
 
21 21
     /**
22 22
      * @param boolean
@@ -33,26 +33,26 @@  discard block
 block discarded – undo
33 33
      */
34 34
     private $logger = null;
35 35
 
36
-  public function __construct(string $logPath)
37
-  {
36
+    public function __construct(string $logPath)
37
+    {
38 38
     $this->logger = new CacheLogger($logPath);
39
-  }
39
+    }
40 40
 
41
-  /**
42
-  * @param string $cacheKey
43
-  * @param string $namespace
44
-  * @param int|string $ttl
45
-  * @return mixed
46
-  */
47
-  public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600)
48
-  {
41
+    /**
42
+     * @param string $cacheKey
43
+     * @param string $namespace
44
+     * @param int|string $ttl
45
+     * @return mixed
46
+     */
47
+    public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600)
48
+    {
49 49
 
50 50
     $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
51 51
 
52 52
     if (!$this->has($cacheKey, $namespace)) {
53
-      $this->setMessage("cacheData not found, does not exists or expired", false);
54
-      $this->logger->debug("{$this->getMessage()} from array driver.");
55
-      return false;
53
+        $this->setMessage("cacheData not found, does not exists or expired", false);
54
+        $this->logger->debug("{$this->getMessage()} from array driver.");
55
+        return false;
56 56
     }
57 57
 
58 58
     $cacheData = $this->arrayStore[$arrayStoreKey];
@@ -60,107 +60,107 @@  discard block
 block discarded – undo
60 60
     $now = time();
61 61
 
62 62
     if($expirationTime !== 0 && $now >= $expirationTime) {
63
-      list($np, $key) = explode(':', $arrayStoreKey);
64
-      $this->clearCache($key, $np);
65
-      $this->setMessage("cacheKey: {$key} has expired.", false);
66
-      $this->logger->debug("{$this->getMessage()} from array driver.");
67
-      return false;
63
+        list($np, $key) = explode(':', $arrayStoreKey);
64
+        $this->clearCache($key, $np);
65
+        $this->setMessage("cacheKey: {$key} has expired.", false);
66
+        $this->logger->debug("{$this->getMessage()} from array driver.");
67
+        return false;
68 68
     }
69 69
 
70 70
     $this->setMessage("Cache retrieved successfully", true);
71 71
     $this->logger->debug("{$this->getMessage()} from array driver.");
72 72
     return $this->serialize($cacheData['value'], false);
73
-  }
74
-
75
-  /**
76
-  * @param string $cacheKey
77
-  * @param mixed $cacheData
78
-  * @param string $namespace
79
-  * @param int|string $ttl
80
-  * @return bool
81
-  */
82
-  public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
83
-  {
73
+    }
74
+
75
+    /**
76
+     * @param string $cacheKey
77
+     * @param mixed $cacheData
78
+     * @param string $namespace
79
+     * @param int|string $ttl
80
+     * @return bool
81
+     */
82
+    public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
83
+    {
84 84
 
85 85
     $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
86 86
 
87 87
     $this->arrayStore[$arrayStoreKey] = [
88
-      'value' => serialize($cacheData),
89
-      'expirationTime' => time() + $ttl
88
+        'value' => serialize($cacheData),
89
+        'expirationTime' => time() + $ttl
90 90
     ];
91 91
 
92 92
     $this->setMessage("Cache stored successfully", true);
93 93
     $this->logger->debug("{$this->getMessage()} from Array driver.");
94 94
     return true;
95
-  }
95
+    }
96 96
 
97
-  /**
98
-  * @param array $items
99
-  * @param string $namespace
100
-  * @param int $batchSize
101
-  * @return void
102
-  */
103
-  public function putMany(array $items, string $namespace = '', int $batchSize = 100)
104
-  {
97
+    /**
98
+     * @param array $items
99
+     * @param string $namespace
100
+     * @param int $batchSize
101
+     * @return void
102
+     */
103
+    public function putMany(array $items, string $namespace = '', int $batchSize = 100)
104
+    {
105 105
 
106 106
     $chunks = array_chunk($items, $batchSize, true);
107 107
 
108 108
     foreach ($chunks as $chunk) {
109
-      foreach ($chunk as $key => $data) {
110
-          $this->putCache($data['cacheKey'], $data['cacheData'], $namespace);
109
+        foreach ($chunk as $key => $data) {
110
+            $this->putCache($data['cacheKey'], $data['cacheData'], $namespace);
111
+        }
111 112
         }
112
-      }
113 113
     $this->setMessage("{$this->getMessage()}", $this->isSuccess());
114 114
     $this->logger->debug("{$this->getMessage()} from Array driver.");
115
-  }
116
-
117
-  /**
118
-  * @param string $cacheKey
119
-  * @param mixed  $cacheData
120
-  * @param string $namespace
121
-  * @return bool
122
-  */
123
-  public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '')
124
-  {
125
-      $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
126
-
127
-      if (!$this->has($cacheKey, $namespace)) {
128
-          $this->setMessage("cacheData can't be appended, because doesn't exist or expired", false);
129
-          $this->logger->debug("{$this->getMessage()} from array driver.");
130
-          return false;
131
-      }
132
-
133
-      $currentValue = $this->arrayStore[$arrayStoreKey]['value'];
134
-      if (is_string($currentValue)) {
135
-          $currentValue = unserialize($currentValue);
136
-      }
137
-
138
-      $this->arrayStore[$arrayStoreKey]['value'] = serialize($cacheData);
139
-      $this->setMessage("Cache appended successfully", true);
140
-      return true;
141
-  }
142
-
143
-
144
-
145
-  /**
146
-  * @param string $cacheKey
147
-  * @param string $namespace
148
-  * @return bool
149
-  */
150
-  public function has(string $cacheKey, string $namespace = '')
151
-  {
115
+    }
116
+
117
+    /**
118
+     * @param string $cacheKey
119
+     * @param mixed  $cacheData
120
+     * @param string $namespace
121
+     * @return bool
122
+     */
123
+    public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '')
124
+    {
125
+        $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
126
+
127
+        if (!$this->has($cacheKey, $namespace)) {
128
+            $this->setMessage("cacheData can't be appended, because doesn't exist or expired", false);
129
+            $this->logger->debug("{$this->getMessage()} from array driver.");
130
+            return false;
131
+        }
132
+
133
+        $currentValue = $this->arrayStore[$arrayStoreKey]['value'];
134
+        if (is_string($currentValue)) {
135
+            $currentValue = unserialize($currentValue);
136
+        }
137
+
138
+        $this->arrayStore[$arrayStoreKey]['value'] = serialize($cacheData);
139
+        $this->setMessage("Cache appended successfully", true);
140
+        return true;
141
+    }
142
+
143
+
144
+
145
+    /**
146
+     * @param string $cacheKey
147
+     * @param string $namespace
148
+     * @return bool
149
+     */
150
+    public function has(string $cacheKey, string $namespace = '')
151
+    {
152 152
     $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
153 153
     return isset($this->arrayStore[$arrayStoreKey]) && time() < $this->arrayStore[$arrayStoreKey]['expirationTime'];
154
-  }
155
-
156
-  /**
157
-  * @param string $cacheKey
158
-  * @param int|string $ttl
159
-  * @param string $namespace
160
-  * @return void
161
-  */
162
-  public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '')
163
-  {
154
+    }
155
+
156
+    /**
157
+     * @param string $cacheKey
158
+     * @param int|string $ttl
159
+     * @param string $namespace
160
+     * @return void
161
+     */
162
+    public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '')
163
+    {
164 164
     $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
165 165
 
166 166
     if (isset($this->arrayStore[$arrayStoreKey])) {
@@ -168,33 +168,33 @@  discard block
 block discarded – undo
168 168
         $this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds;
169 169
         $this->setMessage("cacheKey: {$cacheKey} renewed successfully", true);
170 170
         $this->logger->debug("{$this->getMessage()} from array driver.");
171
-      }
172
-  }
171
+        }
172
+    }
173 173
   
174
-  /**
175
-  * @param string $cacheKey
176
-  * @param string $namespace
177
-  * @return void
178
-  */
179
-  public function clearCache(string $cacheKey, string $namespace = '')
180
-  {
174
+    /**
175
+     * @param string $cacheKey
176
+     * @param string $namespace
177
+     * @return void
178
+     */
179
+    public function clearCache(string $cacheKey, string $namespace = '')
180
+    {
181 181
     $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
182 182
     unset($this->arrayStore[$arrayStoreKey]);
183 183
     $this->setMessage("Cache cleared successfully", true);
184 184
     $this->logger->debug("{$this->getMessage()} from array driver.");
185 185
 
186
-  }
186
+    }
187 187
 
188
-  /**
189
-  * @return void
190
-  */
191
-  public function flushCache()
192
-  {
188
+    /**
189
+     * @return void
190
+     */
191
+    public function flushCache()
192
+    {
193 193
     unset($this->arrayStore);
194 194
     $this->arrayStore = [];
195 195
     $this->setMessage("Cache flushed successfully", true);
196 196
     $this->logger->debug("{$this->getMessage()} from array driver.");
197
-  }
197
+    }
198 198
     
199 199
     /**
200 200
      * @param string  $message
@@ -224,24 +224,24 @@  discard block
 block discarded – undo
224 224
         return $this->success;
225 225
     }
226 226
 
227
-  /**
228
-  * @param string $cacheKey
229
-  * @param string $namespace
230
-  * @return string
231
-  */
232
-  private function buildArrayKey(string $cacheKey, string $namespace = '')
233
-  {
227
+    /**
228
+     * @param string $cacheKey
229
+     * @param string $namespace
230
+     * @return string
231
+     */
232
+    private function buildArrayKey(string $cacheKey, string $namespace = '')
233
+    {
234 234
     return !empty($namespace) ? ($namespace . ':' . $cacheKey) : $cacheKey;
235
-  }
236
-
237
-  /**
238
-  * @param mixed $data
239
-  * @param bool $serialize
240
-  * @return mixed
241
-  */
242
-  private function serialize(mixed $data, bool $serialize = true)
243
-  {
235
+    }
236
+
237
+    /**
238
+     * @param mixed $data
239
+     * @param bool $serialize
240
+     * @return mixed
241
+     */
242
+    private function serialize(mixed $data, bool $serialize = true)
243
+    {
244 244
     return $serialize ? serialize($data) : unserialize($data);
245
-  }
245
+    }
246 246
 
247 247
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
   * @param int|string $ttl
45 45
   * @return mixed
46 46
   */
47
-  public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600)
47
+  public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600)
48 48
   {
49 49
 
50 50
     $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
     $expirationTime = $cacheData['expirationTime'] ?? 0;
60 60
     $now = time();
61 61
 
62
-    if($expirationTime !== 0 && $now >= $expirationTime) {
62
+    if ($expirationTime !== 0 && $now >= $expirationTime) {
63 63
       list($np, $key) = explode(':', $arrayStoreKey);
64 64
       $this->clearCache($key, $np);
65 65
       $this->setMessage("cacheKey: {$key} has expired.", false);
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
   * @param int|string $ttl
80 80
   * @return bool
81 81
   */
82
-  public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
82
+  public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int | string $ttl = 3600)
83 83
   {
84 84
 
85 85
     $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
@@ -159,12 +159,12 @@  discard block
 block discarded – undo
159 159
   * @param string $namespace
160 160
   * @return void
161 161
   */
162
-  public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '')
162
+  public function renewCache(string $cacheKey, int | string $ttl = 3600, string $namespace = '')
163 163
   {
164 164
     $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace);
165 165
 
166 166
     if (isset($this->arrayStore[$arrayStoreKey])) {
167
-        $ttlSeconds = is_numeric($ttl) ? (int) $ttl : strtotime($ttl) - time();
167
+        $ttlSeconds = is_numeric($ttl) ? (int)$ttl : strtotime($ttl) - time();
168 168
         $this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds;
169 169
         $this->setMessage("cacheKey: {$cacheKey} renewed successfully", true);
170 170
         $this->logger->debug("{$this->getMessage()} from array driver.");
Please login to merge, or discard this patch.
src/Utils/CacheDriver.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -65,8 +65,8 @@  discard block
 block discarded – undo
65 65
     }
66 66
 
67 67
     /**
68
-    * @return Cacheer
69
-    */
68
+     * @return Cacheer
69
+     */
70 70
     public function useArrayDriver()
71 71
     {
72 72
         $this->cacheer->cacheStore = new ArrayCacheStore($this->logPath);
@@ -92,14 +92,14 @@  discard block
 block discarded – undo
92 92
     }
93 93
 
94 94
     /**
95
-    * @param mixed $dirName
96
-    * @return bool
97
-    */
95
+     * @param mixed $dirName
96
+     * @return bool
97
+     */
98 98
     private function isDir(mixed $dirName)
99 99
     {
100
-      if (is_dir($dirName)) {
101
-          return true;
102
-      }
103
-      return mkdir($dirName, 0755, true);
100
+        if (is_dir($dirName)) {
101
+            return true;
102
+        }
103
+        return mkdir($dirName, 0755, true);
104 104
     }
105 105
 }
Please login to merge, or discard this patch.
tests/Unit/ArrayCacheStoreTest.php 2 patches
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -7,40 +7,40 @@  discard block
 block discarded – undo
7 7
 class ArrayCacheStoreTest extends TestCase
8 8
 {
9 9
 
10
-  private $cache;
10
+    private $cache;
11 11
 
12
-  protected function setUp(): void
13
-  {
12
+    protected function setUp(): void
13
+    {
14 14
     $this->cache = new Cacheer();
15 15
     $this->cache->setDriver()->useArrayDriver();
16 16
     $this->cache->setConfig()->setTimeZone('America/Toronto');
17
-  }
17
+    }
18 18
 
19
-  protected function tearDown(): void
20
-  {
19
+    protected function tearDown(): void
20
+    {
21 21
     $this->cache->flushCache();
22
-  }
22
+    }
23 23
 
24 24
     public function testUsingArrayDriverSetsProperInstance()
25
-  {
25
+    {
26 26
     $this->assertInstanceOf(ArrayCacheStore::class, $this->cache->cacheStore);
27
-  }
27
+    }
28 28
 
29
-  public function testPutAndGetCacheInArray()
30
-  {
31
-      $cacheKey = 'test_key';
32
-      $cacheData = ['name' => 'John Doe', 'email' => '[email protected]'];
29
+    public function testPutAndGetCacheInArray()
30
+    {
31
+        $cacheKey = 'test_key';
32
+        $cacheData = ['name' => 'John Doe', 'email' => '[email protected]'];
33 33
       
34
-      $this->cache->putCache($cacheKey, $cacheData, '', 3600);
34
+        $this->cache->putCache($cacheKey, $cacheData, '', 3600);
35 35
 
36
-      $result = $this->cache->getCache($cacheKey);
36
+        $result = $this->cache->getCache($cacheKey);
37 37
 
38
-      $this->assertNotEmpty($result);
39
-      $this->assertEquals($cacheData, $result);
40
-  }
38
+        $this->assertNotEmpty($result);
39
+        $this->assertEquals($cacheData, $result);
40
+    }
41 41
 
42
-  public function testExpiredCacheInArray()
43
-  {
42
+    public function testExpiredCacheInArray()
43
+    {
44 44
     $cacheKey = 'expired_key';
45 45
     $cacheData = ['name' => 'Expired User', 'email' => '[email protected]'];
46 46
 
@@ -50,11 +50,11 @@  discard block
 block discarded – undo
50 50
     $this->assertEquals("Cache stored successfully", $this->cache->getMessage());
51 51
     $this->assertEmpty($this->cache->getCache($cacheKey));
52 52
     $this->assertFalse($this->cache->isSuccess());
53
-  }
53
+    }
54 54
 
55 55
 
56
-  public function testOverwriteExistingCacheInArray()
57
-  {
56
+    public function testOverwriteExistingCacheInArray()
57
+    {
58 58
     $cacheKey = 'overwrite_key';
59 59
     $initialCacheData = ['name' => 'Initial Data', 'email' => '[email protected]'];
60 60
     $newCacheData = ['name' => 'New Data', 'email' => '[email protected]'];
@@ -65,12 +65,12 @@  discard block
 block discarded – undo
65 65
     $this->cache->appendCache($cacheKey, $newCacheData);
66 66
     $this->assertEquals("Cache appended successfully", $this->cache->getMessage());
67 67
     $this->assertEquals($newCacheData, $this->cache->getCache($cacheKey));
68
-  }
68
+    }
69 69
 
70 70
 
71
-  public function testPutManyCacheItemsInArray()
72
-  {
73
-     $items = [
71
+    public function testPutManyCacheItemsInArray()
72
+    {
73
+        $items = [
74 74
             [
75 75
                 'cacheKey' => 'user_1_profile',
76 76
                 'cacheData' => [
@@ -96,10 +96,10 @@  discard block
 block discarded – undo
96 96
           
97 97
             $this->assertEquals($item['cacheData'], $this->cache->getCache($item['cacheKey']));
98 98
         }
99
-  }
99
+    }
100 100
 
101
-  public function testHasCacheFromArray()
102
-  {
101
+    public function testHasCacheFromArray()
102
+    {
103 103
     $cacheKey = 'test_key';
104 104
     $cacheData = ['name' => 'Sílvio Silva', 'role' => 'Developer'];
105 105
 
@@ -111,10 +111,10 @@  discard block
 block discarded – undo
111 111
     $hasCache = $this->cache->has($cacheKey);
112 112
 
113 113
     $this->assertTrue($this->cache->isSuccess());
114
-  }
114
+    }
115 115
 
116 116
     public function testRenewCacheFromArray()
117
-  {
117
+    {
118 118
     $cacheKey = 'expired_key';
119 119
     $cacheData = ['name' => 'Expired User', 'email' => '[email protected]'];
120 120
 
@@ -129,10 +129,10 @@  discard block
 block discarded – undo
129 129
     $this->cache->renewCache($cacheKey, 7200);
130 130
     $this->assertTrue($this->cache->isSuccess());
131 131
     $this->assertNotEmpty($this->cache->getCache($cacheKey));
132
-  }
132
+    }
133 133
 
134
-      public function testRenewCacheWithNamespaceFromRedis()
135
-  {
134
+        public function testRenewCacheWithNamespaceFromRedis()
135
+    {
136 136
     $cacheKey = 'expired_key';
137 137
     $namespace = 'expired_namespace';
138 138
     $cacheData = ['name' => 'Expired User', 'email' => '[email protected]'];
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
     $this->cache->renewCache($cacheKey, 7200, $namespace);
147 147
     $this->assertTrue($this->cache->isSuccess());
148 148
     $this->assertNotEmpty($this->cache->getCache($cacheKey, $namespace));
149
-  }
149
+    }
150 150
 
151 151
     public function testClearCacheDataFromRedis()
152 152
     {
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
         $this->assertEquals("Cache flushed successfully", $this->cache->getMessage());
183 183
     }
184 184
 
185
-  public function test_remember_saves_and_recover_values() 
185
+    public function test_remember_saves_and_recover_values() 
186 186
     {
187 187
         $this->cache->flushCache();
188 188
 
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
     }
218 218
 
219 219
 
220
-      public function test_get_and_forget()
220
+        public function test_get_and_forget()
221 221
     {
222 222
         $cacheKey = 'cache_key_test';
223 223
         $this->cache->putCache($cacheKey, 10);
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
         $this->assertNull($noCacheData);
239 239
     }
240 240
 
241
-      public function test_store_if_not_present_with_add_function()
241
+        public function test_store_if_not_present_with_add_function()
242 242
     {
243 243
         $existentKey = 'cache_key_test';
244 244
 
@@ -262,7 +262,7 @@  discard block
 block discarded – undo
262 262
 
263 263
     }
264 264
 
265
-      public function test_increment_function() {
265
+        public function test_increment_function() {
266 266
 
267 267
         $cacheKey = 'test_increment';
268 268
         $cacheData = 2025;
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -258,13 +258,13 @@  discard block
 block discarded – undo
258 258
     {
259 259
         $this->cache->flushCache();
260 260
 
261
-        $value = $this->cache->remember('remember_test_key', 60, function () {
261
+        $value = $this->cache->remember('remember_test_key', 60, function() {
262 262
             return 'valor_teste';
263 263
         });
264 264
 
265 265
         $this->assertEquals('valor_teste', $value);
266 266
 
267
-        $cachedValue = $this->cache->remember('remember_test_key', 60, function (){
267
+        $cachedValue = $this->cache->remember('remember_test_key', 60, function() {
268 268
             return 'novo_valor';
269 269
         });
270 270
 
@@ -276,12 +276,12 @@  discard block
 block discarded – undo
276 276
     {
277 277
         $this->cache->flushCache();
278 278
 
279
-        $value = $this->cache->rememberForever('remember_forever_key', function () {
279
+        $value = $this->cache->rememberForever('remember_forever_key', function() {
280 280
             return 'valor_eterno';
281 281
         });
282 282
         $this->assertEquals('valor_eterno', $value);
283 283
 
284
-        $cachedValue = $this->cache->rememberForever('remember_forever_key', function () {
284
+        $cachedValue = $this->cache->rememberForever('remember_forever_key', function() {
285 285
             return 'novo_valor';
286 286
         });
287 287
 
Please login to merge, or discard this patch.