Skip to content

Commit aeb9733

Browse files
comment(*): Update PHP DOC, comment, and add import
1 parent 9cd07be commit aeb9733

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

src/ApiCache.php

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55
namespace CrowdSecBouncer;
66

77
use DateTime;
8+
use Exception;
89
use IPLib\Address\AddressInterface;
910
use IPLib\Address\Type;
1011
use IPLib\Factory;
1112
use IPLib\Range\Subnet;
13+
use Psr\Cache\InvalidArgumentException;
1214
use Psr\Log\LoggerInterface;
1315
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1416
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1517
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
1618
use Symfony\Component\Cache\PruneableInterface;
1719

1820
/**
19-
* The cache mecanism to store every decisions from LAPI/CAPI. Symfony Cache component powered.
21+
* The cache mechanism to store every decision from LAPI/CAPI. Symfony Cache component powered.
2022
*
2123
* @author CrowdSec team
2224
*
@@ -47,11 +49,15 @@ class ApiCache
4749

4850
/** @var bool */
4951
private $warmedUp = null;
52+
/**
53+
* @var string
54+
*/
55+
private $fallbackRemediation;
5056

5157
/**
52-
* @param LoggerInterface $logger The logger to use
53-
* @param ApiClient $apiClient The APIClient instance to use
54-
* @param AbstractAdapter $adapter The AbstractAdapter adapter to use
58+
* @param LoggerInterface $logger The logger to use
59+
* @param ApiClient|null $apiClient The APIClient instance to use
60+
* @param AbstractAdapter|null $adapter The AbstractAdapter adapter to use
5561
*/
5662
public function __construct(LoggerInterface $logger, ApiClient $apiClient = null, AbstractAdapter $adapter = null)
5763
{
@@ -63,14 +69,16 @@ public function __construct(LoggerInterface $logger, ApiClient $apiClient = null
6369
/**
6470
* Configure this instance.
6571
*
66-
* @param bool $liveMode If we use the live mode (else we use the stream mode)
67-
* @param string $apiUrl The URL of the LAPI
68-
* @param int $timeout The timeout well calling LAPI
69-
* @param string $userAgent The user agent to use when calling LAPI
70-
* @param string $apiKey The Bouncer API Key to use to connect LAPI
71-
* @param int $cacheExpirationForCleanIp The duration to cache an IP considered as clean by LAPI
72-
* @param int $cacheExpirationForBadIp The duration to cache an IP considered as bad by LAPI
73-
* @param string $fallbackRemediation The remediation to use when the remediation sent by LAPI is not supported by this library
72+
* @param bool $liveMode If we use the live mode (else we use the stream mode)
73+
* @param string $apiUrl The URL of the LAPI
74+
* @param int $timeout The timeout well calling LAPI
75+
* @param string $userAgent The user agent to use when calling LAPI
76+
* @param string $apiKey The Bouncer API Key to use to connect LAPI
77+
* @param int $cacheExpirationForCleanIp The duration to cache an IP considered as clean by LAPI
78+
* @param int $cacheExpirationForBadIp The duration to cache an IP considered as bad by LAPI
79+
* @param string $fallbackRemediation The remediation to use when the remediation sent by LAPI is not supported by
80+
* this library
81+
* @throws InvalidArgumentException
7482
*/
7583
public function configure(
7684
bool $liveMode,
@@ -103,6 +111,8 @@ public function configure(
103111

104112
/**
105113
* Add remediation to a Symfony Cache Item identified by IP.
114+
* @throws InvalidArgumentException
115+
* @throws Exception
106116
*/
107117
private function addRemediationToCacheItem(string $ip, string $type, int $expiration, int $decisionId): string
108118
{
@@ -145,6 +155,8 @@ private function addRemediationToCacheItem(string $ip, string $type, int $expira
145155

146156
/**
147157
* Remove a decision from a Symfony Cache Item identified by ip.
158+
* @throws InvalidArgumentException
159+
* @throws Exception
148160
*/
149161
private function removeDecisionFromRemediationItem(string $ip, int $decisionId): bool
150162
{
@@ -177,7 +189,7 @@ private function removeDecisionFromRemediationItem(string $ip, int $decisionId):
177189
$item->expiresAt(new DateTime('@'.$maxLifetime));
178190
$item->set($cacheContent);
179191

180-
// Save the cache without commiting it to the cache system.
192+
// Save the cache without committing it to the cache system.
181193
// Useful to improve performance when updating the cache.
182194
if (!$this->adapter->saveDeferred($item)) {
183195
throw new BouncerException("cache#$ip: Unable to save item");
@@ -230,7 +242,7 @@ private function formatRemediationFromDecision(?array $decision): array
230242
if (!$decision) {
231243
$duration = time() + $this->cacheExpirationForCleanIp;
232244
if (!$this->liveMode) {
233-
// In stream mode we considere an clean IP forever... until the next resync.
245+
// In stream mode we consider a clean IP forever... until the next resync.
234246
$duration = 315360000; // in this case, forever is 10 years as PHP_INT_MAX will cause trouble with the Memcached Adapter (int to float unwanted conversion)
235247
}
236248

@@ -251,6 +263,9 @@ private function formatRemediationFromDecision(?array $decision): array
251263
];
252264
}
253265

266+
/**
267+
* @throws InvalidArgumentException
268+
*/
254269
private function defferUpdateCacheConfig(array $config): void
255270
{
256271
$cacheConfigItem = $this->adapter->getItem('cacheConfig');
@@ -262,6 +277,7 @@ private function defferUpdateCacheConfig(array $config): void
262277

263278
/**
264279
* Update the cached remediation of the specified IP from these new decisions.
280+
* @throws InvalidArgumentException
265281
*/
266282
private function saveRemediationsForIp(array $decisions, string $ip): string
267283
{
@@ -292,6 +308,7 @@ private function saveRemediationsForIp(array $decisions, string $ip): string
292308

293309
/**
294310
* Update the cached remediations from these new decisions.
311+
* @throws InvalidArgumentException
295312
*/
296313
private function saveRemediations(array $decisions): array
297314
{
@@ -347,6 +364,9 @@ private function saveRemediations(array $decisions): array
347364
return ['success' => $this->commit(), 'errors' => $errors];
348365
}
349366

367+
/**
368+
* @throws InvalidArgumentException
369+
*/
350370
private function removeRemediations(array $decisions): array
351371
{
352372
$errors = [];
@@ -420,6 +440,9 @@ private function removeRemediations(array $decisions): array
420440
return ['count' => $count, 'errors' => $errors];
421441
}
422442

443+
/**
444+
* @throws InvalidArgumentException
445+
*/
423446
public function clear(): bool
424447
{
425448
$this->setCustomErrorHandler();
@@ -442,6 +465,7 @@ public function clear(): bool
442465
* Used when the stream mode has just been activated.
443466
*
444467
* @return array "count": number of decisions added, "errors": decisions not added
468+
* @throws InvalidArgumentException
445469
*/
446470
public function warmUp(): array
447471
{
@@ -451,8 +475,7 @@ public function warmUp(): array
451475
$this->clear();
452476
}
453477
$this->logger->debug('', ['type' => 'START_CACHE_WARMUP']);
454-
$startup = true;
455-
$decisionsDiff = $this->apiClient->getStreamedDecisions($startup);
478+
$decisionsDiff = $this->apiClient->getStreamedDecisions(true);
456479
$newDecisions = $decisionsDiff['new'];
457480

458481
$nbNew = 0;
@@ -483,6 +506,7 @@ public function warmUp(): array
483506
* Used for the stream mode when we have to update the remediations list.
484507
*
485508
* @return array number of deleted and new decisions, and errors when processing decisions
509+
* @throws InvalidArgumentException
486510
*/
487511
public function pullUpdates(): array
488512
{
@@ -522,8 +546,9 @@ public function pullUpdates(): array
522546
/**
523547
* This method is called when nothing has been found in cache for the requested IP.
524548
* In live mode is enabled, calls the API for decisions concerning the specified IP
525-
* In stream mode, as we considere cache is the single source of truth, the IP is considered clean.
526-
* Finally the result is stored in caches for further calls.
549+
* In stream mode, as we consider cache is the single source of truth, the IP is considered clean.
550+
* Finally, the result is stored in caches for further calls.
551+
* @throws InvalidArgumentException
527552
*/
528553
private function miss(string $ipToQuery, string $cacheKey): string
529554
{
@@ -538,9 +563,10 @@ private function miss(string $ipToQuery, string $cacheKey): string
538563
}
539564

540565
/**
541-
* Used in both mode (stream and ruptue).
566+
* Used in both mode (stream and rupture).
542567
* This method formats the cached item as a remediation.
543568
* It returns the highest remediation level found.
569+
* @throws InvalidArgumentException
544570
*/
545571
private function hit(string $ip): string
546572
{
@@ -557,6 +583,7 @@ private function hit(string $ip): string
557583
* Request the cache for the specified IP.
558584
*
559585
* @return string the computed remediation string, or null if no decision was found
586+
* @throws InvalidArgumentException
560587
*/
561588
public function get(AddressInterface $address): string
562589
{
@@ -604,8 +631,8 @@ public function prune(): bool
604631
}
605632

606633
/**
607-
* When Memcached connection fail, it throw an unhandled warning.
608-
* To catch this warning as a clean execption we have to temporarily change the error handler.
634+
* When Memcached connection fail, it throws an unhandled warning.
635+
* To catch this warning as a clean exception we have to temporarily change the error handler.
609636
*/
610637
private function setCustomErrorHandler(): void
611638
{
@@ -646,7 +673,7 @@ private function commit(): bool
646673
/**
647674
* Test the connection to the cache system (Redis or Memcached).
648675
*
649-
* @throws BouncerException if the connection was not successful
676+
* @throws BouncerException|InvalidArgumentException if the connection was not successful
650677
* */
651678
public function testConnection(): void
652679
{

src/Bouncer.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
class Bouncer
2828
{
2929
/** @var LoggerInterface */
30-
private $logger = null;
30+
private $logger;
3131

3232
/** @var array */
3333
private $config = [];
3434

3535
/** @var ApiCache */
36-
private $apiCache = null;
36+
private $apiCache;
3737

3838
/** @var int */
3939
private $maxRemediationLevelIndex = null;
@@ -172,7 +172,7 @@ public function warmBlocklistCacheUp(): array
172172

173173
/**
174174
* Used in stream mode only.
175-
* This method should be called periodically (ex: crontab) in a asynchronous way to update the bouncer cache.
175+
* This method should be called periodically (ex: crontab) in an asynchronous way to update the bouncer cache.
176176
*
177177
* @return array Number of deleted and new decisions, and errors when processing decisions
178178
*/
@@ -228,11 +228,11 @@ public static function buildCaptchaCouple()
228228

229229
/**
230230
* Check if the captcha filled by the user is correct or not.
231-
* We are premissive with the user (0 is interpreted as "o" and 1 in interpretted as "l").
231+
* We are permissive with the user (0 is interpreted as "o" and 1 in interpreted as "l").
232232
*
233233
* @param string $expected The expected phrase
234-
* @param string $expected The phrase to check (the user input)
235-
* @param string $ip Th IP of the use (for logging purpose)
234+
* @param string $try The phrase to check (the user input)
235+
* @param string $ip The IP of the use (for logging purpose)
236236
*
237237
* @return bool If the captcha input was correct or not
238238
*/
@@ -251,12 +251,12 @@ public function checkCaptcha(string $expected, string $try, string $ip)
251251
/**
252252
* Test the connection to the cache system (Redis or Memcached).
253253
*
254-
* @return bool If the connection was successful or not
254+
* @return void If the connection was successful or not
255255
*
256256
* @throws BouncerException if the connection was not successful
257257
* */
258258
public function testConnection()
259259
{
260-
return $this->apiCache->testConnection();
260+
$this->apiCache->testConnection();
261261
}
262262
}

src/TemplateConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TemplateConfiguration implements ConfigurationInterface
2121
/**
2222
* {@inheritdoc}
2323
*/
24-
public function getConfigTreeBuilder()
24+
public function getConfigTreeBuilder(): TreeBuilder
2525
{
2626
$treeBuilder = new TreeBuilder('config');
2727
/** @var $rootNode ArrayNodeDefinition */

0 commit comments

Comments
 (0)