Completed
Push — master ( 6a7f55...29301f )
by Lars
02:40
created

AdapterMemcache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 128
ccs 24
cts 33
cp 0.7272
rs 10
c 0
b 0
f 0

12 Methods

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