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.

Memcached::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Kotori.php
4
 *
5
 * A Tiny Model-View-Controller PHP Framework
6
 *
7
 * This content is released under the Apache 2 License
8
 *
9
 * Copyright (c) 2015-2017 Kotori Technology. All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 *     http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23
24
/**
25
 * Memcached Caching Class
26
 *
27
 * @package     Kotori
28
 * @subpackage  Cache
29
 * @author      Kokororin
30
 * @link        https://kotori.love
31
 */
32
namespace Kotori\Core\Cache;
33
34
use Kotori\Core\Container;
35
use Kotori\Debug\Hook;
36
use Kotori\Exception\CacheException;
37
38
class Memcached
39
{
40
    /**
41
     * Holds the memcached object
42
     *
43
     * @var object
44
     */
45
    protected $memcached;
46
47
    /**
48
     * Memcached configuration
49
     *
50
     * @var array
51
     */
52
    protected $memcacheConf = [
53
        'default' => [
54
            'host' => '127.0.0.1',
55
            'port' => 11211,
56
            'weight' => 1,
57
        ],
58
    ];
59
60
    /**
61
     * Class constructor
62
     *
63
     * Setup Memcache(d)
64
     *
65
     * @param  array $config
66
     *
67
     * @throws \Kotori\Exception\CacheException
68
     */
69 1
    public function __construct($config = [])
70
    {
71
        // Try to load memcached server info from the config file.
72 1
        $defaults = $this->memcacheConf['default'];
73 1
        if (empty($config)) {
74 1
            $config = Container::get('config')->get('cache');
75
        }
76
77 1
        $memcacheConf = isset($config['memcached']) ? $config['memcached'] : null;
78
79 1
        if (is_array($memcacheConf)) {
80
            $this->memcacheConf = [];
81
82
            foreach ($memcacheConf as $name => $conf) {
83
                $this->memcacheConf[$name] = $conf;
84
            }
85
        }
86
87 1
        if (class_exists('Memcached', false)) {
88 1
            $this->memcached = new \Memcached();
89
        } elseif (class_exists('Memcache', false)) {
90
            $this->memcached = new \Memcache();
91
        } else {
92
            throw new CacheException('Failed to create Memcache(d) object; extension not loaded?');
93
        }
94
95 1
        foreach ($this->memcacheConf as $cacheServer) {
96 1
            if (!isset($cacheServer['host'])) {
97
                $cacheServer['host'] = $defaults['host'];
98
            }
99
100 1
            if (!isset($cacheServer['port'])) {
101
                $cacheServer['port'] = $defaults['port'];
102
            }
103
104 1
            if (!isset($cacheServer['weight'])) {
105
                $cacheServer['weight'] = $defaults['weight'];
106
            }
107
108 1
            if (get_class($this->memcached) === 'Memcache') {
109
                // Third parameter is persistance and defaults to TRUE.
110
                $this->memcached->addServer(
111
                    $cacheServer['host'],
112
                    $cacheServer['port'],
113
                    true,
114
                    $cacheServer['weight']
115
                );
116
            } else {
117 1
                $this->memcached->addServer(
118 1
                    $cacheServer['host'],
119 1
                    $cacheServer['port'],
120 1
                    $cacheServer['weight']
121
                );
122
            }
123
        }
124
125 1
        Hook::listen(__CLASS__);
126 1
    }
127
128
    /**
129
     * Fetch from cache
130
     *
131
     * @param  string $key
132
     * @return mixed
133
     */
134 6
    public function get($key)
135
    {
136 6
        $value = $this->memcached->get($key);
137
138 6
        return is_array($value) ? $value[0] : $value;
139
    }
140
141
    /**
142
     * Set
143
     *
144
     * @param  string   $key
145
     * @param  mixed    $value
146
     * @param  int      $ttl
147
     * @param  boolean  $raw
148
     * @return boolean
149
     */
150 6
    public function set($key, $value, $ttl = 60, $raw = false)
151
    {
152 6
        if ($raw !== true) {
153 6
            $value = [$value, time(), $ttl];
154
        }
155
156 6
        if (get_class($this->memcached) === 'Memcached') {
157 6
            return $this->memcached->set($key, $value, $ttl);
158
        } elseif (get_class($this->memcached) === 'Memcache') {
159
            return $this->memcached->set($key, $value, 0, $ttl);
160
        }
161
162
        return false;
163
    }
164
165
    /**
166
     * Delete from Cache
167
     *
168
     * @param  mixed    $key
169
     * @return boolean
170
     */
171 3
    public function delete($key)
172
    {
173 3
        return $this->memcached->delete($key);
174
    }
175
176
    /**
177
     * Clean the Cache
178
     *
179
     * @return boolean
180
     */
181 1
    public function clear()
182
    {
183 1
        return $this->memcached->flush();
184
    }
185
186
    /**
187
     * Is supported
188
     *
189
     * Returns FALSE if memcached is not supported on the system.
190
     * If it is, we setup the memcached object & return TRUE
191
     *
192
     * @return boolean
193
     */
194 1
    public function isSupported()
195
    {
196 1
        return (extension_loaded('memcached') || extension_loaded('memcache'));
197
    }
198
}
199