Mock   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 79
ccs 18
cts 18
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A doGet() 0 7 2
A doClear() 0 5 1
A doDelete() 0 5 1
A doHas() 0 3 1
A doSet() 0 9 1
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\CacheWarmingTrait;
17
18
/**
19
 * A mock driver for testing Cache Provider.
20
 */
21
class Mock extends CacheProvider
22
{
23
    use CacheWarmingTrait;
24
25
    protected $type = 'mock';
26
27
    /**
28
     * Fetch a cache by an extended Cache Driver.
29
     *
30
     * @param string $key The key of a cache.
31
     *
32
     * @return array
33
     */
34 6
    protected function doGet(string $key): array
35
    {
36 6
        if ($this->doHas($key)) {
37 6
            return $this->pool[$key];
38
        }
39
40 4
        return [];
41
    }
42
43
    /**
44
     * Set a cache by an extended Cache Driver.
45
     *
46
     * @param string $key       The key of a cache.
47
     * @param mixed  $value     The value of a cache. (serialized)
48
     * @param int    $ttl       The time to live for a cache.
49
     * @param int    $timestamp The time to store a cache.
50
     *
51
     * @return bool
52
     */
53 6
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
54
    {
55 6
        $this->pool[$key] = [
56 6
            'value'     => $value,
57 6
            'ttl'       => $ttl,
58 6
            'timestamp' => $timestamp,
59
        ];
60
61 6
        return true;
62
    }
63
64
    /**
65
     * Delete a cache by an extended Cache Driver.
66
     *
67
     * @param string $key The key of a cache.
68
     *
69
     * @return bool
70
     */
71 4
    protected function doDelete(string $key): bool
72
    {
73 4
        unset($this->pool[$key]);
74
75 4
        return true;
76
    }
77
78
    /**
79
     * Delete all caches by an extended Cache Driver.
80
     *
81
     * @return bool
82
     */
83 2
    protected function doClear(): bool
84
    {
85 2
        $this->pool = [];
86
87 2
        return true;
88
    }
89
90
    /**
91
     * Undocumented function
92
     *
93
     * @param string $key The key of a cache.
94
     *
95
     * @return bool
96
     */
97 6
    protected function doHas(string $key): bool
98
    {
99 6
        return isset($this->pool[$key]);
100
    }
101
}
102