Completed
Push — master ( b3fbb6...d0db0e )
by Lars
02:47
created

Cache::getUsedAdapterClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace voku\cache;
4
5
/**
6
 * Cache: global-cache class
7
 *
8
 * can use different cache-adapter:
9
 * - Redis
10
 * - Memcache / Memcached
11
 * - APC / APCu
12
 * - Xcache
13
 * - Array
14
 * - File
15
 *
16
 * @package   voku\cache
17
 */
18
class Cache implements iCache
19
{
20
21
  /**
22
   * @var iAdapter
23
   */
24
  private $adapter;
25
26
  /**
27
   * @var iSerializer
28
   */
29
  private $serializer;
30
31
  /**
32
   * @var string
33
   */
34
  private $prefix = '';
35
36
  /**
37
   * @var bool
38
   */
39
  private $isReady = false;
40
41
  /**
42
   * @var bool
43
   */
44
  private $isActive = true;
45
46
  /**
47
   * @var mixed no cache, if admin-session is set
48
   */
49
  private $isAdminSession = false;
50
51
  /**
52
   * __construct
53
   *
54
   * @param null|iAdapter    $adapter
55
   * @param null|iSerializer $serializer
56
   * @param boolean          $checkForUser   check for dev-ip or if cms-user is logged-in
57
   * @param boolean          $cacheEnabled   false will disable the cache (use it e.g. for global settings)
58
   * @param string|boolean   $isAdminSession set a user-id, if the user is a admin (so we can disable cache for this
59
   *                                         user)
60
   */
61 42
  public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false)
62
  {
63 42
    $this->isAdminSession = $isAdminSession;
64
65
    // First check if the cache is active at all.
66 42
    $this->setActive($cacheEnabled);
67
    if (
68 42
        $this->isActive === true
69 42
        &&
70
        $checkForUser === true
71 42
    ) {
72
      $this->setActive($this->isCacheActiveForTheCurrentUser());
73
    }
74
75
    // If the cache is active, then try to auto-connect to the best possible cache-system.
76 42
    if ($this->isActive === true) {
77
78 42
      $this->setPrefix($this->getTheDefaultPrefix());
79
80
      if (
81
          $adapter === null
82 42
          ||
83 42
          !is_object($adapter)
84 42
          ||
85
          !$adapter instanceof iAdapter
86 42
      ) {
87
        $adapter = $this->autoConnectToAvailableCacheSystem();
88
      }
89
90
      // INFO: Memcache(d) has his own "serializer", so don't use it twice
91 42
      if (!is_object($serializer) && $serializer === null) {
92
        if (
93
            $adapter instanceof AdapterMemcached
94
            ||
95
            $adapter instanceof AdapterMemcache
96
        ) {
97
          $serializer = new SerializerNo();
98
        } else {
99
          // set default serializer
100
          $serializer = new SerializerIgbinary();
101
        }
102
      }
103 42
    }
104
105
    // Final checks ...
106
    if (
107
        $serializer instanceof iSerializer
108 42
        &&
109
        $adapter instanceof iAdapter
110 42
    ) {
111 42
      $this->setCacheIsReady(true);
112
113 42
      $this->adapter = $adapter;
114 42
      $this->serializer = $serializer;
115 42
    }
116 42
  }
117
118
  /**
119
   * enable / disable the cache
120
   *
121
   * @param boolean $isActive
122
   */
123 42
  public function setActive($isActive)
124
  {
125 42
    $this->isActive = (boolean)$isActive;
126 42
  }
127
128
  /**
129
   * check if the current use is a admin || dev || server == client
130
   *
131
   * @return bool
132
   */
133
  public function isCacheActiveForTheCurrentUser()
0 ignored issues
show
Coding Style introduced by
isCacheActiveForTheCurrentUser uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
isCacheActiveForTheCurrentUser uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
134
  {
135
    $active = true;
136
137
    // test the cache, with this GET-parameter
138
    $testCache = isset($_GET['testCache']) ? (int)$_GET['testCache'] : 0;
139
140
    if ($testCache != 1) {
141
      if (
142
        // server == client
143
          (
144
              isset($_SERVER['SERVER_ADDR'])
145
              &&
146
              $_SERVER['SERVER_ADDR'] == $this->getClientIp()
147
          )
148
          ||
149
          // admin is logged-in
150
          $this->isAdminSession
151
          ||
152
          // user is a dev
153
          $this->checkForDev() === true
154
      ) {
155
        $active = false;
156
      }
157
    }
158
159
    return $active;
160
  }
