Completed
Push — master ( 3faadf...57140e )
by Klaus
11:44
created

ApcuAdapter::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
4
namespace Linio\Component\Cache\Adapter;
5
6
use Linio\Component\Cache\Exception\KeyNotFoundException;
7
8
class ApcuAdapter extends AbstractAdapter implements AdapterInterface
9
{
10
    /**
11
     * @var int
12
     */
13
    protected $ttl;
14
15
    public function __construct(array $config = [])
16
    {
17
        // default config
18
        $this->ttl = 0;
19
20
        // config
21
        if (isset($config['ttl'])) {
22
            $this->ttl = $config['ttl'];
23
        }
24
25
        if (isset($config['cache_not_found_keys'])) {
26
            $this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys'];
27
        }
28
    }
29
30
    public function get(string $key)
1 ignored issue
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
31
    {
32
        $value = apcu_fetch($this->addNamespaceToKey($key), $success);
0 ignored issues
show
Bug introduced by
The variable $success does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
33
34
        if (!$success) {
35
            throw new KeyNotFoundException();
36
        }
37
38
        return $value;
39
    }
40
41
    public function getMulti(array $keys): array
42
    {
43
        $values = [];
44
45
        foreach ($keys as $key) {
46
            $value = apcu_fetch($this->addNamespaceToKey($key), $success);
0 ignored issues
show
Bug introduced by
The variable $success does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47
            if ($success) {
48
                $values[$key] = $value;
49
            }
50
        }
51
52
        return $values;
53
    }
54
55
    public function set(string $key, $value): bool
56
    {
57
        return apcu_add($this->addNamespaceToKey($key), $value, $this->ttl);
58
    }
59
60
    public function setMulti(array $data): bool
61
    {
62
        $namespacedData = $this->addNamespaceToKeys($data, true);
63
        $errors = apcu_add($namespacedData, $this->ttl);
64
65
        return empty($errors);
66
    }
67
68
    public function contains(string $key): bool
69
    {
70
        return apcu_exists($this->addNamespaceToKey($key));
71
    }
72
73
    public function delete(string $key): bool
74
    {
75
        apcu_delete($this->addNamespaceToKey($key));
76
77
        return true;
78
    }
79
80
    public function deleteMulti(array $keys): bool
81
    {
82
        foreach ($keys as $key) {
83
            apcu_delete($this->addNamespaceToKey($key));
84
        }
85
86
        return true;
87
    }
88
89
    public function flush(): bool
90
    {
91
        return apcu_clear_cache();
92
    }
93
}
94