Completed
Push — master ( bdda65...748aaa )
by Fernando
04:26 queued 02:09
created

MemcachedAdapter::__construct()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 4.909
c 0
b 0
f 0
cc 9
eloc 16
nc 24
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['connection_persistent']) && $config['connection_persistent']) {
37
            $persistentId = '1';
38
39
            if (isset($config['pool_size']) && $config['pool_size'] > 1) {
40
                $persistentId = (string) mt_rand(1, $config['pool_size']);
41
            }
42
        }
43
44
        $this->memcached = new Memcached($persistentId);
45
        $this->memcached->addServers($config['servers']);
46
47
        if (isset($config['options']) && !empty($config['options'])) {
48
            $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...
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($this->addNamespaceToKey($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
        $namespacedKeys = $this->memcached->getMulti($this->addNamespaceToKeys($keys));
70
71
        return $this->removeNamespaceFromKeys($namespacedKeys);
72
    }
73
74
    public function set(string $key, $value): bool
75
    {
76
        return $this->memcached->set($this->addNamespaceToKey($key), $value, $this->ttl);
77
    }
78
79
    public function setMulti(array $data): bool
80
    {
81
        $namespacedData = $this->addNamespaceToKeys($data, true);
82
83
        return $this->memcached->setMulti($namespacedData, $this->ttl);
84
    }
85
86
    public function contains(string $key): bool
87
    {
88
        try {
89
            $this->get($key);
90
        } catch (KeyNotFoundException $exception) {
91
            return false;
92
        }
93
94
        return true;
95
    }
96
97
    public function delete(string $key): bool
98
    {
99
        $this->memcached->delete($this->addNamespaceToKey($key));
100
101
        return true;
102
    }
103
104
    public function deleteMulti(array $keys): bool
105
    {
106
        $namespacedKeys = $this->addNamespaceToKeys($keys);
107
108
        $this->memcached->deleteMulti($namespacedKeys);
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...
109
110
        return true;
111
    }
112
113
    public function flush(): bool
114
    {
115
        return $this->memcached->flush();
116
    }
117
118
    protected function validateMemcacheConfiguration(array $config)
119
    {
120
        if (!array_key_exists('servers', $config)) {
121
            throw new InvalidConfigurationException('Missing configuration parameter: servers');
122
        }
123
124
        if (!is_array($config['servers'])) {
125
            throw new InvalidConfigurationException('Invalid configuration parameter: servers');
126
        }
127
128
        foreach ($config['servers'] as $server) {
129
            if (!is_array($server) || count($server) < 2 || count($server) > 3 || !is_numeric($server[1]) || (isset($server[2]) && !is_numeric($server[2]))) {
130
                throw new InvalidConfigurationException('Invalid configuration parameter: servers');
131
            }
132
        }
133
134
        if (array_key_exists('options', $config) && !is_array($config['options'])) {
135
            throw new InvalidConfigurationException('Invalid configuration parameter: options');
136
        }
137
138
        if (isset($config['ttl']) && !is_numeric($config['ttl'])) {
139
            throw new InvalidConfigurationException('Invalid configuration parameter: ttl');
140
        }
141
    }
142
}
143