161
162
  /**
163
   * returns the IP address of the client
164
   *
165
   * @param   bool $trust_proxy_headers   Whether or not to trust the
166
   *                                      proxy headers HTTP_CLIENT_IP
167
   *                                      and HTTP_X_FORWARDED_FOR. ONLY
168
   *                                      use if your $_SERVER is behind a
169
   *                                      proxy that sets these values
170
   *
171
   * @return  string
172
   */
173
  private function getClientIp($trust_proxy_headers = false)
0 ignored issues
show
Coding Style introduced by
getClientIp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
174
  {
175
    $remoteAddr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'NO_REMOTE_ADDR';
176
177
    if ($trust_proxy_headers) {
178
      return $remoteAddr;
179
    }
180
181
    if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']) {
182
      $ip = $_SERVER['HTTP_CLIENT_IP'];
183
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
184
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
185
    } else {
186
      $ip = $remoteAddr;
187
    }
188
189
    return $ip;
190
  }
191
192
  /**
193
   * Check for local developer.
194
   *
195
   * @return bool
196
   */
197
  private function checkForDev()
0 ignored issues
show
Coding Style introduced by
checkForDev uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
checkForDev uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
198
  {
199
    $return = false;
200
201
    if (function_exists('checkForDev')) {
202
      $return = checkForDev();
203
    } else {
204
205
      // for testing with dev-address
206
      $noDev = isset($_GET['noDev']) ? (int)$_GET['noDev'] : 0;
207
      $remoteAddr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'NO_REMOTE_ADDR';
208
209
      if (
210
          $noDev != 1
211
          &&
212
          (
213
              $remoteAddr === '127.0.0.1'
214
              ||
215
              $remoteAddr === '::1'
216
              ||
217
              PHP_SAPI === 'cli'
218
          )
219
      ) {
220
        $return = true;
221
      }
222
    }
223
224
    return $return;
225
  }
226
227
  /**
228
   * Set the default-prefix via "SERVER"-var + "SESSION"-language.
229
   */
230 42
  protected function getTheDefaultPrefix()
0 ignored issues
show
Coding Style introduced by
getTheDefaultPrefix uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getTheDefaultPrefix uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
231
  {
232 42
    return (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '') . '_' .
233 42
           (isset($_SERVER['THEME']) ? $_SERVER['THEME'] : '') . '_' .
234 42
           (isset($_SERVER['STAGE']) ? $_SERVER['STAGE'] : '') . '_' .
235 42
           (isset($_SESSION['language']) ? $_SESSION['language'] : '') . '_' .
236 42
           (isset($_SESSION['language_extra']) ? $_SESSION['language_extra'] : '');
237
  }
238
239
  /**
240
   * Auto-connect to the available cache-system on the server.
241
   *
242
   * @return iAdapter
243
   */
244
  protected function autoConnectToAvailableCacheSystem()
