Completed
Push — master ( b3fbb6...d0db0e )
by Lars
02:47
created

AdapterApc   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 15.63%

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 0
dl 137
loc 137
ccs 5
cts 32
cp 0.1563
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 3
A apc_cache_exists() 4 4 1
A cacheClear() 4 4 1
A cacheInfo() 4 4 1
A exists() 8 8 2
A get() 8 8 2
A installed() 4 4 1
A remove() 4 4 1
A removeAll() 4 4 2
A set() 4 4 1
A setExpired() 4 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
namespace voku\cache;
4
5
/**
6
 * AdapterApc: a APC-Cache adapter
7
 *
8
 * http://php.net/manual/de/book.apc.php
9
 *
10
 * @package   voku\cache
11
 */
12 View Code Duplication
class AdapterApc implements iAdapter
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
15
  /**
16
   * @var bool
17
   */
18
  public $installed = false;
19
20
  /**
21
   * @var bool
22
   */
23
  public $debug = false;
24
25
  /**
26
   * __construct()
27
   */
28 9
  public function __construct()
29
  {
30
    if (
31 9
        function_exists('apc_store') === true
32 9
        &&
33
        ini_get('apc.enabled')
34 9
    ) {
35
      $this->installed = true;
36
    }
37 9
  }
38
39
  /**
40
   * Check if apc-cache exists.
41
   *
42
   * WARNING: use $this->exists($key) instead
43
   *
44
   * @param string $key
45
   *
46
   * @return bool
47
   *
48
   * @internal
49
   */
50
  public function apc_cache_exists($key)
51
  {
52
    return (bool)apc_fetch($key);
53
  }
54
55
  /**
56
   * Clears the APC cache by type.
57
   *
58
   * @param string $type - If $type is "user", the user cache will be cleared; otherwise,
59
   *                       the system cache (cached files) will be cleared.
60
   *
61
   * @return boolean
62
   *
63
   * @internal
64
   */
65
  public function cacheClear($type)
66
  {
67
    return apc_clear_cache($type);
68
  }
69
70
  /**
71
   * Retrieves cached information from APC's data store
72
   *
73
   * @param string  $type    - If $type is "user", information about the user cache will be returned.
74
   * @param boolean $limited - If $limited is TRUE, the return value will exclude the individual list of cache entries.
75
   *                         This is useful when trying to optimize calls for statistics gathering.
76
   *
77
   * @return array of cached data (and meta-data) or FALSE on failure.
78
   */
79
  public function cacheInfo($type = '', $limited = false)
80
  {
81
    return apc_cache_info($type, $limited);
82
  }
83
84
  /**
85
   * @inheritdoc
86
   */
87
  public function exists($key)
88
  {
89
    if (function_exists('apc_exists')) {
90
      return apc_exists($key);
91
    } else {
92
      return $this->apc_cache_exists($key);
93
    }
94
  }
95
96
  /**
97
   * @inheritdoc
98
   */
99
  public function get($key)
100
  {
101
    if ($this->exists($key)) {
102
      return apc_fetch($key);
103
    } else {
104
      return false;
105
    }
106
  }
107
108
  /**
109
   * @inheritdoc
110
   */
111
  public function installed()
112
  {
113
    return $this->installed;
114
  }
115
116
  /**
117
   * @inheritdoc
118
   */
119
  public function remove($key)
120
  {
121
    return apc_delete($key);
122
  }
123
124
  /**
125
   * @inheritdoc
126
   */
127
  public function removeAll()
128
  {
129
    return $this->cacheClear('system') && $this->cacheClear('user');
130
  }
131
132
  /**
133
   * @inheritdoc
134
   */
135
  public function set($key, $value)
136
  {
137
    return apc_store($key, $value);
138
  }
139
140
  /**
141
   * @inheritdoc
142
   */
143
  public function setExpired($key, $data, $ttl)
144
  {
145
    return apc_store($key, $data, $ttl);
146
  }
147
148
}
149