29
29
*/
30
30
class ApiCache
31
31
{
32
- /** @var LoggerInterface */
33
32
private LoggerInterface $ logger ;
34
33
35
- /** @var ApiClient */
36
34
private ApiClient $ apiClient ;
37
35
38
36
/** @var AbstractAdapter */
39
37
private $ adapter ;
40
38
41
- /** @var Geolocation */
42
39
private Geolocation $ geolocation ;
43
40
44
41
/** @var bool */
45
42
private ?bool $ liveMode = null ;
46
43
47
- /** @var int */
48
44
private int $ cacheExpirationForCleanIp = 0 ;
49
45
50
- /** @var int */
51
46
private int $ cacheExpirationForBadIp = 0 ;
52
47
53
48
/** @var bool */
54
49
private ?bool $ warmedUp = null ;
55
- /**
56
- * @var string
57
- */
50
+
58
51
private string $ fallbackRemediation ;
59
- /**
60
- * @var array
61
- */
52
+
62
53
private array $ geolocConfig ;
63
- /**
64
- * @var array
65
- */
54
+
66
55
private array $ cacheKey = [];
67
56
68
57
/**
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
73
62
*/
74
63
public function __construct (LoggerInterface $ logger , ApiClient $ apiClient = null , AbstractAdapter $ adapter =
75
64
null , Geolocation $ geolocation = null )
@@ -83,15 +72,16 @@ public function __construct(LoggerInterface $logger, ApiClient $apiClient = null
83
72
/**
84
73
* Configure this instance.
85
74
*
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
+ *
95
85
* @throws InvalidArgumentException
96
86
*/
97
87
public function configure (
@@ -121,23 +111,18 @@ public function configure(
121
111
'exp_clean_ips ' => $ cacheExpirationForCleanIp ,
122
112
'exp_bad_ips ' => $ cacheExpirationForBadIp ,
123
113
'warmed_up ' => ($ this ->warmedUp ? 'true ' : 'false ' ),
124
- 'geolocation ' => $ this ->geolocConfig
114
+ 'geolocation ' => $ this ->geolocConfig ,
125
115
]);
126
116
$ this ->apiClient ->configure ($ apiUrl , $ timeout , $ userAgent , $ apiKey );
127
117
}
128
118
129
119
/**
130
120
* 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
+ *
136
122
* @throws InvalidArgumentException
137
123
* @throws Exception
138
124
*/
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
141
126
{
142
127
$ item = $ this ->adapter ->getItem (base64_encode ($ cacheKey ));
143
128
@@ -178,6 +163,7 @@ private function addRemediationToCacheItem(string $cacheKey, string $type, int $
178
163
179
164
/**
180
165
* Remove a decision from a Symfony Cache Item identified by a cache key.
166
+ *
181
167
* @throws InvalidArgumentException
182
168
* @throws Exception
183
169
*/
@@ -300,9 +286,7 @@ private function defferUpdateCacheConfig(array $config): void
300
286
301
287
/**
302
288
* Update the cached remediation of the specified cacheKey from these new decisions.
303
- * @param array $decisions
304
- * @param string $cacheKey
305
- * @return string
289
+ *
306
290
* @throws InvalidArgumentException
307
291
*/
308
292
private function saveRemediationsForCacheKey (array $ decisions , string $ cacheKey ): string
@@ -333,25 +317,23 @@ private function saveRemediationsForCacheKey(array $decisions, string $cacheKey)
333
317
}
334
318
335
319
/**
336
- * Cache key convention
337
- * @param string $scope
338
- * @param string $value
339
- * @return string
320
+ * Cache key convention.
321
+ *
340
322
* @throws Exception
341
323
*/
342
324
private function getCacheKey (string $ scope , string $ value ): string
343
325
{
344
- if (!isset ($ this ->cacheKey [$ scope ][$ value ])){
326
+ if (!isset ($ this ->cacheKey [$ scope ][$ value ])) {
345
327
switch ($ scope ) {
346
328
case Constants::SCOPE_RANGE :
347
329
case Constants::SCOPE_IP :
348
- $ this ->cacheKey [$ scope ][$ value ] = Constants::SCOPE_IP . ': ' . $ value ;
330
+ $ this ->cacheKey [$ scope ][$ value ] = Constants::SCOPE_IP . ': ' . $ value ;
349
331
break ;
350
332
case Constants::SCOPE_COUNTRY :
351
- $ this ->cacheKey [$ scope ][$ value ] = Constants::SCOPE_COUNTRY . ': ' . $ value ;
333
+ $ this ->cacheKey [$ scope ][$ value ] = Constants::SCOPE_COUNTRY . ': ' . $ value ;
352
334
break ;
353
335
default :
354
- throw new Exception ('Unknown scope: ' . $ scope );
336
+ throw new Exception ('Unknown scope: ' . $ scope );
355
337
}
356
338
}
357
339
@@ -360,6 +342,7 @@ private function getCacheKey(string $scope, string $value): string
360
342
361
343
/**
362
344
* Update the cached remediations from these new decisions.
345
+ *
363
346
* @throws InvalidArgumentException
364
347
* @throws Exception
365
348
*/
@@ -505,7 +488,6 @@ private function removeRemediations(array $decisions): array
505
488
'value ' => $ decision ['value ' ],
506
489
]);
507
490
}
508
-
509
491
}
510
492
}
511
493
@@ -539,6 +521,7 @@ public function clear(): bool
539
521
* Used when the stream mode has just been activated.
540
522
*
541
523
* @return array "count": number of decisions added, "errors": decisions not added
524
+ *
542
525
* @throws InvalidArgumentException
543
526
*/
544
527
public function warmUp (): array
@@ -580,6 +563,7 @@ public function warmUp(): array
580
563
* Used for the stream mode when we have to update the remediations list.
581
564
*
582
565
* @return array number of deleted and new decisions, and errors when processing decisions
566
+ *
583
567
* @throws InvalidArgumentException
584
568
*/
585
569
public function pullUpdates (): array
@@ -622,9 +606,7 @@ public function pullUpdates(): array
622
606
* In live mode is enabled, calls the API for decisions concerning the specified IP
623
607
* In stream mode, as we consider cache is the single source of truth, the value is considered clean.
624
608
* Finally, the result is stored in caches for further calls.
625
- * @param string $value
626
- * @param string $cacheScope
627
- * @return string
609
+ *
628
610
* @throws InvalidArgumentException
629
611
* @throws Exception
630
612
*/
@@ -633,14 +615,14 @@ private function miss(string $value, string $cacheScope): string
633
615
$ decisions = [];
634
616
$ cacheKey = $ this ->getCacheKey ($ cacheScope , $ value );
635
617
if ($ this ->liveMode ) {
636
- if ( $ cacheScope === Constants:: SCOPE_IP ) {
618
+ if (Constants:: SCOPE_IP === $ cacheScope ) {
637
619
$ this ->logger ->debug ('' , ['type ' => 'DIRECT_API_CALL ' , 'ip ' => $ value ]);
638
620
$ decisions = $ this ->apiClient ->getFilteredDecisions (['ip ' => $ value ]);
639
- } elseif ( $ cacheScope === Constants:: SCOPE_COUNTRY ) {
621
+ } elseif (Constants:: SCOPE_COUNTRY === $ cacheScope ) {
640
622
$ this ->logger ->debug ('' , ['type ' => 'DIRECT_API_CALL ' , 'country ' => $ value ]);
641
623
$ decisions = $ this ->apiClient ->getFilteredDecisions ([
642
624
'scope ' => Constants::SCOPE_COUNTRY ,
643
- 'value ' => $ value
625
+ 'value ' => $ value,
644
626
]);
645
627
}
646
628
}
@@ -652,6 +634,7 @@ private function miss(string $value, string $cacheScope): string
652
634
* Used in both mode (stream and rupture).
653
635
* This method formats the cached item as a remediation.
654
636
* It returns the highest remediation level found.
637
+ *
655
638
* @throws InvalidArgumentException
656
639
*/
657
640
private function hit (string $ ip ): string
@@ -666,13 +649,12 @@ private function hit(string $ip): string
666
649
}
667
650
668
651
/**
669
- * @param string $cacheScope
670
652
* @param $value
671
- * @return string
653
+ *
672
654
* @throws InvalidArgumentException
673
655
* @throws Exception
674
656
*/
675
- private function handleCacheRemediation (string $ cacheScope , $ value ): string
657
+ private function handleCacheRemediation (string $ cacheScope , $ value ): string
676
658
{
677
659
$ cacheKey = $ this ->getCacheKey ($ cacheScope , $ value );
678
660
if ($ this ->adapter ->hasItem (base64_encode ($ cacheKey ))) {
@@ -684,8 +666,7 @@ private function handleCacheRemediation (string $cacheScope, $value): string
684
666
}
685
667
686
668
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 ]);
689
670
} else {
690
671
$ this ->logger ->warning ('' , [
691
672
'type ' => 'BAD_VALUE ' ,
@@ -695,14 +676,15 @@ private function handleCacheRemediation (string $cacheScope, $value): string
695
676
'cache ' => $ cache ,
696
677
]);
697
678
}
698
- return $ remediation ;
699
679
680
+ return $ remediation ;
700
681
}
701
682
702
683
/**
703
684
* Request the cache for the specified IP.
704
685
*
705
686
* @return string the computed remediation string, or null if no decision was found
687
+ *
706
688
* @throws InvalidArgumentException
707
689
* @throws Exception
708
690
*/
@@ -716,7 +698,7 @@ public function get(AddressInterface $address): string
716
698
}
717
699
718
700
// Handle Ip and Range remediation
719
- $ remediations = [[$ this ->handleCacheRemediation (Constants::SCOPE_IP , $ ip ), "" , "" , ]];
701
+ $ remediations = [[$ this ->handleCacheRemediation (Constants::SCOPE_IP , $ ip ), '' , '' ]];
720
702
721
703
// Handle Geolocation remediation
722
704
if (!empty ($ this ->geolocConfig ['enabled ' ])) {
@@ -739,7 +721,7 @@ public function get(AddressInterface $address): string
739
721
]);
740
722
}
741
723
if ($ countryToQuery ) {
742
- $ remediations [] = [$ this ->handleCacheRemediation (Constants::SCOPE_COUNTRY , $ countryToQuery ), "" , "" , ];
724
+ $ remediations [] = [$ this ->handleCacheRemediation (Constants::SCOPE_COUNTRY , $ countryToQuery ), '' , '' ];
743
725
}
744
726
}
745
727
$ prioritizedRemediations = Remediation::sortRemediationByPriority ($ remediations );
0 commit comments