Completed
Push — master ( 6f3b92...6a7f55 )
by Lars
03:19
created

AdapterMemcache::get()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 32
Code Lines 15

Duplication

Lines 32
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 5.1374

Importance

Changes 0
Metric Value
dl 32
loc 32
ccs 14
cts 17
cp 0.8235
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 12
nop 1
crap 5.1374
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 View Code Duplication
  public function get($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...
52
  {
53 4
    static $memcacheCache = array();
54 4
    static $memcacheCacheCounter = array();
55 4
    $staticCacheCounterHelper = 5;
56
57 4
    if (!isset($memcacheCacheCounter[$key])) {
58 3
      $memcacheCacheCounter[$key] = 0;
59 3
    }
60
61 4
    if ($memcacheCacheCounter[$key] < ($staticCacheCounterHelper + 1)) {
62 4
      $memcacheCacheCounter[$key]++;
63 4
    }
64
65 4
    if (array_key_exists($key, $memcacheCache) === true) {
66
67
      // get from static-cache
68
      return $memcacheCache[$key];
69
70
    } else {
71
72
      // get from cache-adapter
73 4
      $return = $this->memcache->get($key);
74
75
      // save into static-cache
76 4
      if ($memcacheCacheCounter[$key] >= $staticCacheCounterHelper) {
77
        $memcacheCache[$key] = $return;
78
      }
79
80 4
      return $return;
81
    }
82
  }
83
84
  /**
85
   * @inheritdoc
86
   */
87 8
  public function installed()
88
  {
89 8
    return $this->installed;
90
  }
91
92
  /**
93
   * @inheritdoc
94
   */
95
  public function remove($key)
96
  {
97
    return $this->memcache->delete($key);
98
  }
99
100
  /**
101
   * @inheritdoc
102
   */
103
  public function removeAll()
104
  {
105
    return $this->memcache->flush();
106
  }
107
108
  /**
109
   * @inheritdoc
110
   */
111 2
  public function set($key, $value)
112
  {
113 2
    return $this->memcache->set($key, $value, $this->getCompressedFlag());
114
  }
115
116
  /**
117
   * @inheritdoc
118
   */
119 1
  public function setExpired($key, $value, $ttl)
120
  {
121 1
    if ($ttl > 2592000) {
122
      $ttl = 2592000;
123
    }
124
125 1
    return $this->memcache->set($key, $value, $this->getCompressedFlag(), $ttl);
126
  }
127
128
  /**
129
   * Get the compressed-flag from MemCache.
130
   *
131
   * @return int 2 || 0
132
   */
133 3
  private function getCompressedFlag()
134
  {
135 3
    return $this->isCompressed() ? MEMCACHE_COMPRESSED : 0;
136
  }
137
138
  /**
139
   * Check if compression from MemCache is active.
140
   *
141
   * @return boolean
142
   */
143 3
  public function isCompressed()
144
  {
145 3
    return $this->compressed;
146
  }
147
148
  /**
149
   * Activate the compression from MemCache.
150
   *
151
   * @param mixed $value will be converted to boolean
152
   */
153
  public function setCompressed($value)
154
  {
155
    $this->compressed = (bool)$value;
156
  }
157
158
}
159