245
  {
246
    static $adapterCache;
247
248
    if (is_object($adapterCache) && $adapterCache instanceof iAdapter) {
249
      return $adapterCache;
250
    } else {
251
252
      $memcached = null;
253
      $isMemcachedAvailable = false;
254
      if (extension_loaded('memcached')) {
255
        $memcached = new \Memcached();
256
        /** @noinspection PhpUsageOfSilenceOperatorInspection */
257
        $isMemcachedAvailable = @$memcached->addServer('127.0.0.1', 11211);
258
      }
259
260
      if ($isMemcachedAvailable === false) {
261
        $memcached = null;
262
      }
263
264
      $adapterMemcached = new AdapterMemcached($memcached);
0 ignored issues
show
Bug introduced by
It seems like $memcached can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
265
      if ($adapterMemcached->installed() === true) {
266
267
        // -------------------------------------------------------------
268
        // "Memcached"
269
        // -------------------------------------------------------------
270
        $adapter = $adapterMemcached;
271
272
      } else {
273
274
        $memcache = null;
275
        $isMemcacheAvailable = false;
276
        if (class_exists('\Memcache')) {
277
          $memcache = new \Memcache;
278
          /** @noinspection PhpUsageOfSilenceOperatorInspection */
279
          $isMemcacheAvailable = @$memcache->connect('127.0.0.1', 11211);
280
        }
281
282
        if ($isMemcacheAvailable === false) {
283
          $memcache = null;
284
        }
285
286
        $adapterMemcache = new AdapterMemcache($memcache);
0 ignored issues
show
Bug introduced by
It seems like $memcache can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
287
        if ($adapterMemcache->installed() === true) {
288
289
          // -------------------------------------------------------------
290
          // "Memcache"
291
          // -------------------------------------------------------------
292
          $adapter = $adapterMemcache;
293
294
        } else {
295
296
          $redis = null;
297
          $isRedisAvailable = false;
298
          if (
299
              extension_loaded('redis')
300
              &&
301
              class_exists('\Predis\Client')
302
          ) {
303
            /** @noinspection PhpUndefinedNamespaceInspection */
304
            $redis = new \Predis\Client(
305
                array(
306
                    'scheme'  => 'tcp',
307
                    'host'    => '127.0.0.1',
308
                    'port'    => 6379,
309
                    'timeout' => '2.0',
310
                )
311
            );
312
            try {
313
              $redis->connect();
314
              $isRedisAvailable = $redis->getConnection()->isConnected();
315
            } catch (\Exception $e) {
316
              // nothing
317
            }
318
          }
319
320
          if ($isRedisAvailable === false) {
321
            $redis = null;
322
          }
323
324
          $adapterRedis = new AdapterPredis($redis);
0 ignored issues
show
Bug introduced by
It seems like $redis can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
325
          if ($adapterRedis->installed() === true) {
326
327
            // -------------------------------------------------------------
328
            // Redis
329
            // -------------------------------------------------------------
330
            $adapter = $adapterRedis;
331
332
          } else {
333
334
            $adapterXcache = new AdapterXcache();
335
            if ($adapterXcache->installed() === true) {
336
337
              // -------------------------------------------------------------
338
              // "Xcache"
339
              // -------------------------------------------------------------
340
              $adapter = $adapterXcache;
341
342
            } else {
343
344
              $adapterApc = new AdapterApc();
345
              if ($adapterApc->installed() === true) {
346
347
                // -------------------------------------------------------------
348
                // "APC"
349
                // -------------------------------------------------------------
350
                $adapter = $adapterApc;
351
352
              } else {
353
354
                $adapterApcu = new AdapterApcu();
355
                if ($adapterApcu->installed() === true) {
356
357
                  // -------------------------------------------------------------
358
                  // "APCu"
359
                  // -------------------------------------------------------------
360
                  $adapter = $adapterApcu;
361
362
                } else {
363
364
                  $adapterFile = new AdapterFile();
365
                  if ($adapterFile->installed() === true) {
366
367
                    // -------------------------------------------------------------
368
                    // File-Cache
369
                    // -------------------------------------------------------------
370
                    $adapter = $adapterFile;
371
372
                  } else {
373
374
                    // -------------------------------------------------------------
375
                    // Static-PHP-Cache
376
                    // -------------------------------------------------------------
377
                    $adapter = new AdapterArray();
378
                  }
379
                }
380
              }
381
            }
382
          }
383
        }
384
      }
385
386
      // save to static cache
387
      $adapterCache = $adapter;
388
    }
389
390
    return $adapter;
391
  }
392
393
  /**
394
   * Set "isReady" state.
395
   *
396
   * @param boolean $isReady
397
   */
398 42
  private function setCacheIsReady($isReady)
399
  {
400 42
    $this->isReady = (boolean)$isReady;
401 42
  }
402
403
  /**
404
   * Get the "isReady" state.
405
   *
406
   * @return boolean
407
   */
408 4
  public function getCacheIsReady()
409
  {
410 4
    return $this->isReady;
411
  }
412
413
  /**
414
   * Get cached-item by key.
415
   *
416
   * @param string $key
417
   *
418
   * @return mixed
419
   */
420 19
  public function getItem($key)
421
  {
422 19
    if ($this->adapter instanceof iAdapter) {
423 19
      $storeKey = $this->calculateStoreKey($key);
424 19
      $serialized = $this->adapter->get($storeKey);
425 12
      $value = $serialized ? $this->serializer->unserialize($serialized) : null;
426 12
    } else {
427
      return null;
428
    }
429
430 12
    return $value;
431
  }
432
433
  /**
434
   * Calculate store-key (prefix + $rawKey).
435
   *
436
   * @param string $rawKey
437
   *
438
   * @return string
439
   */
440 33
  private function calculateStoreKey($rawKey)
441
  {
442 33
    $str = $this->getPrefix() . $rawKey;
443
444 33
    if ($this->adapter instanceof AdapterFile) {
445 6
      $str = $this->cleanStoreKey($str);
446 6
    }
447
448 33
    return $str;
449
  }
450
451
  /**
452
   * Clean store-key (required e.g. for the "File"-Adapter).
453
   *
454
   * @param string $str
455
   *
456
   * @return string
457
   */
458 6
  private function cleanStoreKey($str)
