Completed
Push — master ( 5cfb46...2936f6 )
by Lars
01:39
created

AdapterMemcache   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 133
Duplicated Lines 6.77 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 9
loc 133
ccs 0
cts 33
cp 0
rs 10
c 1
b 1
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() 9 9 2
A setExpired() 0 8 2
A getCompressedFlag() 0 4 2
A isCompressed() 0 4 1
A setCompressed() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
use voku\cache\Exception\InvalidArgumentException;
8
9
/**
10
 * AdapterMemcache: Memcache-adapter
11
 *
12
 * @package voku\cache
13
 */
14
class AdapterMemcache implements iAdapter
15
{
16
  /**
17
   * @var bool
18
   */
19
  public $installed = false;
20
21
  /**
22
   * @var \Memcache
23
   */
24
  private $memcache;
25
26
  /**
27
   * @var bool
28
   */
29
  private $compressed = false;
30
31
  /**
32
   * __construct
33
   *
34
   * @param \Memcache|null $memcache
35
   */
36
  public function __construct($memcache = null)
37
  {
38
    if ($memcache instanceof \Memcache) {
39
      $this->setMemcache($memcache);
40
    }
41
  }
42
43
  /**
44
   * @param \Memcache $memcache
45
   */
46
  public function setMemcache(\Memcache $memcache) {
47
    $this->memcache = $memcache;
48
    $this->installed = true;
49
  }
50
51
  /**
52
   * @inheritdoc
53
   */
54
  public function exists(string $key): bool
55
  {
56
    return $this->get($key) !== false;
57
  }
58
59
  /**
60
   * @inheritdoc
61
   */
62
  public function get(string $key)
63
  {
64
    return $this->memcache->get($key);
65
  }
66
67
  /**
68
   * @inheritdoc
69
   */
70
  public function installed(): bool
71
  {
72
    return $this->installed;
73
  }
74
75
  /**
76
   * @inheritdoc
77
   */
78
  public function remove(string $key): bool
79
  {
80
    return $this->memcache->delete($key);
81
  }
82
83
  /**
84
   * @inheritdoc
85
   */
86
  public function removeAll(): bool
87
  {
88
    return $this->memcache->flush();
89
  }
90
91
  /**
92
   * @inheritdoc
93
   */
94 View Code Duplication
  public function set(string $key, $value): bool
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...
95
  {
96
    // Make sure we are under the proper limit
97
    if (\strlen($key) > 250) {
98
      throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . print_r($key, true));
99
    }
100
101
    return $this->memcache->set($key, $value, $this->getCompressedFlag());
102
  }
103
104
  /**
105
   * @inheritdoc
106
   */
107
  public function setExpired(string $key, $value, int $ttl = 0): bool
108
  {
109
    if ($ttl > 2592000) {
110
      $ttl = 2592000;
111
    }
112
113
    return $this->memcache->set($key, $value, $this->getCompressedFlag(), $ttl);
114
  }
115
116
  /**
117
   * Get the compressed-flag from MemCache.
118
   *
119
   * @return int 2 || 0
120
   */
121
  private function getCompressedFlag(): int
122
  {
123
    return $this->isCompressed() ? MEMCACHE_COMPRESSED : 0;
124
  }
125
126
  /**
127
   * Check if compression from MemCache is active.
128
   *
129
   * @return bool
130
   */
131
  public function isCompressed(): bool
132
  {
133
    return $this->compressed;
134
  }
135
136
  /**
137
   * Activate the compression from MemCache.
138
   *
139
   * @param mixed $value will be converted to bool
140
   */
141
  public function setCompressed($value)
142
  {
143
    $this->compressed = (bool)$value;
144
  }
145
146
}
147