Skip to content

Commit 57e3012

Browse files
style(*): Pass through Phpcs fixer
1 parent b9e98b1 commit 57e3012

File tree

5 files changed

+62
-85
lines changed

5 files changed

+62
-85
lines changed

src/ApiCache.php

Lines changed: 43 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,36 @@
2929
*/
3030
class ApiCache
3131
{
32-
/** @var LoggerInterface */
3332
private LoggerInterface $logger;
3433

35-
/** @var ApiClient */
3634
private ApiClient $apiClient;
3735

3836
/** @var AbstractAdapter */
3937
private $adapter;
4038

41-
/** @var Geolocation */
4239
private Geolocation $geolocation;
4340

4441
/** @var bool */
4542
private ?bool $liveMode = null;
4643

47-
/** @var int */
4844
private int $cacheExpirationForCleanIp = 0;
4945

50-
/** @var int */
5146
private int $cacheExpirationForBadIp = 0;
5247

5348
/** @var bool */
5449
private ?bool $warmedUp = null;
55-
/**
56-
* @var string
57-
*/
50+
5851
private string $fallbackRemediation;
59-
/**
60-
* @var array
61-
*/
52+
6253
private array $geolocConfig;
63-
/**
64-
* @var array
65-
*/
54+
6655
private array $cacheKey = [];
6756

6857
/**
69-
* @param LoggerInterface $logger The logger to use
70-
* @param ApiClient|null $apiClient The APIClient instance to use
71-
* @param AbstractAdapter|null $adapter The AbstractAdapter adapter to use
72-
* @param Geolocation|null $geolocation The Geolocation helper 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
61+
* @param Geolocation|null $geolocation The Geolocation helper to use
7362
*/
7463
public function __construct(LoggerInterface $logger, ApiClient $apiClient = null, AbstractAdapter $adapter =
7564
null, Geolocation $geolocation = null)
@@ -83,15 +72,16 @@ public function __construct(LoggerInterface $logger, ApiClient $apiClient = null
8372
/**
8473
* Configure this instance.
8574
*
86-
* @param bool $liveMode If we use the live mode (else we use the stream mode)
87-
* @param string $apiUrl The URL of the LAPI
88-
* @param int $timeout The timeout well calling LAPI
89-
* @param string $userAgent The user agent to use when calling LAPI
90-
* @param string $apiKey The Bouncer API Key to use to connect LAPI
91-
* @param int $cacheExpirationForCleanIp The duration to cache an IP considered as clean by LAPI
92-
* @param int $cacheExpirationForBadIp The duration to cache an IP considered as bad by LAPI
93-
* @param string $fallbackRemediation The remediation to use when the remediation sent by LAPI is not supported by
94-
* this library
75+
* @param bool $liveMode If we use the live mode (else we use the stream mode)
76+
* @param string $apiUrl The URL of the LAPI
77+
* @param int $timeout The timeout well calling LAPI
78+
* @param string $userAgent The user agent to use when calling LAPI
79+
* @param string $apiKey The Bouncer API Key to use to connect LAPI
80+
* @param int $cacheExpirationForCleanIp The duration to cache an IP considered as clean by LAPI
81+
* @param int $cacheExpirationForBadIp The duration to cache an IP considered as bad by LAPI
82+
* @param string $fallbackRemediation The remediation to use when the remediation sent by LAPI is not supported by
83+
* this library
84+
*
9585
* @throws InvalidArgumentException
9686
*/
9787
public function configure(
@@ -121,23 +111,18 @@ public function configure(
121111
'exp_clean_ips' => $cacheExpirationForCleanIp,
122112
'exp_bad_ips' => $cacheExpirationForBadIp,
123113
'warmed_up' => ($this->warmedUp ? 'true' : 'false'),
124-
'geolocation' => $this->geolocConfig
114+
'geolocation' => $this->geolocConfig,
125115
]);
126116
$this->apiClient->configure($apiUrl, $timeout, $userAgent, $apiKey);
127117
}
128118

129119
/**
130120
* Add remediation to a Symfony Cache Item identified by a cache key.
131-
* @param string $cacheKey
132-
* @param string $type
133-
* @param int $expiration
134-
* @param int $decisionId
135-
* @return string
121+
*
136122
* @throws InvalidArgumentException
137123
* @throws Exception
138124
*/
139-
private function addRemediationToCacheItem(string $cacheKey, string $type, int $expiration, int $decisionId):
140-
string
125+
private function addRemediationToCacheItem(string $cacheKey, string $type, int $expiration, int $decisionId): string
141126
{
142127
$item = $this->adapter->getItem(base64_encode($cacheKey));
143128

@@ -178,6 +163,7 @@ private function addRemediationToCacheItem(string $cacheKey, string $type, int $
178163

179164
/**
180165
* Remove a decision from a Symfony Cache Item identified by a cache key.
166+
*
181167
* @throws InvalidArgumentException
182168
* @throws Exception
183169
*/
@@ -300,9 +286,7 @@ private function defferUpdateCacheConfig(array $config): void
300286

301287
/**
302288
* Update the cached remediation of the specified cacheKey from these new decisions.
303-
* @param array $decisions
304-
* @param string $cacheKey
305-
* @return string
289+
*
306290
* @throws InvalidArgumentException
307291
*/
308292
private function saveRemediationsForCacheKey(array $decisions, string $cacheKey): string
@@ -333,25 +317,23 @@ private function saveRemediationsForCacheKey(array $decisions, string $cacheKey)
333317
}
334318

335319
/**
336-
* Cache key convention
337-
* @param string $scope
338-
* @param string $value
339-
* @return string
320+
* Cache key convention.
321+
*
340322
* @throws Exception
341323
*/
342324
private function getCacheKey(string $scope, string $value): string
343325
{
344-
if(!isset($this->cacheKey[$scope][$value])){
326+
if (!isset($this->cacheKey[$scope][$value])) {
345327
switch ($scope) {
346328
case Constants::SCOPE_RANGE:
347329
case Constants::SCOPE_IP:
348-
$this->cacheKey[$scope][$value] = Constants::SCOPE_IP . ':' . $value;
330+
$this->cacheKey[$scope][$value] = Constants::SCOPE_IP.':'.$value;
349331
break;
350332
case Constants::SCOPE_COUNTRY:
351-
$this->cacheKey[$scope][$value] = Constants::SCOPE_COUNTRY . ':' . $value;
333+
$this->cacheKey[$scope][$value] = Constants::SCOPE_COUNTRY.':'.$value;
352334
break;
353335
default:
354-
throw new Exception('Unknown scope:' . $scope);
336+
throw new Exception('Unknown scope:'.$scope);
355337
}
356338
}
357339

@@ -360,6 +342,7 @@ private function getCacheKey(string $scope, string $value): string
360342

361343
/**
362344
* Update the cached remediations from these new decisions.
345+
*
363346
* @throws InvalidArgumentException
364347
* @throws Exception
365348
*/
@@ -505,7 +488,6 @@ private function removeRemediations(array $decisions): array
505488
'value' => $decision['value'],
506489
]);
507490
}
508-
509491
}
510492
}
511493

