Skip to content

Commit

Permalink
style(*): Pass through code format tools
Browse files Browse the repository at this point in the history
  • Loading branch information
julienloizelet committed Dec 25, 2024
1 parent aa537f0 commit 07ab9bf
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 241 deletions.
147 changes: 75 additions & 72 deletions src/AbstractRemediation.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ public function getOriginsCount(): array
return $this->getOriginsCountItem();
}

/**
* @throws CacheException
* @throws InvalidArgumentException
*/
public function incrementRemediationOriginCount(string $origin, string $remediation): int
{
$cacheOriginCount = $this->getOriginsCountItem();
$count = isset($cacheOriginCount[$origin][$remediation]) ?
(int) $cacheOriginCount[$origin][$remediation] :
0;

$this->cacheStorage->upsertItem(
AbstractCache::ORIGINS_COUNT,
[
$origin => [
$remediation => ++$count,
],
],
0,
[AbstractCache::ORIGINS_COUNT]
);

return $count;
}

/**
* Prune cache.
*
Expand All @@ -111,6 +136,29 @@ public function pruneCache(): bool
*/
abstract public function refreshDecisions(): array;

/**
* @throws CacheException
* @throws InvalidArgumentException
*/
public function resetRemediationOriginCount(string $origin, string $remediation): bool
{
$cacheOriginCount = $this->getOriginsCountItem();
if (!isset($cacheOriginCount[$origin][$remediation])) {
return false;
}

return $this->cacheStorage->upsertItem(
AbstractCache::ORIGINS_COUNT,
[
$origin => [
$remediation => 0,
],
],
0,
[AbstractCache::ORIGINS_COUNT]
);
}

