Completed
Push — master ( 8ad0a5...af5d94 )
by yuuki
10s
created

MemcachedBucketStore::decrement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 17
ccs 0
cts 14
cp 0
crap 12
rs 9.4285
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
    public function __construct($memcached, $prefix = '', array $servers)
50
    {
51
        parent::__construct($memcached, $prefix);
52
        $this->servers = $servers;
53
    }
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
    public function increment($key, $value = 1)
64
    {
65
        if ($integer = $this->get($key)) {
66
            $this->put($key, $integer + $value, 0);
67
68
            return $integer + $value;
69
        }
70
71
        $this->put($key, $value, 0);
72
73
        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
    public function decrement($key, $value = 1)
85
    {
86
        $decrement = 0;
87
        if ($integer = $this->get($key)) {
88
            $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
            if ($decrement <= 0) {
90
                $decrement = 0;
91
            }
92
            $this->put($key, $decrement, 0);
93
94
            return $decrement;
95
        }
96
97
        $this->put($key, $decrement, 0);
98
99
        return $decrement;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function flush()
106
    {
107
        $handler = curl_multi_init();
108
        $initialize = null;
0 ignored issues
show
Unused Code introduced by
$initialize is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
        foreach ($this->servers as $server) {
110
            $initialize = curl_init();
111
            $configureOption = (isset($server['options'])) ? $server['options'] : [];
112
113
            $options = array_replace($this->options, [
114
                CURLOPT_POST => true,
115
                CURLOPT_URL => $server['host'].sprintf($this->flushEndpoint, $this->port, $server['bucket']),
116
            ], $configureOption);
117
            curl_setopt_array($initialize, $options);
118
            curl_multi_add_handle($handler, $initialize);
119
        }
120
121
        $this->callMulti($handler);
122
    }
123
124
    /**
125
     * @param $handler
126
     *
127
     * @throws \RuntimeException
128
     */
129
    protected function callMulti($handler)
130
    {
131
        $running = null;
132
133
        do {
134
            $stat = curl_multi_exec($handler, $running);
135
        } while ($stat === CURLM_CALL_MULTI_PERFORM);
136
        if (!$running || $stat !== CURLM_OK) {
137
            throw new \RuntimeException('failed to initialized cURL');
138
        }
139
140
        do {
141
            curl_multi_select($handler, $this->timeout);
142
            do {
143
                $stat = curl_multi_exec($handler, $running);
144
            } while ($stat === CURLM_CALL_MULTI_PERFORM);
145
            do {
146
                if ($read = curl_multi_info_read($handler, $remains)) {
147
                    $response = curl_multi_getcontent($read['handle']);
148
149
                    if ($response === false) {
150
                        $info = curl_getinfo($read['handle']);
151
                        throw new \RuntimeException("error: {$info['url']}: {$info['http_code']}");
152
                    }
153
                    curl_multi_remove_handle($handler, $read['handle']);
154
                    curl_close($read['handle']);
155
                }
156
            } 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...
157
        } while ($running);
158
        curl_multi_close($handler);
159
    }
160
161
    /**
162
     * @param int $second
163
     */
164
    public function timeout($second)
165
    {
166
        $this->timeout = $second;
167
    }
168
169
    /**
170
     * @param int $port
171
     */
172
    public function port($port)
173
    {
174
        $this->port = $port;
175
    }
176
}
177