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

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