SimpleCacheActionLogger::getActions()   A
last analyzed

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 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 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Test\Support\SimpleCache;
6
7
use DateInterval;
8
use Psr\SimpleCache\CacheInterface;
9
use Traversable;
10
11
/**
12
 * @template TCacheService as CacheInterface
13
 */
14
final class SimpleCacheActionLogger implements CacheInterface
15
{
16
    /** @var Action[] */
17
    private array $actions = [];
18
19
    /**
20
     * `SimpleCacheActionLogger` constructor.
21
     *
22
     * @param TCacheService $cacheService
0 ignored issues
show
Bug introduced by
The type Yiisoft\Test\Support\SimpleCache\TCacheService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
     */
24 137
    public function __construct(
25
        private CacheInterface $cacheService,
26
        array $cacheData = []
27
    ) {
28 137
        $this->cacheService->setMultiple($cacheData);
29
    }
30
31 73
    public function get(string $key, mixed $default = null): mixed
32
    {
33 73
        $this->actions[] = Action::createGetAction($key);
34 73
        return $this->cacheService->get($key, $default);
35
    }
36
37 21
    public function delete(string $key): bool
38
    {
39 21
        $this->actions[] = Action::createDeleteAction($key);
40 21
        return $this->cacheService->delete($key);
41
    }
42
43 34
    public function has(string $key): bool
44
    {
45 34
        $this->actions[] = Action::createHasAction($key);
46 34
        return $this->cacheService->has($key);
47
    }
48
49 84
    public function clear(): bool
50
    {
51 84
        $this->actions[] = Action::createClearAction();
52 84
        return $this->cacheService->clear();
53
    }
54
55 89
    public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
56
    {
57 89
        $this->actions[] = Action::createSetAction($key, $value, $ttl);
58 89
        return $this->cacheService->set($key, $value, $ttl);
59
    }
60
61 15
    public function getMultiple(iterable $keys, mixed $default = null): iterable
62
    {
63 15
        $keys = $this->iterableToArray($keys);
64 15
        foreach ($keys as $key) {
65 15
            $this->actions[] = Action::createGetAction($key);
66
        }
67 15
        return $this->cacheService->getMultiple($keys, $default);
68
    }
69
70 20
    public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
71
    {
72 20
        $values = $this->iterableToArray($values);
73 20
        foreach ($values as $key => $value) {
74 20
            $this->actions[] = Action::createSetAction($key, $value, $ttl);
75
        }
76 20
        return $this->cacheService->setMultiple($values, $ttl);
77
    }
78
79 9
    public function deleteMultiple(iterable $keys): bool
80
    {
81 9
        $keys = $this->iterableToArray($keys);
82 9
        foreach ($keys as $key) {
83 9
            $this->actions[] = Action::createDeleteAction($key);
84
        }
85 9
        return $this->cacheService->deleteMultiple($keys);
86
    }
87
88
    /**
89
     * @return Action[]
90
     */
91 1
    public function getActions(): array
92
    {
93 1
        return $this->actions;
94
    }
95
96
    /**
97
     * @return array<int, array{0: string, 1: mixed}>
98
     */
99 2
    public function getActionKeyList(): array
100
    {
101 2
        $result = [];
102 2
        foreach ($this->actions as $action) {
103 2
            $result[] = [$action->getAction(), $action->getKey()];
104
        }
105 2
        return $result;
106
    }
107
108
    /**
109
     * @return TCacheService
110
     */
111 1
    public function getCacheService(): CacheInterface
112
    {
113 1
        return $this->cacheService;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cacheService returns the type Psr\SimpleCache\CacheInterface which is incompatible with the documented return type Yiisoft\Test\Support\SimpleCache\TCacheService.
Loading history...
114
    }
115
116
    /**
117
     * Converts iterable to array.
118
     *
119
     * @psalm-template T
120
     * @psalm-param iterable<T> $iterable
121
     * @psalm-return array<array-key,T>
122
     */
123 34
    private function iterableToArray(iterable $iterable): array
124
    {
125 34
        return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $iterable instanc...($iterable) : $iterable could return the type iterable which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
126
    }
127
}
128