Completed
Pull Request — master (#31)
by Carlos
02:26 queued 14s
created

MemcachedAdapter::__construct()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 32
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Cache\Adapter;
6
7
use Linio\Component\Cache\Exception\InvalidConfigurationException;
8
use Linio\Component\Cache\Exception\KeyNotFoundException;
9
use Memcached;
10
11
class MemcachedAdapter extends AbstractAdapter implements AdapterInterface
12
{
13
    /**
14
     * @var int
15
     */
16
    protected $ttl;
17
18
    /**
19
     * @var Memcached
20
     */
21
    protected $memcached;
22
23
    public function __construct(array $config = [])
24
    {
25
        $this->validateMemcacheConfiguration($config);
26
27
        // default config
28
        $this->ttl = 0;
29
30
        // config
31
        if (isset($config['ttl'])) {
32
            $this->ttl = $config['ttl'];
33
        }
34
35
        $persistentId = null;
36
        if (isset($config['persistent_id'])) {
37
            $persistentId = (string) $config['persistent_id'];
38
        }
39
40
        $this->memcached = new Memcached($persistentId);
41
        $this->memcached->addServers($config['servers']);
42
43
        if (isset($config['options']) && !empty($config['options'])) {
44
            $this->memcached->setOptions($config['options']);
0 ignored issues
show
Bug introduced by
The method setOptions() does not exist on Memcached. Did you maybe mean setOption()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
45
        }
46
47
        if ($this->namespace) {
48
            $this->memcached->setOption(Memcached::OPT_PREFIX_KEY, $this->namespace . ':');
49
        }
50
51
        if (isset($config['cache_not_found_keys'])) {
52
            $this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys'];
53
        }
54
    }
55
56
    public function get(string $key)
57
    {
58
        $value = $this->memcached->get($key);
59
60
        if ($this->memcached->getResultCode() == Memcached::RES_NOTFOUND) {
61
            throw new KeyNotFoundException();
62
        }
63
64
        return $value;
65
    }
66
67
    public function getMulti(array $keys): array
68
    {
69
        return $this->memcached->getMulti($keys);
70
    }
71
72
    public function set(string $key, $value): bool
73
    {
74
        return $this->memcached->set($key, $value, $this->ttl);
75
    }
76
77
    public function setMulti(array $data): bool
78
    {
79
        return $this->memcached->setMulti($data, $this->ttl);
80
    }
81
82
    public function contains(string $key): bool
83
    {
84
        try {
85
            $this->get($key);
86
        } catch (KeyNotFoundException $exception) {
87
            return false;
88
        }
89
90
        return true;
91
    }
92
93
    public function delete(string $key): bool
94
    {
95
        $this->memcached->delete($key);
96
97
        return true;
98
    }
99
100
    public function deleteMulti(array $keys): bool
101
    {
102
        $this->memcached->deleteMulti($keys);
0 ignored issues
show
Bug introduced by
The method deleteMulti() does not exist on Memcached. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
103
104
        return true;
105
    }
106
107
    public function flush(): bool
108
    {
109
        return $this->memcached->flush();
110
    }
111
112
    protected function validateMemcacheConfiguration(array $config)
113
    {
114
        if (!array_key_exists('servers', $config)) {
115
            throw new InvalidConfigurationException('Missing configuration parameter: servers');
116
        }
117
118
        if (!is_array($config['servers'])) {
119
            throw new InvalidConfigurationException('Invalid configuration parameter: servers');
120
        }
121
122
        foreach ($config['servers'] as $server) {
123
            if (!is_array($server) || count($server) < 2 || count($server) > 3 || !is_numeric($server[1]) || (isset($server[2]) && !is_numeric($server[2]))) {
124
                throw new InvalidConfigurationException('Invalid configuration parameter: servers');
125
            }
126
        }
127
128
        if (array_key_exists('options', $config) && !is_array($config['options'])) {
129
            throw new InvalidConfigurationException('Invalid configuration parameter: options');
130
        }
131
132
        if (isset($config['ttl']) && !is_numeric($config['ttl'])) {
133
            throw new InvalidConfigurationException('Invalid configuration parameter: ttl');
134
        }
135
    }
136
}
137