Completed
Push — master ( 386972...cf64d5 )
by Lars
02:26 queued 46s
created

AdapterMemcached::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
ccs 3
cts 4
cp 0.75
rs 9.6666
cc 2
eloc 4
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace voku\cache;
4
5
use voku\cache\Exception\InvalidArgumentException;
6
7
/**
8
 * AdapterMemcached: Memcached-adapter
9
 *
10
 * @package voku\cache
11
 */
12
class AdapterMemcached implements iAdapter
13
{
14
  /**
15
   * @var bool
16
   */
17
  public $installed = false;
18
19
  /**
20
   * @var \Memcached
21
   */
22
  private $memcached;
23
24
  /**
25
   * __construct
26
   *
27
   * @param \Memcached|null $memcached
28
   */
29 9
  public function __construct($memcached = null)
30
  {
31 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...
32 9
      $this->setMemcached($memcached);
33 9
    }
34 9
  }
35
36
  /**
37
   * @param \Memcached $memcached
38
   */
39 9
  public function setMemcached(\Memcached $memcached) {
40 9
    $this->memcached = $memcached;
41 9
    $this->installed = true;
42
43 9
    $this->setSettings();
44 9
  }
45
46
  /**
47
   * @inheritdoc
48
   */
49 2
  public function exists($key)
50
  {
51 2
    return $this->get($key) !== false;
52
  }
53
54
  /**
55
   * @inheritdoc
56
   */
57 5
  public function get($key)
58
  {
59 5
    return $this->memcached->get($key);
60
  }
61
62
  /**
63
   * @inheritdoc
64
   */
65 9
  public function installed()
66
  {
67 9
    return $this->installed;
68
  }
69
70
  /**
71
   * @inheritdoc
72
   */
73
  public function remove($key)
74
  {
75
    return $this->memcached->delete($key);
76
  }
77
78
  /**
79
   * @inheritdoc
80
   */
81
  public function removeAll()
82
  {
83
    return $this->memcached->flush();
84
  }
85
86
  /**
87
   * @inheritdoc
88
   */
89 3 View Code Duplication
  public function set($key, $value)
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...
90
  {
91
    // Make sure we are under the proper limit
92 3
    if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) {
93
      throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . print_r($key, true));
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