Test Failed
Pull Request — master (#9)
by
unknown
05:49
created

LogLevelConfiguration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addLogLevelThreshold() 0 4 1
A getApplicableLogLevel() 0 4 2
A determineApplicableLogLevel() 0 23 1
1
<?php
2
3
namespace Abacaphiliac\Doctrine;
4
5
class LogLevelConfiguration
6
{
7
    /** @var array */
8
    private $logLevelMapping = [];
9
10
    public function __construct(array $logLevelMapping)
11
    {
12
        foreach ($logLevelMapping as $logLevelPriority => $durationThresholdInMilliseconds) {
13
            $this->addLogLevelThreshold($logLevelPriority, $durationThresholdInMilliseconds);
14
        }
15
    }
16
17
    private function addLogLevelThreshold(string $logLevelPriority, int $durationThresholdInMilliseconds) : void
18
    {
19
        $this->logLevelMapping[$logLevelPriority] = $durationThresholdInMilliseconds;
20
    }
21
22
    public function getApplicableLogLevel(float $durationInSeconds): ?string
23
    {
24
        return count($this->logLevelMapping) > 0 ? $this->determineApplicableLogLevel($durationInSeconds) : null;
25
    }
26
27
    private function determineApplicableLogLevel(float $durationInSeconds) : string
28
    {
29
        $durationInMilliseconds = $durationInSeconds * 1000;
30
31
        //Acquire a common / non-associative array with all the thresholds
32
        $durationThresholds = array_values($this->logLevelMapping);
33
34
        //Append the incoming query duration in milliseconds to the array of duration thresholds
35
        $durationThresholds[] = $durationInMilliseconds;
36
37
        //Sort the array from low to high: the provided duration will end up somewhere between the thresholds
38
        asort($durationThresholds, SORT_NUMERIC);
39
40
        //A re-index is required after sorting
41
        $durationThresholds = array_values($durationThresholds);
42
43
        //Determine at which position the duration ended up after sorting
44
        $key = array_search($durationInMilliseconds, $durationThresholds, true);
45
46
        $logLevels = array_keys($this->logLevelMapping);
47
48
        return $logLevels[$key - 1]; //Now take the "previous" key
49
    }
50
}
51