GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Adapter::getInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Cache\Adapters\Memcached;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Cache\Abstracts\AbstractAdapter;
19
use O2System\Cache\DataStructures\Config;
20
use O2System\Spl\Exceptions\Logic\BadFunctionCall\BadMethodCallException;
21
22
/**
23
 * Class Adapter
24
 *
25
 * @package O2System\Cache\Adapters\File
26
 */
27
abstract class Adapter extends AbstractAdapter
28
{
29
    /**
30
     * Adapter::$platform
31
     *
32
     * Adapter Platform Name
33
     *
34
     * @var string
35
     */
36
    protected $platform = 'Memcached';
37
38
    // ------------------------------------------------------------------------
39
40
    /**
41
     * Adapter::$memchached
42
     *
43
     * Memcached Instance
44
     *
45
     * @var \Memcached
46
     */
47
    protected $memcached;
48
49
    // ------------------------------------------------------------------------
50
51
    /**
52
     * AbstractAdapter::__construct
53
     *
54
     * @param Config|NULL $config
55
     *
56
     * @return Adapter
57
     */
58
    public function __construct(Config $config = null)
59
    {
60
        if (isset($config)) {
61
            if ($this->isSupported()) {
62
                $this->connect($config->getArrayCopy());
63
64
                if ($config->offsetExists('prefixKey')) {
65
                    $this->setPrefixKey($config->prefixKey);
0 ignored issues
show
Bug Best Practice introduced by
The property prefixKey does not exist on O2System\Cache\DataStructures\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
                }
67
            }
68
        } elseif ($this->isSupported()) {
69
            $this->connect([]);
70
        }
71
    }
72
73
    // ------------------------------------------------------------------------
74
75
    /**
76
     * Adapter::isSupported
77
     *
78
     * Checks if this adapter is supported on this system.
79
     *
80
     * @return bool Returns FALSE if not supported.
81
     */
82
    public function isSupported()
83
    {
84
        return (bool)(extension_loaded('memcached') && class_exists('Memcached', false));
85
    }
86
87
    // ------------------------------------------------------------------------
88
89
    /**
90
     * Adapter::connect
91
     *
92
     * @param array $config Cache adapter connection configuration.
93
     *
94
     * @return void
95
     */
96
    public function connect(array $config)
97
    {
98
        $this->memcached = new \Memcached();
99
        $this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, false);
100
101
        if (empty($config)) {
102
            $this->config = [
103
                'host'   => '127.0.0.1',
104
                'port'   => 11211,
105
                'weight' => 1,
106
            ];
107
108
            $this->memcached->addserver($this->config[ 'host' ], $this->config[ 'port' ]);
109
        } elseif (isset($config[ 'servers' ])) {
110
            foreach ($config[ 'servers' ] as $server) {
111
                $this->config[ $server[ 'host' ] ] = array_merge(
112
                    [
113
                        'host'   => '127.0.0.1',
114
                        'port'   => 11211,
115
                        'weight' => 1,
116
                    ],
117
                    $server
118
                );
119
120
                if (array_key_exists('status', $server)) {
121
                    if ($server[ 'status' ] === false) {
122
                        $this->config[ $server[ 'host' ] ][ 'retryInterval' ] = -1;
123
124
                        $this->memcached->addserver(
125
                            $this->config[ $server[ 'host' ] ][ 'host' ],
126
                            $this->config[ $server[ 'host' ] ][ 'port' ],
127
                            $this->config[ $server[ 'host' ] ][ 'weight' ]
128
                        );
129
130
                        continue;
131
                    }
132
                }
133
134
                $this->memcached->addserver(
135
                    $this->config[ $server[ 'host' ] ][ 'host' ],
136
                    $this->config[ $server[ 'host' ] ][ 'port' ],
137
                    $this->config[ $server[ 'host' ] ][ 'weight' ]
138
                );
139
            }
140
        } else {
141
            $this->config = array_merge(
142
                [
143
                    'host'   => '127.0.0.1',
144
                    'port'   => 11211,
145
                    'weight' => 1,
146
                ],
147
                $config
148
            );
149
150
            if (isset($this->config[ 'status' ])) {
151
                if ($this->config[ 'status' ] === false) {
152
                    $this->memcached->addserver(
153
                        $this->config[ 'host' ],
154
                        $this->config[ 'port' ],
155
                        $this->config[ 'weight' ]
156
                    );
157
                } else {
158
                    $this->memcached->addserver(
159
                        $this->config[ 'host' ],
160
                        $this->config[ 'port' ],
161
                        $this->config[ 'weight' ]
162
                    );
163
                }
164
            } else {
165
                $this->memcached->addserver(
166
                    $this->config[ 'host' ],
167
                    $this->config[ 'port' ],
168
                    $this->config[ 'weight' ]
169
                );
170
            }
171
        }
172
    }
