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

AdapterMemcached::setMemcached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace voku\cache;
4
5
/**
6
 * AdapterMemcached: Memcached-adapter
7
 *
8
 * @package   voku\cache
9
 */
10
class AdapterMemcached implements iAdapter
11
{
12
  /**
13
   * @var bool
14
   */
15
  public $installed = false;
16
17
  /**
18
   * @var \Memcached
19
   */
20
  private $memcached;
21
22
  /**
23
   * __construct
24
   *
25
   * @param \Memcached|null $memcached
26
   */
27 9
  public function __construct($memcached = null)
28
  {
29 9
    if ($memcached instanceof \Memcached) {
0 ignored issues
show
Bug introduced by
The class Memcached 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...
30 9
      $this->setMemcached($memcached);
31 9
    }
32 9
  }
33
34
  /**
35
   * @param \Memcached $memcached
36
   */
37 9
  public function setMemcached(\Memcached $memcached) {
38 9
    $this->memcached = $memcached;
39 9
    $this->installed = true;
40
41 9
    $this->setSettings();
42 9
  }
43
44
  /**
45
   * @inheritdoc
46
   */
47 2
  public function exists($key)
48
  {
49 2
    return $this->get($key) !== false;
50
  }
51
52
  /**
53
   * @inheritdoc
54
   */
55 6
  public function get($key)
56
  {
57 6
    return $this->memcached->get($key);
58
  }
59
60
  /**
61
   * @inheritdoc
62
   */
63 9
  public function installed()
64
  {
65 9
    return $this->installed;
66
  }
67
68
  /**
69
   * @inheritdoc
70
   */
71
  public function remove($key)
72
  {
73
    return $this->memcached->delete($key);
74
  }
75
76
  /**
77
   * @inheritdoc
78
   */
79
  public function removeAll()
80
  {
81
    return $this->memcached->flush();
82
  }
83
84
  /**
85
   * @inheritdoc
86
   */
87 3
  public function set($key, $value)
88
  {
89
    // Make sure we are under the proper limit
90
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
    if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) {
92
      throw new \Exception('The passed cache key is over 250 bytes');
93
    }
94
    */
95
96 3
    return $this->memcached->set($key, $value);
97
  }
98
99
  /**
100
   * @inheritdoc
101
   */
102 1
  public function setExpired($key, $value, $ttl)
103
  {
104 1
    if ($ttl > 2592000) {
105
      $ttl = 2592000;
106
    }
107
108 1
    return $this->memcached->set($key, $value, $ttl);
109
  }
110
111
  /**
112
   * Set the MemCached settings.
113
   */
114 9
  private function setSettings()
115
  {
116
    // Use faster compression if available
117 9
    if (\Memcached::HAVE_IGBINARY) {
118
      $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
119
    }
120 9
    $this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
121 9
    $this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
122 9
    $this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true);
123 9
    $this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true);
124 9
    $this->memcached->setOption(\Memcached::OPT_COMPRESSION, false);
125 9
    $this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2);
126 9
  }
127
128
}
129