Test Failed
Push — master ( a4afd4...c83c8c )
by Terry
03:28
created

Apcu::doClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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 APCu extension.
22
 */
23
use function apcu_clear_cache;
24
use function apcu_delete;
25
use function apcu_exists;
26
use function apcu_fetch;
27
use function apcu_store;
28
29
/**
30
 * A cache driver class provided by APCu (APC User Cache)
31
 *
32
 * @see https://www.php.net/manual/en/book.apcu.php
33
 */
34
class Apcu extends CacheProvider
35
{
36
    /**
37
     * Constructor.
38
     *
39
     * @param array $setting The settings.
40
     * 
41
     * @throws CacheException
42
     */
43 8
    public function __construct(array $setting = [])
44
    {
45 8
        if (!function_exists('apcu_fetch')) {
46 8
            throw new CacheException(
47 8
                'APCu extension is not enable.'
48
            );
49
        }
50
51
        unset($setting);
52
    }
53
54
    /**
55
     * Fetch a cache by an extended Cache Driver.
56
     *
57
     * @param string $key The key of a cache.
58
     *
59
     * @return array
60
     */
61
    protected function doGet(string $key): array
62
    {
63
        $success = false;
64
        $content = apcu_fetch($key, $success);
65
66
        if (empty($content) || !$success) {
67
            return [];
68
        }
69
        $data = unserialize($content);
70
71
        return $data;
72
    }
73
74
    /**
75
     * Set a cache by an extended Cache Driver.
76
     *
77
     * @param string $key       The key of a cache.
78
     * @param mixed  $value     The value of a cache. (serialized)
79
     * @param int    $ttl       The time to live for a cache.
80
     * @param int    $timestamp The time to store a cache.
81
     *
82
     * @return bool
83
     */
84
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
85
    {
86
        $contents = [
87
            'timestamp' => $timestamp,
88
            'ttl'       => $ttl,
89
            'value'     => $value
90
        ];
91
92
        $result = apcu_store(
93
            $key,
94
            serialize($contents),
95
            $ttl
96
        );
97
98
        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...
99
    }
100
101
    /**
102
     * Delete a cache by an extended Cache Driver.
103
     *
104
     * @param string $key The key of a cache.
105
     * 
106
     * @return bool
107
     */
108
    protected function doDelete(string $key): bool
109
    {
110
        return apcu_delete($key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_delete($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...
111
    }
112
113
    /**
114
     * Delete all caches by an extended Cache Driver.
115
     * 
116
     * @return bool
117
     */
118
    protected function doClear(): bool
119
    {
120
        return apcu_clear_cache();
121
    }
122
123
    /**
124
     * Check if the cache exists or not.
125
     *
126
     * @param string $key The key of a cache.
127
     *
128
     * @return bool
129
     */
130
    protected function doHas(string $key): bool
131
    {
132
        return apcu_exists($key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_exists($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...
133
    }
134
}
135
136