Completed
Pull Request — master (#46)
by Klaus
09:50
created

ArrayAdapter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 80
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Cache\Adapter;
6
7
use Linio\Component\Cache\Exception\KeyNotFoundException;
8
9
class ArrayAdapter extends AbstractAdapter implements AdapterInterface
10
{
11
    protected array $cacheData = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
12
13
    public function __construct(array $config = [])
14
    {
15
        if (isset($config['cache_not_found_keys'])) {
16
            $this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys'];
17
        }
18
    }
19
20
    /**
21
     * @return mixed
22
     */
23
    public function get(string $key)
24
    {
25
        if (!array_key_exists($this->addNamespaceToKey($key), $this->cacheData)) {
26
            throw new KeyNotFoundException();
27
        }
28
29
        return $this->cacheData[$this->addNamespaceToKey($key)];
30
    }
31
32
    public function getMulti(array $keys): array
33
    {
34
        $values = [];
35
36
        foreach ($keys as $key) {
37
            if (array_key_exists($this->addNamespaceToKey($key), $this->cacheData)) {
38
                $values[$key] = $this->cacheData[$this->addNamespaceToKey($key)];
39
            }
40
        }
41
42
        return $values;
43
    }
44
45
    /**
46
     * @param mixed $value
47
     */
48
    public function set(string $key, $value): bool
49
    {
50
        $this->cacheData[$this->addNamespaceToKey($key)] = $value;
51
52
        return true;
53
    }
54
55
    public function setMulti(array $data): bool
56
    {
57
        foreach ($data as $key => $value) {
58
            $this->cacheData[$this->addNamespaceToKey($key)] = $value;
59
        }
60
61
        return true;
62
    }
63
64
    public function contains(string $key): bool
65
    {
66
        return array_key_exists($this->addNamespaceToKey($key), $this->cacheData);
67
    }
68
69
    public function delete(string $key): bool
70
    {
71
        unset($this->cacheData[$this->addNamespaceToKey($key)]);
72
73
        return true;
74
    }
75
76
    public function deleteMulti(array $keys): bool
77
    {
78
        foreach ($keys as $key) {
79
            unset($this->cacheData[$this->addNamespaceToKey($key)]);
80
        }
81
82
        return true;
83
    }
84
85
    public function flush(): bool
86
    {
87
        $this->cacheData = [];
88
89
        return true;
90
    }
91
}
92