Passed
Push — master ( 4bba79...d3b029 )
by Terry
17:12
created

Apc::getKeyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Shieldon Simple Cache package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\SimpleCache\Driver;
14
15
use Shieldon\SimpleCache\CacheProvider;
16
use Shieldon\SimpleCache\Exception\CacheException;
17
use function unserialize;
18
use function serialize;
19
20
/**
21
 * Provided by APC extension.
22
 */
23
use APCIterator;
24
use function apc_clear_cache;
25
use function apc_delete;
26
use function apc_exists;
27
use function apc_fetch;
28
use function apc_store;
29
30
/**
31
 * A cache driver class provided by APC (Alternative PHP Cache).
32
 *
33
 * @see https://www.php.net/manual/en/book.apc.php
34
 */
35
class Apc extends CacheProvider
36
{
37
    /**
38
     * Constructor.
39
     *
40
     * @param array $setting The settings.
41
     * 
42
     * @throws CacheException
43
     */
44 12
    public function __construct(array $setting = [])
45
    {
46 12
        if (!function_exists('apc_fetch')) {
47
            // @codeCoverageIgnoreStart
48
            throw new CacheException(
49
                'APC extension is not enable.'
50
            );
51
            // @codeCoverageIgnoreEnd
52
        }
53
54 12
        unset($setting);
55 12
    }
56
57
    /**
58
     * Fetch a cache by an extended Cache Driver.
59
     *
60
     * @param string $key The key of a cache.
61
     *
62
     * @return array
63
     */
64 8
    protected function doGet(string $key): array
65
    {
66 8
        $success = false;
67 8
		$content = apc_fetch($this->getKeyName($key), $success);
68
69 8
        if (empty($content) || !$success) {
70 8
            return [];
71
        }
72 8
        $data = unserialize($content);
73
74 8
        return $data;
75
    }
76
77
    /**
78
     * Set a cache by an extended Cache Driver.
79
     *
80
     * @param string $key       The key of a cache.
81
     * @param mixed  $value     The value of a cache. (serialized)
82
     * @param int    $ttl       The time to live for a cache.
83
     * @param int    $timestamp The time to store a cache.
84
     *
85
     * @return bool
86
     */
87 10
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
88
    {
89
        $contents = [
90 10
            'timestamp' => $timestamp,
91 10
            'ttl'       => $ttl,
92 10
            'value'     => $value
93
        ];
94
95 10
        $result = apc_store(
96 10
            $this->getKeyName($key),
97 10
            serialize($contents),
98 10
            $ttl
99
        );
100
101 10
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result could return the type array which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
102
    }
103
104
    /**
105
     * Delete a cache by an extended Cache Driver.
106
     *
107
     * @param string $key The key of a cache.
108
     * 
109
     * @return bool
110
     */
111 2
    protected function doDelete(string $key): bool
112
    {
113 2
        return apc_delete($this->getKeyName($key));
0 ignored issues
show
Bug Best Practice introduced by
The expression return apc_delete($this->getKeyName($key)) could return the type string[] which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
114
    }
115
116
    /**
117
     * Delete all caches by an extended Cache Driver.
118
     * 
119
     * @return bool
120
     */
121 6
    protected function doClear(): bool
122
    {
123 6
        return apc_clear_cache('user');
124
    }
125
126
    /**
127
     * Check if the cache exists or not.
128
     *
129
     * @param string $key The key of a cache.
130
     *
131
     * @return bool
132
     */
133 4
    protected function doHas(string $key): bool
134
    {
135 4
        return apc_exists($this->getKeyName($key));
0 ignored issues
show
Bug introduced by
$this->getKeyName($key) of type string is incompatible with the type boolean|string[] expected by parameter $keys of apc_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        return apc_exists(/** @scrutinizer ignore-type */ $this->getKeyName($key));
Loading history...
Bug Best Practice introduced by
The expression return apc_exists($this->getKeyName($key)) could return the type string[] which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
136
    }
137
138
    /**
139
     * Fetch all cache items.
140
     *
141
     * @return array
142
     */
143 4
    protected function getAll(): array
144
    {
145 4
        $list = [];
146
147 4
        foreach (new APCIterator('/^sc_/') as $item) {
148 2
            $key = str_replace('sc_', '', $item['key']);
149 2
            $value = unserialize($item['value']);
150 2
            $list[$key] = $value;
151
        }
152
153 4
        return $list;
154
    }
155
156
    /**
157
     * Get the key name of a cache.
158
     *
159
     * @param string $key The key of a cache.
160
     *
161
     * @return string
162
     */
163 10
    private function getKeyName(string $key): string
164
    {
165 10
        return 'sc_' . $key;
166
    }
167
}
168
169