Completed
Push — master ( eb4f5d...fec2a8 )
by Lars
02:31
created

AdapterMemcached::setSettings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.8204

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 3
cts 13
cp 0.2308
rs 9.4285
cc 2
eloc 12
nc 2
nop 0
crap 3.8204
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 $memcached
26
   */
27 6
  public function __construct($memcached)
28
  {
29 6
    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 6
      $this->memcached = $memcached;
31 6
      $this->installed = true;
32
33 6
      $this->setSettings();
34
    }
35
  }
36
37
  /**
38
   * set Memcached settings
39
   */
40 6
  private function setSettings()
41
  {
42
    // Use faster compression if available
43 6
    if (\Memcached::HAVE_IGBINARY) {
44
      $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
45
    } else {
46 6
      $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_JSON);
47
    }
48
49
    $this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
50
    $this->memcached->setOption(\Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT);
51
    $this->memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
52
    $this->memcached->setOption(\Memcached::OPT_NO_BLOCK, true);
53
    $this->memcached->setOption(\Memcached::OPT_TCP_NODELAY, true);
54
    $this->memcached->setOption(\Memcached::OPT_COMPRESSION, true);
55
    $this->memcached->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 2);
56
  }
57
58
  /**
59
   * set cache-item by key => value
60
   *
61
   * @param string $key
62
   * @param mixed  $value
63
   *
64
   * @return mixed|void
65
   */
66
  public function set($key, $value)
67
  {
68
    // Make sure we are under the proper limit
69
    /*
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...
70
    if (strlen($this->memcached->getOption(\Memcached::OPT_PREFIX_KEY) . $key) > 250) {
71
      throw new \Exception('The passed cache key is over 250 bytes');
72
    }
73
    */
74
75
    return $this->memcached->set($key, $value);
76
  }
77
78
  /**
79
   * set cache-item by key => value + ttl
80
   *
81
   * @param string $key
82
   * @param mixed  $value
83
   * @param int    $ttl
84
   *
85
   * @return boolean
86
   */
87
  public function setExpired($key, $value, $ttl)
88
  {
89
    if ($ttl > 2592000) {
90
      $ttl = 2592000;
91
    }
92
93
    return $this->memcached->set($key, $value, $ttl);
94
  }
95
96
  /**
97
   * remove cached-item by key
98
   *
99
   * @param string $key
100
   *
101
   * @return boolean
102
   */
103
  public function remove($key)
104
  {
105
    return $this->memcached->delete($key);
106
  }
107
108
  /**
109
   * check if cached-item exists
110
   *
111
   * @param string $key
112
   *
113
   * @return bool
114
   */
115
  public function exists($key)
116
  {
117
    return $this->get($key) !== false;
118
  }
119
120
  /**
121
   * get cached-item by key
122
   *
123
   * @param String $key
124
   *
125
   * @return mixed
126
   */
127 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...
128
  {
129
    static $memcachedCache;
130
131
    if (isset($memcachedCache[$key])) {
132
      return $memcachedCache[$key];
133
    } else {
134
      $return = $this->memcached->get($key);
135
      $memcachedCache[$key] = $return;
136
      return $return;
137
    }
138
  }
139
140
  /**
141
   * check if cache is installed
142
   *
143
   * @return boolean
144
   */
145
  public function installed()
146
  {
147
    return $this->installed;
148
  }
149
150
}
151