@@ -539,6 +521,7 @@ public function clear(): bool
539521
* Used when the stream mode has just been activated.
540522
*
541523
* @return array "count": number of decisions added, "errors": decisions not added
524+
*
542525
* @throws InvalidArgumentException
543526
*/
544527
public function warmUp(): array
@@ -580,6 +563,7 @@ public function warmUp(): array
580563
* Used for the stream mode when we have to update the remediations list.
581564
*
582565
* @return array number of deleted and new decisions, and errors when processing decisions
566+
*
583567
* @throws InvalidArgumentException
584568
*/
585569
public function pullUpdates(): array
@@ -622,9 +606,7 @@ public function pullUpdates(): array
622606
* In live mode is enabled, calls the API for decisions concerning the specified IP
623607
* In stream mode, as we consider cache is the single source of truth, the value is considered clean.
624608
* Finally, the result is stored in caches for further calls.
625-
* @param string $value
626-
* @param string $cacheScope
627-
* @return string
609+
*
628610
* @throws InvalidArgumentException
629611
* @throws Exception
630612
*/
@@ -633,14 +615,14 @@ private function miss(string $value, string $cacheScope): string
633615
$decisions = [];
634616
$cacheKey = $this->getCacheKey($cacheScope, $value);
635617
if ($this->liveMode) {
636-
if($cacheScope === Constants::SCOPE_IP){
618+
if (Constants::SCOPE_IP === $cacheScope) {
637619
$this->logger->debug('', ['type' => 'DIRECT_API_CALL', 'ip' => $value]);
638620
$decisions = $this->apiClient->getFilteredDecisions(['ip' => $value]);
639-
} elseif($cacheScope === Constants::SCOPE_COUNTRY){
621+
} elseif (Constants::SCOPE_COUNTRY === $cacheScope) {
640622
$this->logger->debug('', ['type' => 'DIRECT_API_CALL', 'country' => $value]);
641623
$decisions = $this->apiClient->getFilteredDecisions([
642624
'scope' => Constants::SCOPE_COUNTRY,
643-
'value' => $value
625+
'value' => $value,
644626
]);
645627
}
646628
}
@@ -652,6 +634,7 @@ private function miss(string $value, string $cacheScope): string
652634
* Used in both mode (stream and rupture).
653635
* This method formats the cached item as a remediation.
654636
* It returns the highest remediation level found.
637+
*
655638
* @throws InvalidArgumentException
656639
*/
657640
private function hit(string $ip): string
@@ -666,13 +649,12 @@ private function hit(string $ip): string
666649
}
667650

