@@ -37,11 +37,15 @@ class Bouncer
37
37
/** @var int */
38
38
private $ maxRemediationLevelIndex = 0 ;
39
39
40
+ /** @var array */
41
+ private $ configs = [];
42
+
40
43
public function __construct (
41
44
TagAwareAdapterInterface $ cacheAdapter = null ,
42
- LoggerInterface $ logger = null ,
43
- ApiCache $ apiCache = null
44
- ) {
45
+ LoggerInterface $ logger = null ,
46
+ ApiCache $ apiCache = null
47
+ )
48
+ {
45
49
if (!$ logger ) {
46
50
$ logger = new Logger ('null ' );
47
51
$ logger ->pushHandler (new NullHandler ());
@@ -50,6 +54,16 @@ public function __construct(
50
54
$ this ->apiCache = $ apiCache ?: new ApiCache ($ logger , new ApiClient ($ logger ), $ cacheAdapter );
51
55
}
52
56
57
+ /**
58
+ * Retrieve Bouncer configurations
59
+ *
60
+ * @return array
61
+ */
62
+ public function getConfigs (): array
63
+ {
64
+ return $ this ->configs ;
65
+ }
66
+
53
67
/**
54
68
* Configure this instance.
55
69
*
@@ -59,33 +73,33 @@ public function __construct(
59
73
*/
60
74
public function configure (array $ config ): void
61
75
{
62
- // Process input configuration.
76
+ // Process and validate input configuration.
63
77
$ configuration = new Configuration ();
64
78
$ processor = new Processor ();
65
- $ finalConfig = $ processor ->processConfiguration ($ configuration , [$ config ]);
79
+ $ this -> configs = $ processor ->processConfiguration ($ configuration , [$ config ]);
66
80
/** @var int */
67
81
$ index = array_search (
68
- $ finalConfig ['max_remediation_level ' ],
82
+ $ this -> configs ['max_remediation_level ' ],
69
83
Constants::ORDERED_REMEDIATIONS
70
84
);
71
85
$ this ->maxRemediationLevelIndex = $ index ;
72
86
$ cacheDurations = [
73
- 'clean_ip_cache_duration ' => $ finalConfig ['clean_ip_cache_duration ' ],
74
- 'bad_ip_cache_duration ' => $ finalConfig ['bad_ip_cache_duration ' ],
75
- 'captcha_cache_duration ' => $ finalConfig ['captcha_cache_duration ' ],
76
- 'geolocation_cache_duration ' => $ finalConfig ['geolocation_cache_duration ' ],
87
+ 'clean_ip_cache_duration ' => $ this -> configs ['clean_ip_cache_duration ' ],
88
+ 'bad_ip_cache_duration ' => $ this -> configs ['bad_ip_cache_duration ' ],
89
+ 'captcha_cache_duration ' => $ this -> configs ['captcha_cache_duration ' ],
90
+ 'geolocation_cache_duration ' => $ this -> configs ['geolocation_cache_duration ' ],
77
91
];
78
92
79
93
// Configure Api Cache.
80
94
$ this ->apiCache ->configure (
81
- $ finalConfig ['stream_mode ' ],
82
- $ finalConfig ['api_url ' ],
83
- $ finalConfig ['api_timeout ' ],
84
- $ finalConfig ['api_user_agent ' ],
85
- $ finalConfig ['api_key ' ],
95
+ $ this -> configs ['stream_mode ' ],
96
+ $ this -> configs ['api_url ' ],
97
+ $ this -> configs ['api_timeout ' ],
98
+ $ this -> configs ['api_user_agent ' ],
99
+ $ this -> configs ['api_key ' ],
86
100
$ cacheDurations ,
87
- $ finalConfig ['fallback_remediation ' ],
88
- $ finalConfig ['geolocation ' ]
101
+ $ this -> configs ['fallback_remediation ' ],
102
+ $ this -> configs ['geolocation ' ]
89
103
);
90
104
}
91
105
@@ -115,7 +129,7 @@ private function capRemediationLevel(string $remediation): string
115
129
*
116
130
* @return string the remediation to apply (ex: 'ban', 'captcha', 'bypass')
117
131
*
118
- * @throws InvalidArgumentException
132
+ * @throws InvalidArgumentException|\Psr\Cache\CacheException
119
133
*/
120
134
public function getRemediationForIp (string $ ip ): string
121
135
{
@@ -154,11 +168,12 @@ public static function getAccessForbiddenHtmlTemplate(array $config): string
154
168
* The input $config should match the TemplateConfiguration input format.
155
169
*/
156
170
public static function getCaptchaHtmlTemplate (
157
- bool $ error ,
171
+ bool $ error ,
158
172
string $ captchaImageSrc ,
159
173
string $ captchaResolutionFormUrl ,
160
- array $ config
161
- ): string {
174
+ array $ config
175
+ ): string
176
+ {
162
177
// Process template configuration.
163
178
$ configuration = new TemplateConfiguration ();
164
179
$ processor = new Processor ();
@@ -176,7 +191,7 @@ public static function getCaptchaHtmlTemplate(
176
191
*
177
192
* @return array "count": number of decisions added, "errors": decisions not added
178
193
*
179
- * @throws InvalidArgumentException
194
+ * @throws InvalidArgumentException|\Psr\Cache\CacheException
180
195
*/
181
196
public function warmBlocklistCacheUp (): array
182
197
{
@@ -189,7 +204,7 @@ public function warmBlocklistCacheUp(): array
189
204
*
190
205
* @return array Number of deleted and new decisions, and errors when processing decisions
191
206
*
192
- * @throws InvalidArgumentException
207
+ * @throws InvalidArgumentException|\Psr\Cache\CacheException
193
208
*/
194
209
public function refreshBlocklistCache (): array
195
210
{
0 commit comments