|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ackintosh\Ganesha; |
|
3
|
|
|
|
|
4
|
|
|
use Ackintosh\Ganesha\Storage\AdapterInterface; |
|
5
|
|
|
use Ackintosh\Ganesha\Storage\StorageKeys; |
|
6
|
|
|
use Ackintosh\Ganesha\Storage\StorageKeysInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Configuration implements \ArrayAccess |
|
9
|
|
|
{ |
|
10
|
|
|
// Configuration keys |
|
11
|
|
|
const ADAPTER = 'adapter'; |
|
12
|
|
|
const TIME_WINDOW = 'timeWindow'; |
|
13
|
|
|
const FAILURE_RATE_THRESHOLD = 'failureRateThreshold'; |
|
14
|
|
|
const FAILURE_COUNT_THRESHOLD = 'failureCountThreshold'; |
|
15
|
|
|
const MINIMUM_REQUESTS = 'minimumRequests'; |
|
16
|
|
|
const INTERVAL_TO_HALF_OPEN = 'intervalToHalfOpen'; |
|
17
|
|
|
const STORAGE_KEYS = 'storageKeys'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $params; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct($params) |
|
25
|
|
|
{ |
|
26
|
|
|
if (!isset($params[self::STORAGE_KEYS])) { |
|
27
|
|
|
$params[self::STORAGE_KEYS] = new StorageKeys(); |
|
28
|
|
|
} |
|
29
|
|
|
$this->params = $params; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function offsetSet($offset, $value) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->params[$offset] = $value; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function offsetExists($offset) |
|
38
|
|
|
{ |
|
39
|
|
|
return isset($this->params[$offset]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function offsetUnset($offset) |
|
43
|
|
|
{ |
|
44
|
|
|
unset($this->params[$offset]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function offsetGet($offset) |
|
48
|
|
|
{ |
|
49
|
|
|
return isset($this->params[$offset]) ? $this->params[$offset] : null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function adapter(): AdapterInterface |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->params[self::ADAPTER]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function timeWindow(): int |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->params[self::TIME_WINDOW]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function failureRateThreshold(): int |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->params[self::FAILURE_RATE_THRESHOLD]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function failureCountThreshold(): int |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->params[self::FAILURE_COUNT_THRESHOLD]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function minimumRequests(): int |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->params[self::MINIMUM_REQUESTS]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function intervalToHalfOpen(): int |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->params[self::INTERVAL_TO_HALF_OPEN]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function storageKeys(): StorageKeysInterface |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->params[self::STORAGE_KEYS]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws \InvalidArgumentException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function validate(): void |
|
91
|
|
|
{ |
|
92
|
|
|
if (isset($this->params[self::ADAPTER]) && !$this->params[self::ADAPTER] instanceof AdapterInterface) { |
|
93
|
|
|
throw new \InvalidArgumentException(get_class($this->params[self::ADAPTER]) . ' should be an instance of AdapterInterface'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (isset($this->params[self::STORAGE_KEYS]) && !$this->params[self::STORAGE_KEYS] instanceof StorageKeysInterface) { |
|
97
|
|
|
throw new \InvalidArgumentException(get_class($this->params[self::STORAGE_KEYS]) . ' should be an instance of StorageKeysInterface'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
foreach ([ |
|
101
|
|
|
self::TIME_WINDOW, |
|
102
|
|
|
self::FAILURE_RATE_THRESHOLD, |
|
103
|
|
|
self::FAILURE_COUNT_THRESHOLD, |
|
104
|
|
|
self::MINIMUM_REQUESTS, |
|
105
|
|
|
self::INTERVAL_TO_HALF_OPEN |
|
106
|
|
|
] as $name) { |
|
107
|
|
|
if (isset($this->params[$name])) { |
|
108
|
|
|
$v = $this->params[$name]; |
|
109
|
|
|
if (!is_int($v) || $v < 1) { |
|
110
|
|
|
throw new \InvalidArgumentException($name . ' should be an positive integer'); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (isset($this->params[self::FAILURE_RATE_THRESHOLD]) && $this->params[self::FAILURE_RATE_THRESHOLD] > 100) { |
|
116
|
|
|
throw new \InvalidArgumentException(self::FAILURE_RATE_THRESHOLD . ' should be equal or less than 100'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|