Completed
Push — master ( eeee5c...bea951 )
by Akihito
10s
created

Storage::supportSlidingTimeWindow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ackintosh\Ganesha;
3
4
use Ackintosh\Ganesha\Exception\StorageException;
5
use Ackintosh\Ganesha\Storage\Adapter\TumblingTimeWindowInterface;
6
use Ackintosh\Ganesha\Storage\Adapter\SlidingTimeWindowInterface;
7
use Ackintosh\Ganesha\Storage\AdapterInterface;
8
9
class Storage
10
{
11
    /**
12
     * @var Storage\AdapterInterface
13
     */
14
    private $adapter;
15
16
    /**
17
     * @var callable
18
     */
19
    private $serviceNameDecorator;
20
21
    /**
22
     * @var string
23
     */
24
    const KEY_PREFIX = 'ganesha_';
25
26
    /**
27
     * @var string
28
     */
29
    const KEY_SUFFIX_SUCCESS = '_success';
30
31
    /**
32
     * @var string
33
     */
34
    const KEY_SUFFIX_FAILURE = '_failure';
35
36
    /**
37
     * @var string
38
     */
39
    const KEY_SUFFIX_REJECTION = '_rejection';
40
41
    /**
42
     * @var string
43
     */
44
    const KEY_SUFFIX_LAST_FAILURE_TIME = '_last_failure_time';
45
46
    /**
47
     * @var string
48
     */
49
    const KEY_SUFFIX_STATUS = '_status';
50
51
    /**
52
     * Storage constructor.
53
     *
54
     * @param AdapterInterface $adapter
55
     * @param callable         $serviceNameDecorator
56
     */
57
    public function __construct(AdapterInterface $adapter, $serviceNameDecorator)
58
    {
59
        $this->adapter = $adapter;
60
        $this->serviceNameDecorator = $serviceNameDecorator;
61
    }
62
63
    /**
64
     * returns count
65
     *
66
     * @param  string $key
67
     * @return int
68
     * @throws StorageException
69
     */
70
    private function getCount($key)
71
    {
72
        return $this->adapter->load($key);
73
    }
74
75
    /**
76
     * returns success count
77
     *
78
     * @param  string $key
79
     * @return int
80
     * @throws StorageException
81
     */
82
    public function getSuccessCountByCustomKey($key)
83
    {
84
        return $this->getCount($this->prefix($key) . self::KEY_SUFFIX_SUCCESS);
85
    }
86
87
    /**
88
     * returns failure count
89
     *
90
     * @param  string $key
91
     * @return int
92
     * @throws StorageException
93
     */
94
    public function getFailureCountByCustomKey($key)
95
    {
96
        return $this->getCount($this->prefix($key) . self::KEY_SUFFIX_FAILURE);
97
    }
98
99
    /**
100
     * returns rejection count
101
     *
102
     * @param  string $key
103
     * @return int
104
     * @throws StorageException
105
     */
106
    public function getRejectionCountByCustomKey($key)
107
    {
108
        return $this->getCount($this->prefix($key) . self::KEY_SUFFIX_REJECTION);
109
    }
110
111
    /**
112
     * returns failure count
113
     *
114
     * @param  string $service
115
     * @return int
116
     * @throws StorageException
117
     */
118
    public function getFailureCount($service)
119
    {
120
        return $this->getCount($this->failureKey($service));
121
    }
122
123
    /**
124
     * returns success count
125
     *
126
     * @param  string $service
127
     * @return int
128
     * @throws StorageException
129
     */
130
    public function getSuccessCount($service)
131
    {
132
        return $this->getCount($this->successKey($service));
133
    }
134
135
    /**
136
     * increments failure count
137
     *
138
     * @param  string $service
139
     * @return void
140
     * @throws StorageException
141
     */
142
    public function incrementFailureCount($service)
143
    {
144
        $this->adapter->increment($this->failureKey($service));
145
    }
146
147
    /**
148
     * decrements failure count
149
     *
150
     * @param  string $service
151
     * @return void
152
     * @throws StorageException
153
     */
154
    public function decrementFailureCount($service)
155
    {
156
        $this->adapter->decrement($this->failureKey($service));
157
    }
158
159
    /**
160
     * increments success count
161
     *
162
     * @param  string $service
163
     * @return void
164
     * @throws StorageException
165
     */
166
    public function incrementSuccessCount($service)
167
    {
168
        $this->adapter->increment($this->successKey($service));
169
    }
170
171
    /**
172
     * returns rejection count
173
     *
174
     * @param  string $service
175
     * @return int
176
     * @throws StorageException
177
     */
178
    public function getRejectionCount($service)
179
    {
180
        return $this->getCount($this->rejectionKey($service));
181
    }
182
183
    /**
184
     * increments rejection count
185
     *
186
     * @param  string $service
187
     * @return void
188
     * @throws StorageException
189
     */
190
    public function incrementRejectionCount($service)
191
    {
192
        $this->adapter->increment($this->rejectionKey($service));
193
    }
194
195
    /**
196
     * sets failure count
197
     *
198
     * @param $service
199
     * @param $failureCount
200
     * @throws StorageException
201
     */
202
    public function setFailureCount($service, $failureCount)
203
    {
204
        $this->adapter->save($this->failureKey($service), $failureCount);
205
    }
206
207
    /**
208
     * sets last failure time
209
     *
210
     * @param  string $service
211
     * @param  int    $lastFailureTime
212
     * @return void
213
     * @throws StorageException
214
     */
215
    public function setLastFailureTime($service, $lastFailureTime)
216
    {
217
        $this->adapter->saveLastFailureTime($this->lastFailureKey($service), $lastFailureTime);
218
    }
219
220
    /**
221
     * returns last failure time
222
     *
223
     * @param  string $service
224
     * @return int | null
225
     * @throws StorageException
226
     */
227
    public function getLastFailureTime($service)
228
    {
229
        return $this->adapter->loadLastFailureTime($this->lastFailureKey($service));
230
    }
231
232
    /**
233
     * sets status
234
     *
235
     * @param  string $service
236
     * @param  int    $status
237
     * @return void
238
     * @throws StorageException
239
     */
240
    public function setStatus($service, $status)
241
    {
242
        $this->adapter->saveStatus($this->statusKey($service), $status);
243
    }
244
245
    /**
246
     * returns status
247
     *
248
     * @param  string $service
249
     * @return int
250
     * @throws StorageException
251
     */
252
    public function getStatus($service)
253
    {
254
        return $this->adapter->loadStatus($this->statusKey($service));
255
    }
256
257
    /**
258
     * @return void
259
     */
260
    public function reset()
261
    {
262
        $this->adapter->reset();
263
    }
264
265
    /**
266
     * @return bool
267
     */
268
    public function supportTumblingTimeWindow()
269
    {
270
        return $this->adapter instanceof TumblingTimeWindowInterface;
271
    }
272
273
    /**
274
     * @return bool
275
     */
276
    public function supportSlidingTimeWindow()
277
    {
278
        return $this->adapter instanceof SlidingTimeWindowInterface;
279
    }
280
281
    /**
282
     * @param  string $service
283
     * @return string
284
     */
285
    private function key($service)
286
    {
287
        if ($this->serviceNameDecorator) {
288
            $service = call_user_func($this->serviceNameDecorator, $service);
289
        }
290
291
        return $this->prefix($service);
292
    }
293
294
    /**
295
     * @param  string $key
296
     * @return string
297
     */
298
    private function prefix($key)
299
    {
300
        return self::KEY_PREFIX . $key;
301
    }
302
303
    /**
304
     * @param  string $service
305
     * @return string
306
     */
307
    private function successKey($service)
308
    {
309
        return $this->key($service) . self::KEY_SUFFIX_SUCCESS;
310
    }
311
312
    /**
313
     * @param  string $service
314
     * @return string
315
     */
316
    private function failureKey($service)
317
    {
318
        return $this->key($service) . self::KEY_SUFFIX_FAILURE;
319
    }
320
321
    /**
322
     * @param  string $service
323
     * @return string
324
     */
325
    private function rejectionKey($service)
326
    {
327
        return $this->key($service) . self::KEY_SUFFIX_REJECTION;
328
    }
329
330
    /**
331
     * @param  string $service
332
     * @return string
333
     */
334
    private function lastFailureKey($service)
335
    {
336
        return $this->supportSlidingTimeWindow()
337
            // If the adapter supports SlidingTimeWindow use failureKey() instead,
338
            // because Redis doesn't save lastFailureTime.
339
            // @see Ackintosh\Ganesha\Storage\Adapter\Redis#saveLastFailureTime()
340
            ? $this->failureKey($service)
341
            : $this->prefix($service) . self::KEY_SUFFIX_LAST_FAILURE_TIME;
342
    }
343
344
    /**
345
     * @param  string $service
346
     * @return string
347
     */
348
    private function statusKey($service)
349
    {
350
        return $this->prefix($service) . self::KEY_SUFFIX_STATUS;
351
    }
352
}
353