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

AdapterMemcache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.7462

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 3
cts 7
cp 0.4286
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.7462
1
<?php
2
3
namespace voku\cache;
4
5
/**
6
 * AdapterMemcache: Memcache-adapter
7
 *
8
 * @package   voku\cache
9
 */
10
class AdapterMemcache implements iAdapter
11
{
12
  /**
13
   * @var bool
14
   */
15
  public $installed = false;
16
17
  /**
18
   * @var \Memcache
19
   */
20
  private $memcache;
21
22
  /**
23
   * @var boolean
24
   */
25
  private $compressed = false;
26
27
  /**
28
   * __construct
29
   *
30
   * @param \Memcache $memcache
31
   */
32 8
  public function __construct($memcache)
33
  {
34 8
    if ($memcache instanceof \Memcache) {
0 ignored issues
show
Bug introduced by
The class Memcache does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
35 8
      $this->memcache = $memcache;
36 8
      $this->installed = true;
37 8
    }
38 8
  }
39
40
  /**
41
   * @inheritdoc
42
   */
43 1
  public function exists($key)
44
  {
45 1
    return $this->get($key) !== false;
46
  }
47
48
  /**
49
   * @inheritdoc
50
   */
51 4
  public function get($key)
52
  {
53 4
    static $memcacheCache;
54
55 4
    if (array_key_exists($key, $memcacheCache) === true) {
56
      return $memcacheCache[$key];
57
    } else {
58
      $return = $this->memcache->get($key);
59
      $memcacheCache[$key] = $return;
60
61
      return $return;
62
    }
63
  }
64
65
  /**
66
   * @inheritdoc
67
   */
68 8
  public function installed()
69
  {
70 8
    return $this->installed;
71
  }
72
73
  /**
74
   * @inheritdoc
75
   */
76
  public function remove($key)
77
  {
78
    return $this->memcache->delete($key);
79
  }
80
81
  /**
82
   * @inheritdoc
83
   */
84
  public function removeAll()
85
  {
86
    return $this->memcache->flush();
87
  }
88
89
  /**
90
   * @inheritdoc
91
   */
92 2
  public function set($key, $value)
93
  {
94 2
    return $this->memcache->set($key, $value, $this->getCompressedFlag());
95
  }
96
97
  /**
98
   * @inheritdoc
99
   */
100 1
  public function setExpired($key, $value, $ttl)
101
  {
102 1
    if ($ttl > 2592000) {
103
      $ttl = 2592000;
104
    }
105
106 1
    return $this->memcache->set($key, $value, $this->getCompressedFlag(), $ttl);
107
  }
108
109
  /**
110
   * Get the compressed-flag from MemCache.
111
   *
112
   * @return int 2 || 0
113
   */
114 3
  private function getCompressedFlag()
115
  {
116 3
    return $this->isCompressed() ? MEMCACHE_COMPRESSED : 0;
117
  }
118
119
  /**
120
   * Check if compression from MemCache is active.
121
   *
122
   * @return boolean
123
   */
124 3
  public function isCompressed()
125
  {
126 3
    return $this->compressed;
127
  }
128
129
  /**
130
   * Activate the compression from MemCache.
131
   *
132
   * @param mixed $value will be converted to boolean
133
   */
134
  public function setCompressed($value)
135
  {
136
    $this->compressed = (bool)$value;
137
  }
138
139
}
140