Completed
Push — master ( 292082...b96965 )
by Lars
01:56
created

AdapterApc   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 150
Duplicated Lines 16 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 0
dl 24
loc 150
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

11 Methods

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