Passed
Push — master ( c82647...c877fa )
by Georges
11:01
created

Driver::driverRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Drivers\Memory;
18
19
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
20
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
21
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
22
use Phpfastcache\Entities\DriverStatistic;
23
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
24
use Phpfastcache\Exceptions\PhpfastcacheInvalidTypeException;
25
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
26
use Psr\Cache\CacheItemInterface;
27
28
/**
29
 * Class Driver
30
 * @method Config getConfig()
31
 */
32
class Driver implements ExtendedCacheItemPoolInterface
33
{
34
    use TaggableCacheItemPoolTrait;
35
36
    /**
37
     * @var array<string, array<string, mixed>>
38
     */
39
    protected array $staticStack = [];
40
41
    /**
42
     * @return bool
43
     */
44
    public function driverCheck(): bool
45
    {
46
        return true;
47
    }
48
49
    /**
50
     * @return bool
51
     */
52
    protected function driverConnect(): bool
53
    {
54
        return true;
55
    }
56
57
    /**
58
     * @param ExtendedCacheItemInterface $item
59
     * @return ?array<string, mixed>
60
     */
61
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
62
    {
63
        return $this->staticStack[$item->getKey()] ?? null;
64
    }
65
66
    /**
67
     * @return array<int, string>
68
     */
69
    protected function driverReadAllKeys(string $pattern = ''): iterable
70
    {
71
        return array_filter(array_keys($this->staticStack), function (string $key) use ($pattern) {
72
            return $pattern
73
                ? preg_match('/' . str_replace('*', '(.*)', $pattern) . '/', $key)
74
                : true;
75
        });
76
    }
77
78
    /**
79
     * @param ExtendedCacheItemInterface $item
80
     * @return bool
81
     * @throws PhpfastcacheInvalidArgumentException
82
     * @throws PhpfastcacheLogicException
83
     */
84
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
85
    {
86
87
        $this->staticStack[$item->getKey()] = $this->driverPreWrap($item);
88
        return true;
89
    }
90
91
    /**
92
     * @param string $key
93
     * @param string $encodedKey
94
     * @return bool
95
     */
96
    protected function driverDelete(string $key, string $encodedKey): bool
97
    {
98
        if (isset($this->staticStack[$key])) {
99
            unset($this->staticStack[$key]);
100
            return true;
101
        }
102
        return false;
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    protected function driverClear(): bool
109
    {
110
        unset($this->staticStack);
111
        $this->staticStack = [];
112
        return true;
113
    }
114
115
    /**
116
     * @return DriverStatistic
117
     */
118
    public function getStats(): DriverStatistic
119
    {
120
        $stat = new DriverStatistic();
121
        $stat->setInfo('[Memstatic] A memory static driver')
122
            ->setSize(mb_strlen(serialize($this->staticStack)))
123
            ->setData(implode(', ', array_keys($this->itemInstances)))
124
            ->setRawData($this->staticStack);
125
126
        return $stat;
127
    }
128
}
129