Passed
Pull Request — master (#5)
by X
05:23
created

Couchbase::loadStatus()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Couchbase Adapter for ackintosh/ganesha
4
 */
5
namespace GaneshaPlugin\Adapter;
6
7
use Ackintosh\Ganesha;
8
use Ackintosh\Ganesha\Exception\StorageException;
9
use Ackintosh\Ganesha\Configuration;
10
use Ackintosh\Ganesha\Storage\AdapterInterface;
11
use Ackintosh\Ganesha\Storage\Adapter\TumblingTimeWindowInterface;
12
use Couchbase\Bucket;
13
use Couchbase\Exception as CBException;
14
15
/**
16
 * Adapter class using Couchbase
17
 */
18
class Couchbase implements AdapterInterface, TumblingTimeWindowInterface
19
{
20
    /**
21
     * @var \Couchbase\Bucket $bucket
22
     */
23
    private $bucket;
24
25
    /**
26
     * @var \Ackintosh\Ganesha\Configuration $configuration
27
     */
28
    private $configuration;
29
30
    /**
31
     * constructor
32
     *
33
     * @param \Couchbase\Bucket $bucket Couchbase bucket to manage data
34
     */
35
    public function __construct(Bucket $bucket)
36
    {
37
        $this->bucket = $bucket;
38
    }
39
40
    /**
41
     * @override
42
     * @param \Ackintosh\Ganesha\Configuration $configuration circuit breaker configuration
43
     * @return void
44
     */
45
    public function setConfiguration(Configuration $configuration)
46
    {
47
        $this->configuration = $configuration;
48
    }
49
50
    /**
51
     * @param  string $service name of the service
52
     * @return int
53
     * @throws \Ackintosh\Ganesha\Exception\StorageException
54
     */
55
    public function load($service)
56
    {
57
        try {
58
            $doc = $this->bucket->get($service, []);
59
        } catch (CBException $e) {
60
            if ($e->getCode() === COUCHBASE_KEY_ENOENT) {
61
                return 0;
62
            }
63
            throw new StorageException($e->getMessage(), $e->getCode(), $e);
64
        }
65
        return $doc->value;
66
    }
67
68
    /**
69
     * @param  string $service name of the service
70
     * @param  int    $count   success / failure / rejection count of the service
71
     * @return void
72
     * @throws \Ackintosh\Ganesha\Exception\StorageException
73
     */
74
    public function save($service, $count)
75
    {
76
        try {
77
            $this->bucket->upsert($service, $count, $this->getOptions());
78
        } catch (CBException $e) {
79
            throw new StorageException($e->getMessage(), $e->getCode(), $e);
80
        }
81
    }
82
83
    /**
84
     * @param  string $service name of the service
85
     * @return void
86
     * @throws \Ackintosh\Ganesha\Exception\StorageException
87
     */
88
    public function increment($service)
89
    {
90
        try {
91
            $this->bucket->counter($service, 1, $this->getOptions(['initial' => 0]));
92
        } catch (\Couchbase\Exception $e) {
93
            throw new StorageException($e->getMessage(), $e->getCode(), $e);
94
        }
95
    }
96
97
    /**
98
     * decrement failure count
99
     *
100
     * If the operation would decrease the value below 0, the new value must be 0.
101
     *
102
     * @param  string $service name of the service
103
     * @return void
104
     * @throws \Ackintosh\Ganesha\Exception\StorageException
105
     */
106
    public function decrement($service)
107
    {
108
        try {
109
            $this->bucket->counter($service, -1, $this->getOptions(['initial' => 0]));
110
        } catch (\Couchbase\Exception $e) {
111
            throw new StorageException($e->getMessage(), $e->getCode(), $e);
112
        }
113
    }
114
115
    /**
116
     * sets last failure time
117
     *
118
     * @param  string $service         name of the service
119
     * @param  int    $lastFailureTime last failure time (unix time)
120
     * @return void
121
     * @throws \Ackintosh\Ganesha\Exception\StorageException
122
     */
123
    public function saveLastFailureTime($service, $lastFailureTime)
124
    {
125
        try {
126
            $this->bucket->upsert($service, $lastFailureTime, $this->getOptions());
127
        } catch (CBException $e) {
128
            throw new StorageException($e->getMessage(), $e->getCode(), $e);
129
        }
130
    }
131
132
    /**
133
     * returns last failure time
134
     *
135
     * @param string $service name of the service
136
     * @return int | null
137
     * @throws \Ackintosh\Ganesha\Exception\StorageException
138
     */
139
    public function loadLastFailureTime($service)
140
    {
141
        try {
142
            $doc = $this->bucket->get($service, []);
143
        } catch (CBException $e) {
144
            if ($e->getCode() === COUCHBASE_KEY_ENOENT) {
145
                return null;
146
            }
147
            throw new StorageException($e->getMessage(), $e->getCode(), $e);
148
        }
149
        return $doc->value;
150
    }
151
152
    /**
153
     * sets status
154
     *
155
     * @param  string $service name of the service
156
     * @param  int    $status  status of the service
157
     * @return void
158
     * @throws \Ackintosh\Ganesha\Exception\StorageException
159
     */
160
    public function saveStatus($service, $status)
161
    {
162
        try {
163
            $this->bucket->upsert($service, $status, $this->getOptions());
164
        } catch (CBException $e) {
165
            throw new StorageException($e->getMessage(), $e->getCode(), $e);
166
        }
167
    }
168
169
    /**
170
     * returns status
171
     *
172
     * @param  string $service name of the service
173
     * @return int
174
     * @throws \Ackintosh\Ganesha\Exception\StorageException
175
     */
176
    public function loadStatus($service)
177
    {
178
        try {
179
            $doc = $this->bucket->get($service, []);
180
        } catch (CBException $e) {
181
            if ($e->getCode() === COUCHBASE_KEY_ENOENT) {
182
                return Ganesha::STATUS_CALMED_DOWN;
183
            }
184
            throw new StorageException($e->getMessage(), $e->getCode(), $e);
185
        }
186
        return $doc->value;
187
    }
188
189
    /**
190
     * resets all counts
191
     *
192
     * @return void
193
     * @throws \Ackintosh\Ganesha\Exception\StorageException
194
     */
195
    public function reset()
196
    {
197
        throw new \RuntimeException('not implemented');
198
    }
199
200
    /**
201
     * return option parameters for Couchbase operation
202
     *
203
     * @param array $additional additional option
204
     * @return array
205
     */
206
    private function getOptions(array $additional = [])
207
    {
208
        $expiry = isset($this->configuration['timeWindow']) ?
209
                $this->configuration['timeWindow'] * 2 : // current + previous
210
                0;
211
        $initial = [
212
            'expiry' => $expiry,
213
        ];
214
        return array_merge($initial, $additional);
215
    }
216
}
217