Completed
Push — master ( 790544...a0889a )
by yuuki
9s
created

MemcachedBucketStore::port()   A

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