protected function convertRawDecision(array $rawDecision): ?Decision
{
if (!$this->validateRawDecision($rawDecision)) {
Expand Down Expand Up @@ -199,10 +247,10 @@ protected function getIpType(string $ip): int
*/
protected function parseDurationToSeconds(string $duration): int
{
$re = '/(-?)((\d+)h)?((\d+)m)?((\d+)(\.\d+)?s|(\d+)ms)?/m';
$re = '/(-)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)(\.\d+)?s)?(?:(\d+)ms)?/';
preg_match($re, $duration, $matches);

if (empty($matches[2]) && empty($matches[4]) && empty($matches[7]) && empty($matches[9])) {
if (empty($matches[0])) {
$this->logger->error('An error occurred during duration parsing', [
'type' => 'REM_DECISION_DURATION_PARSE_ERROR',
'duration' => $duration,
Expand All @@ -212,34 +260,37 @@ protected function parseDurationToSeconds(string $duration): int
}

$seconds = 0;

// Parse hours
if (isset($matches[3])) {
$seconds += ((int)$matches[3]) * 3600;
if (!empty($matches[2])) {
$seconds += ((int) $matches[2]) * 3600;
}

// Parse minutes
if (isset($matches[5])) {
$seconds += ((int)$matches[5]) * 60;
if (!empty($matches[3])) {
$seconds += ((int) $matches[3]) * 60;
}

// Parse seconds and milliseconds
$secondsPart = 0;
if (isset($matches[7])) { // seconds part
$secondsPart += (float)$matches[7];
// Parse seconds
if (!empty($matches[4])) {
$seconds += (int) $matches[4];
}
if (isset($matches[9])) { // milliseconds part
$secondsPart += ((int)$matches[9]) * 0.001;

// Parse fractional seconds
if (!empty($matches[5])) {
$seconds += (float) $matches[5];
}

$seconds += $secondsPart;
// Parse milliseconds
if (!empty($matches[6])) {
$seconds += ((int) $matches[6]) * 0.001;
}

// Handle negative durations
if ('-' === $matches[1]) {
$seconds *= -1;
}

return (int)round($seconds);
return (int) round($seconds);
}

/**
Expand All @@ -256,8 +307,8 @@ protected function parseDurationToSeconds(string $duration): int
protected function processCachedDecisions(array $cacheDecisions): string
{
$remediationData = $this->retrieveRemediationFromCachedDecisions($cacheDecisions);
$origin = !empty($remediationData[self::INDEX_ORIGIN]) ? (string)$remediationData[self::INDEX_ORIGIN] : '';
$remediation = !empty($remediationData[self::INDEX_REM]) ? (string)$remediationData[self::INDEX_REM] :
$origin = !empty($remediationData[self::INDEX_ORIGIN]) ? (string) $remediationData[self::INDEX_ORIGIN] : '';
$remediation = !empty($remediationData[self::INDEX_REM]) ? (string) $remediationData[self::INDEX_REM] :
Constants::REMEDIATION_BYPASS;
if ($origin) {
$this->incrementRemediationOriginCount($origin, $remediation);
Expand Down Expand Up @@ -333,54 +384,6 @@ protected function storeDecisions(array $decisions): array
];
}

/**
* @throws CacheException
* @throws InvalidArgumentException
*/
public function incrementRemediationOriginCount(string $origin, string $remediation): int
{
$cacheOriginCount = $this->getOriginsCountItem();
$count = isset($cacheOriginCount[$origin][$remediation]) ?
(int)$cacheOriginCount[$origin][$remediation] :
0;

$this->cacheStorage->upsertItem(
AbstractCache::ORIGINS_COUNT,
[
$origin => [
$remediation => ++$count
]
],
0,
[AbstractCache::ORIGINS_COUNT]
);

return $count;
}

/**
* @throws CacheException
* @throws InvalidArgumentException
*/
public function resetRemediationOriginCount(string $origin, string $remediation): bool
{
$cacheOriginCount = $this->getOriginsCountItem();
if (!isset($cacheOriginCount[$origin][$remediation])) {
return false;
}

return $this->cacheStorage->upsertItem(
AbstractCache::ORIGINS_COUNT,
[
$origin => [
$remediation => 0
]
],
0,
[AbstractCache::ORIGINS_COUNT]
);
}

/**
* Cap the remediation to a fixed value given by the bouncing level configuration.
*
Expand All @@ -390,11 +393,11 @@ public function resetRemediationOriginCount(string $origin, string $remediation)
*/
private function capRemediationLevel(string $remediation): string
{
if ($remediation === Constants::REMEDIATION_BYPASS) {
if (Constants::REMEDIATION_BYPASS === $remediation) {
return Constants::REMEDIATION_BYPASS;
}

$orderedRemediations = (array)$this->getConfig('ordered_remediations');
$orderedRemediations = (array) $this->getConfig('ordered_remediations');

$bouncingLevel = $this->getConfig('bouncing_level') ?? Constants::BOUNCING_LEVEL_NORMAL;
// Compute max remediation level
Expand All @@ -411,8 +414,8 @@ private function capRemediationLevel(string $remediation): string
break;
}

$currentIndex = (int)array_search($remediation, $orderedRemediations);
$maxIndex = (int)array_search(
$currentIndex = (int) array_search($remediation, $orderedRemediations);
$maxIndex = (int) array_search(
$maxRemediationLevel,
$orderedRemediations
);
Expand Down Expand Up @@ -463,7 +466,7 @@ private function handleDecisionExpiresAt(string $type, string $duration): int
{
$duration = $this->parseDurationToSeconds($duration);
if (Constants::REMEDIATION_BYPASS !== $type && !$this->getConfig('stream_mode')) {
$duration = min((int)$this->getConfig('bad_ip_cache_duration'), $duration);
$duration = min((int) $this->getConfig('bad_ip_cache_duration'), $duration);
}

return time() + $duration;
Expand Down Expand Up @@ -514,7 +517,7 @@ private function retrieveRemediationFromCachedDecisions(array $cacheDecisions):

return [
self::INDEX_REM => $cappedRemediation,
self::INDEX_ORIGIN => $cappedRemediation === Constants::REMEDIATION_BYPASS ? AbstractCache::CLEAN : $origin,
self::INDEX_ORIGIN => Constants::REMEDIATION_BYPASS === $cappedRemediation ? AbstractCache::CLEAN : $origin,
];
}

Expand All @@ -527,7 +530,7 @@ private function sortDecisionsByPriority(array $decisions): array
return $decisions;
}
// Add priorities
$orderedRemediations = (array)$this->getConfig('ordered_remediations');
$orderedRemediations = (array) $this->getConfig('ordered_remediations');
$fallback = $this->getConfig('fallback_remediation');
$decisionsWithPriority = [];
foreach ($decisions as $decision) {
Expand Down
Loading

0 comments on commit 07ab9bf

Please sign in to comment.