Completed
Pull Request — master (#29)
by Carlos
02:19
created

MemcachedAdapter::validateMemcacheConfiguration()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 5.1379
c 0
b 0
f 0
cc 14
eloc 12
nc 9
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 (isset($config['cache_not_found_keys'])) {
48
            $this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys'];
49
        }
50
    }
51
52
    public function get(string $key)
53
    {
54
        $value = $this->memcached->get($this->addNamespaceToKey($key));
55
56
        if ($this->memcached->getResultCode() == Memcached::RES_NOTFOUND) {
57
            throw new KeyNotFoundException();
58
        }
59
60
        return $value;
61
    }
62
63
    public function getMulti(array $keys): array
64
    {
65
        $namespacedKeys = $this->memcached->getMulti($this->addNamespaceToKeys($keys));
66
67
        return $this->removeNamespaceFromKeys($namespacedKeys);
68
    }
69
70
    public function set(string $key, $value): bool
71
    {
72
        return $this->memcached->set($this->addNamespaceToKey($key), $value, $this->ttl);
73
    }
74
75
    public function setMulti(array $data): bool
76
    {
77
        $namespacedData = $this->addNamespaceToKeys($data, true);
78
79
        return $this->memcached->setMulti($namespacedData, $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($this->addNamespaceToKey($key));
96
97
        return true;
98
    }
99
100
    public function deleteMulti(array $keys): bool
101
    {
102
        $namespacedKeys = $this->addNamespaceToKeys($keys);
103
104
        $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...
105
106
        return true;
107
    }
108
109
    public function flush(): bool
110
    {
111
        return $this->memcached->flush();
112
    }
113
114
    protected function validateMemcacheConfiguration(array $config)
115
    {
116
        if (!array_key_exists('servers', $config)) {
117
            throw new InvalidConfigurationException('Missing configuration parameter: servers');
118
        }
119
120
        if (!is_array($config['servers'])) {
121
            throw new InvalidConfigurationException('Invalid configuration parameter: servers');
122
        }
123
124
        foreach ($config['servers'] as $server) {
125
            if (!is_array($server) || count($server) < 2 || count($server) > 3 || !is_numeric($server[1]) || (isset($server[2]) && !is_numeric($server[2]))) {
126
                throw new InvalidConfigurationException('Invalid configuration parameter: servers');
127
            }
128
        }
129
130
        if (array_key_exists('options', $config) && !is_array($config['options'])) {
131
            throw new InvalidConfigurationException('Invalid configuration parameter: options');
132
        }
133
134
        if (isset($config['ttl']) && !is_numeric($config['ttl'])) {
135
            throw new InvalidConfigurationException('Invalid configuration parameter: ttl');
136
        }
137
    }
138
}
139