459
  {
460 6
    $str = preg_replace("/[\r\n\t ]+/", ' ', $str);
461 6
    $str = str_replace(
462 6
        array('"', '*', ':', '<', '>', '?', "'", '|'),
463
        array(
464 6
            '-+-',
465 6
            '-+-+-',
466 6
            '-+-+-+-',
467 6
            '-+-+-+-+-',
468 6
            '-+-+-+-+-+-',
469 6
            '-+-+-+-+-+-+-',
470 6
            '-+-+-+-+-+-+-+-',
471 6
            '-+-+-+-+-+-+-+-+-',
472 6
        ),
473
        $str
474 6
    );
475 6
    $str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
476 6
    $str = htmlentities($str, ENT_QUOTES, 'UTF-8');
477 6
    $str = preg_replace('/(&)([a-z])([a-z]+;)/i', '$2', $str);
478 6
    $str = str_replace(' ', '-', $str);
479 6
    $str = rawurlencode($str);
480 6
    $str = str_replace('%', '-', $str);
481
482 6
    return $str;
483
  }
484
485
  /**
486
   * Get the prefix.
487
   *
488
   * @return string
489
   */
490 33
  public function getPrefix()
491
  {
492 33
    return $this->prefix;
493
  }
494
495
  /**
496
   * !!! Set the prefix. !!!
497
   *
498
   * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix.
499
   *
500
   * @param string $prefix
501
   */
502 42
  public function setPrefix($prefix)
503
  {
504 42
    $this->prefix = (string)$prefix;
505 42
  }
506
507
  /**
508
   * Set cache-item by key => value + date.
509
   *
510
   * @param string    $key
511
   * @param mixed     $value
512
   * @param \DateTime $date
513
   *
514
   * @return boolean
515
   * @throws \Exception
516
   */
517 6
  public function setItemToDate($key, $value, \DateTime $date)
518
  {
519 6
    $ttl = $date->getTimestamp() - time();
520
521 6
    if ($ttl <= 0) {
522 1
      throw new \Exception('Date in the past.');
523
    }
524
525 5
    $storeKey = $this->calculateStoreKey($key);
526
527 5
    return $this->setItem($storeKey, $value, $ttl);
528
  }
529
530
  /**
531
   * Set cache-item by key => value + ttl.
532
   *
533
   * @param string $key
534
   * @param mixed  $value
535
   * @param int    $ttl
536
   *
537
   * @return bool
538
   */
539 18
  public function setItem($key, $value, $ttl = 0)
540
  {
541
    if (
542 18
        $this->adapter instanceof iAdapter
543 18
        &&
544 18
        $this->serializer instanceof iSerializer
545 18
    ) {
546 18
      $storeKey = $this->calculateStoreKey($key);
547 18
      $serialized = $this->serializer->serialize($value);
548
549 18
      if ($ttl) {
550 7
        return $this->adapter->setExpired($storeKey, $serialized, $ttl);
551
      } else {
552 11
        return $this->adapter->set($storeKey, $serialized);
553
      }
554
    } else {
555
      return false;
556
    }
557
  }
558
559
  /**
560
   * Remove a cached-item.
561
   *
562
   * @param string $key
563
   *
564
   * @return bool
565
   */
566 2 View Code Duplication
  public function removeItem($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
567
  {
568 2
    if ($this->adapter instanceof iAdapter) {
569 2
      $storeKey = $this->calculateStoreKey($key);
570
571 2
      return $this->adapter->remove($storeKey);
572
    } else {
573
      return false;
574
    }
575
  }
576
577
  /**
578
   * Remove all cached-items.
579
   *
580
   * @return bool
581
   */
582 1
  public function removeAll()
583
  {
584 1
    if ($this->adapter instanceof iAdapter) {
585 1
      return $this->adapter->removeAll();
586
    } else {
587
      return false;
588
    }
589
  }
590
591
  /**
592
   * Check if cached-item exists.
593
   *
594
   * @param string $key
595
   *
596
   * @return boolean
597
   */
598 6 View Code Duplication
  public function existsItem($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
599
  {
600 6
    if ($this->adapter instanceof iAdapter) {
601 6
      $storeKey = $this->calculateStoreKey($key);
602
603 6
      return $this->adapter->exists($storeKey);
604
    } else {
605
      return false;
606
    }
607
  }
608
609
  /**
610
   * Get the current adapter class-name.
611
   *
612
   * @return string
613
   */
614 2
  public function getUsedAdapterClassName()
615
  {
616 2
    return get_class($this->adapter);
617
  }
618
619
  /**
620
   * Get the current serializer class-name.
621
   *
622
   * @return string
623
   */
624 2
  public function getUsedSerializerClassName()
625
  {
626 2
    return get_class($this->serializer);
627
  }
628
}
629