668651
/**
669-
* @param string $cacheScope
670652
* @param $value
671-
* @return string
653+
*
672654
* @throws InvalidArgumentException
673655
* @throws Exception
674656
*/
675-
private function handleCacheRemediation (string $cacheScope, $value): string
657+
private function handleCacheRemediation(string $cacheScope, $value): string
676658
{
677659
$cacheKey = $this->getCacheKey($cacheScope, $value);
678660
if ($this->adapter->hasItem(base64_encode($cacheKey))) {
@@ -684,8 +666,7 @@ private function handleCacheRemediation (string $cacheScope, $value): string
684666
}
685667

686668
if (Constants::REMEDIATION_BYPASS === $remediation) {
687-
$this->logger->info('', ['type' => 'CLEAN_VALUE', 'scope' => $cacheScope, 'value' => $value, 'cache' =>
688-
$cache]);
669+
$this->logger->info('', ['type' => 'CLEAN_VALUE', 'scope' => $cacheScope, 'value' => $value, 'cache' => $cache]);
689670
} else {
690671
$this->logger->warning('', [
691672
'type' => 'BAD_VALUE',
@@ -695,14 +676,15 @@ private function handleCacheRemediation (string $cacheScope, $value): string
695676
'cache' => $cache,
696677
]);
697678
}
698-
return $remediation;
699679

680+
return $remediation;
700681
}
701682

702683
/**
703684
* Request the cache for the specified IP.
704685
*
705686
* @return string the computed remediation string, or null if no decision was found
687+
*
706688
* @throws InvalidArgumentException
707689
* @throws Exception
708690
*/
@@ -716,7 +698,7 @@ public function get(AddressInterface $address): string
716698
}
717699

718700
// Handle Ip and Range remediation
719-
$remediations = [[$this->handleCacheRemediation(Constants::SCOPE_IP, $ip), "", "",]];
701+
$remediations = [[$this->handleCacheRemediation(Constants::SCOPE_IP, $ip), '', '']];
720702

