Completed
Push — master ( 6d0805...e0c924 )
by Lars
01:52
created

AdapterApcu::setExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterApcu: a APCu-Cache adapter
9
 *
10
 * @see http://php.net/manual/de/book.apcu.php
11
 */
12
class AdapterApcu 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('apcu_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('apcu_store') === true
43
            &&
44
            \ini_get('apc.enable_cli')
45
        ) {
46
            $this->installed = true;
47
        }
48
    }
49
50
    /**
51
     * Check if apcu-cache exists.
52
     *
53
     * WARNING: we only keep this method for compatibly-reasons
54
     *          -> use ->exists($key)
55
     *
56
     * @param string $key
57
     *
58
     * @return bool
59
     *
60
     * @deprecated
61
     */
62
    public function apcu_cache_exists($key): bool
63
    {
64
        return $this->exists($key);
65
    }
66
67
    /**
68
     * Clears the APCu cache by type.
69
     *
70
     * @param string $type <p>WARNING: is not used in APCu only valid for APC</p>
71
     *
72
     * @return bool
73
     *
74
     * @internal
75
     */
76
    public function cacheClear(string $type): bool
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        return (bool) \apcu_clear_cache();
79
    }
80
81
    /**
82
     * Retrieves cached information from APCu's data store
83
     *
84
     * @param bool $limited    - If $limited is TRUE, the return value will exclude the individual list of cache
85
     *                         entries. This is useful when trying to optimize calls for statistics gathering
86
     *
87
     * @return array|bool <p>Array of cached data (and meta-data) or FALSE on failure.</p>
88
     */
89
    public function cacheInfo(bool $limited = false): array
90
    {
91
        return \apcu_cache_info($limited);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function exists(string $key): bool
98
    {
99
        return (bool) \apcu_exists($key);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function get(string $key)
106
    {
107
        if ($this->exists($key)) {
108
            return \apcu_fetch($key);
109
        }
110
111
        return null;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function installed(): bool
118
    {
119
        return $this->installed;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function remove(string $key): bool
126
    {
127
        return (bool) \apcu_delete($key);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function removeAll(): bool
134
    {
135
        return (bool) ($this->cacheClear('system') && $this->cacheClear('user'));
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function set(string $key, $value): bool
142
    {
143
        return (bool) \apcu_store($key, $value);
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function setExpired(string $key, $data, int $ttl = 0): bool
150
    {
151
        return (bool) \apcu_store($key, $data, $ttl);
152
    }
153
}
154