MemcachedBucketStore::timeout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 */
13
14
namespace Ytake\LaravelCouchbase\Cache;
15
16
use Illuminate\Cache\MemcachedStore;
17
18
/**
19
 * Class MemcachedBucketStore.
20
 *
21
 * @author Yuuki Takezawa<[email protected]>
22
 * @deprecated
23
 */
24
class MemcachedBucketStore extends MemcachedStore
25
{
26
    /** @var  string[] */
27
    protected $servers;
28
29
    /** @var array */
30
    protected $options = [
31
        CURLOPT_RETURNTRANSFER => false,
32
        CURLOPT_SSL_VERIFYPEER => false,
33
    ];
34
35
    /** @var int */
36
    protected $port = 8091;
37
38
    /** @var int */
39
    protected $timeout = 1;
40
41
    /** @var string */
42
    protected $flushEndpoint = ':%s/pools/default/buckets/%s/controller/doFlush';
43
44
    /** @var array */
45
    protected $sasl = [];
46
47
    /**
48
     * MemcachedBucketStore constructor.
49
     *
50
     * @param \Memcached $memcached
51
     * @param string     $prefix
52
     * @param array      $servers
53
     * @param array      $sasl
54
     */
55 10
    public function __construct(
56
        \Memcached $memcached,
57
        string $prefix = '',
58
        array $servers,
59
        array $sasl = []
60
    ) {
61 10
        parent::__construct($memcached, $prefix);
62 10
        $this->servers = $servers;
63 10
        $this->sasl = $sasl;
64 10
    }
65
66
    /**
67
     * Increment the value of an item in the cache.
68
     *
69
     * @param string $key
70
     * @param mixed  $value
71
     *
72
     * @return int|bool
73
     */
74 4
    public function increment($key, $value = 1)
75
    {
76 4
        if ($integer = $this->get($key)) {
77 2
            $this->put($key, $integer + $value, 0);
78
79 2
            return $integer + $value;
80
        }
81
82 4
        $this->put($key, $value, 0);
83
84 4
        return $value;
85
    }
86
87
    /**
88
     * Decrement the value of an item in the cache.
89
     *
90
     * @param string $key
91
     * @param mixed  $value
92
     *
93
     * @return int|bool
94
     */
95 4
    public function decrement($key, $value = 1)
96
    {
97 4
        $decrement = 0;
98 4
        if ($integer = $this->get($key)) {
99 2
            $decrement = $integer - $value;
0 ignored issues
show
Bug Compatibility introduced by
The expression $integer - $value; of type integer|double adds the type double to the return on line 105 which is incompatible with the return type declared by the interface Illuminate\Contracts\Cache\Store::decrement of type integer|boolean.
Loading history...
100 2
            if ($decrement <= 0) {
101 2
                $decrement = 0;
102
            }
103 2
            $this->put($key, $decrement, 0);
104
105 2
            return $decrement;
106
        }
107
108 4
        $this->put($key, $decrement, 0);
109
110 4
        return $decrement;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 2
    public function flush()
117
    {
118 2
        $handler = curl_multi_init();
119 2
        foreach ($this->servers as $server) {
120 2
            $initialize = curl_init();
121 2
            $configureOption = (isset($server['options'])) ? $server['options'] : [];
122 2
123
            $options = array_replace($this->options, [
124 2
                CURLOPT_POST => true,
125 2
                CURLOPT_URL  => $server['host'] . sprintf($this->flushEndpoint, $this->port, $server['bucket']),
126 2
            ], $configureOption, $this->setCredential());
127 2
            curl_setopt_array($initialize, $options);
128 2
            curl_multi_add_handle($handler, $initialize);
129 2
        }
130
        $this->callMulti($handler);
131 2
    }
132 2
133
    /**
134
     * @return array
135
     */
136
    protected function setCredential(): array
137 2
    {
138
        if (count($this->sasl) === 2) {
139 2
            list($username, $password) = $this->sasl;
140 2
141
            return [CURLOPT_USERPWD => "{$username}:{$password}"];
142 2
        }
143
144
        return [];
145
    }
146
147
    /**
148
     * @param $handler
149
     *
150
     * @throws \RuntimeException
151
     */
152
    protected function callMulti($handler)
153 2
    {
154
        $running = null;
155 2
156
        do {
157
            $stat = curl_multi_exec($handler, $running);
158 2
        } while ($stat === CURLM_CALL_MULTI_PERFORM);
159 2
        if (!$running || $stat !== CURLM_OK) {
160 2
            throw new \RuntimeException('failed to initialized cURL');
161
        }
162
163
        do {
164
            curl_multi_select($handler, $this->timeout);
165 2
            do {
166
                $stat = curl_multi_exec($handler, $running);
167 2
            } while ($stat === CURLM_CALL_MULTI_PERFORM);
168 2
            do {
169
                if ($read = curl_multi_info_read($handler, $remains)) {
170 2
                    $response = curl_multi_getcontent($read['handle']);
171 2
172
                    if ($response === false) {
173 2
                        $info = curl_getinfo($read['handle']);
174
                        throw new \RuntimeException("error: {$info['url']}: {$info['http_code']}");
175
                    }
176
                    curl_multi_remove_handle($handler, $read['handle']);
177 2
                    curl_close($read['handle']);
178 2
                }
179
            } while ($remains);
0 ignored issues
show
Bug Best Practice introduced by
The expression $remains of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
180 2
        } while ($running);
181 2
        curl_multi_close($handler);
182 2
    }
183 2
184
    /**
185
     * @param int $second
186
     */
187
    public function timeout(int $second)
188
    {
189
        $this->timeout = $second;
190
    }
191
192
    /**
193
     * @param int $port
194
     */
195
    public function port(int $port)
196
    {
197
        $this->port = $port;
198
    }
199
}
200