721703
// Handle Geolocation remediation
722704
if (!empty($this->geolocConfig['enabled'])) {
@@ -739,7 +721,7 @@ public function get(AddressInterface $address): string
739721
]);
740722
}
741723
if ($countryToQuery) {
742-
$remediations[] = [$this->handleCacheRemediation(Constants::SCOPE_COUNTRY, $countryToQuery), "", "",];
724+
$remediations[] = [$this->handleCacheRemediation(Constants::SCOPE_COUNTRY, $countryToQuery), '', ''];
743725
}
744726
}
745727
$prioritizedRemediations = Remediation::sortRemediationByPriority($remediations);

src/Bouncer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public static function buildCaptchaCouple()
232232
* We are permissive with the user (0 is interpreted as "o" and 1 in interpreted as "l").
233233
*
234234
* @param string $expected The expected phrase
235-
* @param string $try The phrase to check (the user input)
235+
* @param string $try The phrase to check (the user input)
236236
* @param string $ip The IP of the use (for logging purpose)
237237
*
238238
* @return bool If the captcha input was correct or not

src/Geolocation.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace CrowdSecBouncer;
46

7+
use BadMethodCallException;
58
use Exception;
69
use GeoIp2\Database\Reader;
710
use GeoIp2\Exception\AddressNotFoundException;
8-
use MaxMind\Db\Reader\InvalidDatabaseException;
911
use InvalidArgumentException;
10-
use BadMethodCallException;
12+
use MaxMind\Db\Reader\InvalidDatabaseException;
1113

1214
/**
13-
* The Library geolocation helper. You'll find here methods used for geolocation purposes
15+
* The Library geolocation helper. You'll find here methods used for geolocation purposes.
1416
*
1517
* @author CrowdSec team
1618
*
@@ -21,17 +23,13 @@
2123
*/
2224
class Geolocation
2325
{
24-
2526
protected array $maxmindCountry = [];
2627

27-
private array $resultTemplate = ['country' => "", 'not_found' => "", 'error' => ""];
28+
private array $resultTemplate = ['country' => '', 'not_found' => '', 'error' => ''];
2829

2930
/**
30-
* Retrieve a country from a MaxMind database
31-
* @param string $ip
32-
* @param string $databaseType
33-
* @param string $databasePath
34-
* @return array
31+
* Retrieve a country from a MaxMind database.
32+
*
3533
* @throws Exception
3634
*/
3735
private function getMaxMindCountry(string $ip, string $databaseType, string $databasePath): array
@@ -64,10 +62,8 @@ private function getMaxMindCountry(string $ip, string $databaseType, string $dat
6462
}
6563

6664
/**
67-
* Retrieve country from a geo-localised IP
68-
* @param array $geolocConfig
69-
* @param string $ip
70-
* @return array
65+
* Retrieve country from a geo-localised IP.
66+
*
7167
* @throws Exception
7268
*/
7369
public function getCountryResult(array $geolocConfig, string $ip): array
@@ -91,13 +87,13 @@ public function getCountryResult(array $geolocConfig, string $ip): array
9187
$this->getMaxMindCountry($currentIp, $configPath['database_type'], $configPath['database_path']);
9288
break;
9389
default:
94-
throw new Exception('Unknown Geolocation type:' . $geolocConfig['type']);
90+
throw new Exception('Unknown Geolocation type:'.$geolocConfig['type']);
9591
}
9692

97-
if($saveInSession){
93+
if ($saveInSession) {
9894
if (!empty($result['country'])) {
9995
Session::setSessionVariable('crowdsec_geolocation_country', $result['country']);
100-
} elseif ( !empty($result['not_found'])) {
96+
} elseif (!empty($result['not_found'])) {
10197
Session::setSessionVariable('crowdsec_geolocation_not_found', $result['not_found']);
10298
}
10399
}

src/Session.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace CrowdSecBouncer;
46

@@ -45,5 +47,4 @@ public static function unsetSessionVariable(string $name): void
4547
unset($_SESSION[$name]);
4648
}
4749
}
48-
4950
}

0 commit comments

Comments
 (0)