Completed
Push — master ( b2545f...e44389 )
by Divine Niiquaye
02:25
created

NullAdapterTest::flushAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of BiuradPHP opensource projects.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\Cache\Tests\Fixtures;
19
20
use Doctrine\Common\Cache\Cache;
21
22
class NullAdapterTest implements Cache
23
{
24
    /** @var array<mixed,int>*/
25
    private $data = [];
26
27
    /** @var int */
28
    private $hitsCount = 0;
29
30
    /** @var int */
31
    private $missesCount = 0;
32
33
    /** @var int */
34
    private $upTime;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function __construct()
40
    {
41
        $this->upTime = \time();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function fetch($id)
48
    {
49
        if (!$this->contains($id)) {
50
            $this->missesCount += 1;
51
52
            return false;
53
        }
54
55
        $this->hitsCount += 1;
56
57
        return $this->data[$id][0];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function contains($id)
64
    {
65
        if (!isset($this->data[$id])) {
66
            return false;
67
        }
68
69
        $expiration = $this->data[$id][1];
70
71
        if ($expiration && $expiration < \time()) {
72
            $this->delete($id);
73
74
            return false;
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function save($id, $data, $lifeTime = 0)
84
    {
85
        $this->data[$id] = [$data, $lifeTime ? \time() + $lifeTime : false];
86
87
        return true;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function delete($id)
94
    {
95
        unset($this->data[$id]);
96
97
        return true;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function flushAll()
104
    {
105
        $this->data = [];
106
107
        return true;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getStats()
114
    {
115
        return [
116
            Cache::STATS_HITS             => $this->hitsCount,
117
            Cache::STATS_MISSES           => $this->missesCount,
118
            Cache::STATS_UPTIME           => $this->upTime,
119
            Cache::STATS_MEMORY_USAGE     => null,
120
            Cache::STATS_MEMORY_AVAILABLE => null,
121
        ];
122
    }
123
124
    public function getNamespace()
125
    {
126
        return '';
127
    }
128
}
129