88use Symfony \Component \Cache \PruneableInterface ;
99use Psr \Log \LoggerInterface ;
1010use Symfony \Component \Cache \Adapter \FilesystemAdapter ;
11+ use \DateTime ;
1112
1213/**
1314 * The cache mecanism to store every decisions from LAPI/CAPI. Symfony Cache component powered.
@@ -76,7 +77,7 @@ public function configure(
7677 /**
7778 * Add remediation to a Symfony Cache Item identified by IP
7879 */
79- private function addRemediationToCacheItem (string $ ip , string $ type , int $ expiration , int $ decisionId ): void
80+ private function addRemediationToCacheItem (string $ ip , string $ type , int $ expiration , int $ decisionId ): string
8081 {
8182 $ item = $ this ->adapter ->getItem ($ ip );
8283
@@ -103,7 +104,7 @@ private function addRemediationToCacheItem(string $ip, string $type, int $expira
103104 $ prioritizedRemediations = Remediation::sortRemediationByPriority ($ remediations );
104105
105106 $ item ->set ($ prioritizedRemediations );
106- $ item ->expiresAfter ( $ maxLifetime );
107+ $ item ->expiresAt ( new DateTime ( ' @ ' . $ maxLifetime) );
107108
108109 // Save the cache without committing it to the cache system.
109110 // Useful to improve performance when updating the cache.
@@ -113,6 +114,7 @@ private function addRemediationToCacheItem(string $ip, string $type, int $expira
113114 "$ type for $ expiration sec, (decision $ decisionId) "
114115 );
115116 }
117+ return $ prioritizedRemediations [0 ][0 ];
116118 }
117119
118120 /**
@@ -143,7 +145,7 @@ private function removeDecisionFromRemediationItem(string $ip, int $decisionId):
143145 // Build the item lifetime in cache and sort remediations by priority
144146 $ maxLifetime = max (array_column ($ remediations , 1 ));
145147 $ cacheContent = Remediation::sortRemediationByPriority ($ remediations );
146- $ item ->expiresAfter ( $ maxLifetime );
148+ $ item ->expiresAt ( new DateTime ( ' @ ' . $ maxLifetime) );
147149 $ item ->set ($ cacheContent );
148150
149151 // Save the cache without commiting it to the cache system.
@@ -174,23 +176,23 @@ private static function parseDurationToSeconds(string $duration): int
174176 };
175177 $ seconds = 0 ;
176178 if (isset ($ matches [2 ])) {
177- $ seconds += ((int ) $ matches [1 ]) * 3600 ; // hours
179+ $ seconds += ((int ) $ matches [2 ]) * 3600 ; // hours
178180 }
179181 if (isset ($ matches [3 ])) {
180- $ seconds += ((int ) $ matches [2 ]) * 60 ; // minutes
182+ $ seconds += ((int ) $ matches [3 ]) * 60 ; // minutes
181183 }
182184 if (isset ($ matches [4 ])) {
183- $ seconds += ((int ) $ matches [1 ]); // seconds
185+ $ seconds += ((int ) $ matches [4 ]); // seconds
184186 }
185- if (isset ($ matches [5 ])) { // units in milliseconds
187+ if (' m ' === ($ matches [5 ])) { // units in milliseconds
186188 $ seconds *= 0.001 ;
187189 }
188- if (isset ($ matches [1 ])) { // negative
190+ if (" - " === ($ matches [1 ])) { // negative
189191 $ seconds *= -1 ;
190192 }
191- $ seconds = round ($ seconds );
192193
193- return (int )$ seconds ;
194+ $ seconds = (int )round ($ seconds );
195+ return $ seconds ;
194196 }
195197
196198
@@ -271,8 +273,9 @@ private function removeRemediations(array $decisions): bool
271273 /**
272274 * Update the cached remediation of the specified IP from these new decisions.
273275 */
274- private function saveRemediationsForIp (array $ decisions , string $ ip ): void
276+ private function saveRemediationsForIp (array $ decisions , string $ ip ): string
275277 {
278+ $ remediationResult = Constants::REMEDIATION_BYPASS ;
276279 if (\count ($ decisions )) {
277280 foreach ($ decisions as $ decision ) {
278281 if (!in_array ($ decision ['type ' ], Constants::ORDERED_REMEDIATIONS )) {
@@ -282,13 +285,14 @@ private function saveRemediationsForIp(array $decisions, string $ip): void
282285 $ decision ['type ' ] = $ highestRemediationLevel ;
283286 }
284287 $ remediation = $ this ->formatRemediationFromDecision ($ decision );
285- $ this ->addRemediationToCacheItem ($ ip , $ remediation [0 ], $ remediation [1 ], $ remediation [2 ]);
288+ $ remediationResult = $ this ->addRemediationToCacheItem ($ ip , $ remediation [0 ], $ remediation [1 ], $ remediation [2 ]);
286289 }
287290 } else {
288291 $ remediation = $ this ->formatRemediationFromDecision (null );
289- $ this ->addRemediationToCacheItem ($ ip , $ remediation [0 ], $ remediation [1 ], $ remediation [2 ]);
292+ $ remediationResult = $ this ->addRemediationToCacheItem ($ ip , $ remediation [0 ], $ remediation [1 ], $ remediation [2 ]);
290293 }
291294 $ this ->adapter ->commit ();
295+ return $ remediationResult ;
292296 }
293297
294298 public function clear (): bool
@@ -364,8 +368,7 @@ private function miss(string $ip): string
364368 $ decisions = $ this ->apiClient ->getFilteredDecisions (['ip ' => $ ip ]);
365369 }
366370
367- $ this ->saveRemediationsForIp ($ decisions , $ ip );
368- return $ this ->hit ($ ip );
371+ return $ this ->saveRemediationsForIp ($ decisions , $ ip );
369372 }
370373
371374 /**
0 commit comments