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

AdapterMemcache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 62.86%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 130
ccs 22
cts 35
cp 0.6286
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A exists() 0 4 1
A get() 0 13 2
A installed() 0 4 1
A remove() 0 4 1
A removeAll() 0 4 1
A set() 0 4 1
A setExpired() 0 8 2
A getCompressedFlag() 0 4 2
A isCompressed() 0 4 1
A setCompressed() 0 4 1
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