Passed
Pull Request — master (#16)
by Aleksei
02:20
created

SimpleCacheActionLogger::setMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Test\Support\SimpleCache;
6
7
use Psr\SimpleCache\CacheInterface;
8
use Traversable;
9
use Yiisoft\Test\Support\SimpleCache\Exception\InvalidArgumentException;
10
11
/**
12
 * @template TCacheService as CacheInterface
13
 */
14
final class SimpleCacheActionLogger implements CacheInterface
15
{
16
    /** @var Action[] */
17
    private array $actions;
18
    /** @var TCacheService */
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...
19
    private CacheInterface $cacheService;
20
21
    /**
22
     * SimpleCacheActionLogger constructor.
23
     *
24
     * @param array $cacheData
25
     * @param TCacheService $cacheService
26
     */
27 93
    public function __construct(CacheInterface $cacheService, array $cacheData = [])
28
    {
29 93
        $this->cacheService = $cacheService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheService of type Psr\SimpleCache\CacheInterface is incompatible with the declared type Yiisoft\Test\Support\SimpleCache\TCacheService of property $cacheService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30 93
        $this->cacheService->setMultiple($cacheData);
31 93
        $this->actions = [];
32 93
    }
33
34 66
    public function get($key, $default = null)
35
    {
36 66
        $this->actions[] = Action::createGetAction($key);
37 66
        return $this->cacheService->get($key, $default);
38
    }
39
40 13
    public function delete($key): bool
41
    {
42 13
        $this->actions[] = Action::createDeleteAction($key);
43 13
        return $this->cacheService->delete($key);
44
    }
45
46 14
    public function has($key): bool
47
    {
48 14
        $this->actions[] = Action::createHasAction($key);
49 14
        return $this->cacheService->has($key);
50
    }
51
52 83
    public function clear(): bool
53
    {
54 83
        $this->actions[] = Action::createClearAction();
55 83
        return $this->cacheService->clear();
56
    }
57
58 75
    public function set($key, $value, $ttl = null): bool
59
    {
60 75
        $this->actions[] = Action::createSetAction($key, $value, $ttl);
61 75
        return $this->cacheService->set($key, $value, $ttl);
62
    }
63
64 9
    public function getMultiple($keys, $default = null): iterable
65
    {
66 9
        $keys = $this->iterableToArray($keys);
67
        /** @psalm-var mixed $key */
68 8
        foreach ($keys as $key) {
69 8
            $this->actions[] = Action::createGetAction($key);
70
        }
71 8
        return $this->cacheService->getMultiple($keys, $default);
72
    }
73
74 11
    public function setMultiple($values, $ttl = null): bool
75
    {
76 11
        $values = $this->iterableToArray($values);
77
        /** @psalm-var mixed $value */
78 10
        foreach ($values as $key => $value) {
79 10
            $this->actions[] = Action::createSetAction($key, $value, $ttl);
80
        }
81 10
        return $this->cacheService->setMultiple($values, $ttl);
82
    }
83
84 3
    public function deleteMultiple($keys): bool
85
    {
86 3
        $keys = $this->iterableToArray($keys);
87
        /** @psalm-var mixed $key */
88 2
        foreach ($keys as $key) {
89 2
            $this->actions[] = Action::createDeleteAction($key);
90
        }
91 2
        return $this->cacheService->deleteMultiple($keys);
92
    }
93
94
    /**
95
     * @return Action[]
96
     */
97
    public function getActions(): array
98
    {
99
        return $this->actions;
100
    }
101
102
    /**
103
     * @return array<int, array{0: string, 1: mixed}>
104
     */
105
    public function getActionKeyList(): array
106
    {
107
        $result = [];
108
        foreach ($this->actions as $action) {
109
            $result[] = [$action->getAction(), $action->getKey()];
110
        }
111
        return $result;
112
    }
113
114
    /**
115
     * @return TCacheService
116
     */
117
    public function getCacheService(): CacheInterface
118
    {
119
        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...
120
    }
121
122
    /**
123
     * @param mixed $iterable
124
     *
125
     * Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException
126
     */
127 15
    private function iterableToArray($iterable): array
128
    {
129 15
        if (!is_iterable($iterable)) {
130 3
            throw new InvalidArgumentException(sprintf('Iterable is expected, got %s.', gettype($iterable)));
131
        }
132 12
        return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable;
133
    }
134
}
135