173
174
    // ------------------------------------------------------------------------
175
176
    /**
177
     * Adapter::isConnected
178
     *
179
     * Checks if this adapter has a successful connection.
180
     *
181
     * @return bool Returns FALSE if not supported.
182
     */
183
    public function isConnected()
184
    {
185
        return (bool)($this->memcached instanceof \Memcached);
186
    }
187
188
    /**
189
     * Adapter::__call
190
     *
191
     * @param string $method    Memcache Adapter Class / Memcache Class / Memcache ItemPool Class method name.
192
     * @param array  $arguments Method arguments.
193
     *
194
     * @return mixed
195
     * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadMethodCallException
196
     */
197
    public function __call($method, array $arguments = [])
198
    {
199
        if (method_exists($this, $method)) {
200
            return call_user_func_array([&$this, $method], $arguments);
201
        } elseif ($this->memcached instanceof \Memcache AND method_exists($this->memcached, $method)) {
0 ignored issues
show
introduced by
$this->memcached is never a sub-type of Memcache.
Loading history...
202
            return call_user_func_array([&$this->memcached, $method], $arguments);
203
        }
204
205
        throw new BadMethodCallException('E_BAD_METHOD_CALL_CACHE_EXCEPTION', 0, [__CLASS__ . '::' . $method]);
206
    }
207
208
    // ------------------------------------------------------------------------
209
210
    /**
211
     * Adapter::increment
212
     *
213
     * Increment a raw value offset.
214
     *
215
     * @param string $key  Cache item key.
216
     * @param int    $step Increment step to add.
217
     *
218
     * @return mixed New value on success or FALSE on failure.
219
     */
220
    public function increment($key, $step = 1)
221
    {
222
        return $this->memcached->increment($this->prefixKey . $key, $step);
223
    }
224
225
    // ------------------------------------------------------------------------
226
227
    /**
228
     * Adapter::decrement
229
     *
230
     * Decrement a raw value offset.
231
     *
232
     * @param string $key  Cache item key.
233
     * @param int    $step Decrement step to add.
234
     *
235
     * @return mixed New value on success or FALSE on failure.
236
     */
237
    public function decrement($key, $step = 1)
238
    {
239
        return $this->memcached->decrement($this->prefixKey . $key, $step);
240
    }
241
242
    // ------------------------------------------------------------------------
243
244
    /**
245
     * Adapter::getInfo
246
     *
247
     * Gets item pool adapter info.
248
     *
249
     * @return mixed
250
     */
251
    public function getInfo()
252
    {
253
        return [
254
            'version' => $this->memcached->getVersion(),
255
            'servers' => $this->memcached->getServerList(),
256
        ];
257
    }
258
259
    // ------------------------------------------------------------------------
260
261
    /**
262
     * Adapter::getInfo
263
     *
264
     * Gets item pool adapter stats.
265
     *
266
     * @return mixed
267
     */
268
    public function getStats()
269
    {
270
        return $this->memcached->getStats();
271
    }
272
}