Passed
Push — master ( 0bf991...cb753f )
by Timothy
57s queued 10s
created

getStopQueryCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Abacaphiliac\Doctrine;
4
5
use Doctrine\DBAL\Logging\SQLLogger;
6
use InvalidArgumentException;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LogLevel;
9
use function array_merge;
10
use function call_user_func;
11
use function is_callable;
12
use function microtime;
13
use function uniqid;
14
15
final class PsrSqlLoggerConfigurableLogLevels implements SQLLogger
16
{
17
    /** @var LoggerInterface */
18
    private $logger;
19
20
    /** @var string */
21
    private $sql;
22
23
    /** @var float */
24
    private $start;
25
26
    /** @var callable */
27
    private $startQueryCallable;
28
29
    /** @var string */
30
    private $queryId;
31
32
    /** @var string */
33
    private $defaultLogLevel;
34
35
    /** @var LogLevelConfiguration|null */
36
    private $logLevelConfiguration;
37
38 4
    public function __construct(
39
        LoggerInterface $logger,
40
        LogLevelConfiguration $logLevelConfiguration,
41
        string $defaultLogLevel = LogLevel::INFO
42
    ) {
43 4
        $this->logger                = $logger;
44 4
        $this->logLevelConfiguration = $logLevelConfiguration;
45 4
        $this->defaultLogLevel       = $defaultLogLevel;
46 4
        $this->startQueryCallable    = $this->getStartQueryCallable($defaultLogLevel);
47 3
    }
48
49 4
    private function getStartQueryCallable(string $level): callable
50
    {
51 4
        $callable = $this->getLoggerCallable($level);
52
53 4
        if (!is_callable($callable)) {
54 1
            throw new InvalidArgumentException(sprintf(
55 1
                '%s::%s" is not callable',
56 1
                LoggerInterface::class,
57 1
                $this->defaultLogLevel
58
            ));
59
        }
60
61 3
        return $callable;
62
    }
63
64 4
    private function getLoggerCallable(string $level) : array
65
    {
66 4
        return [$this->logger, $level];
67
    }
68
69 3
    public function startQuery($sql, array $params = null, array $types = null) : void
70
    {
71 3
        $this->sql = $sql;
72 3
        $this->queryId = uniqid('', true);
73 3
        $this->start = microtime(true);
74
75 3
        call_user_func($this->startQueryCallable, 'Query started', array_merge(
76 3
            $this->getStartQueryContext($sql, $params, $types),
77
            [
78 3
                'query_id' => $this->queryId,
79
            ]
80
        ));
81 3
    }
82
83 3
    protected function getStartQueryContext($sql, array $params = null, array $types = null) : array
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        return [
86 3
            'sql' => $sql,
87 3
            'types' => $types,
88
        ];
89
    }
90
91 3
    public function stopQuery() : void
92
    {
93 3
        $stop = microtime(true);
94 3
        $durationInSeconds = $stop - $this->start;
95
96 3
        call_user_func($this->getStopQueryCallable($durationInSeconds), 'Query finished', [
97 3
            'query_id' => $this->queryId,
98 3
            'start' => $this->start,
99 3
            'stop' => $stop,
100 3
            'duration_s' => $durationInSeconds,
101 3
            'sql' => $this->sql,
102
        ]);
103 3
    }
104
105 3
    private function getStopQueryCallable(float $durationInSeconds): callable
106
    {
107 3
        return $this->getLoggerCallable($this->getApplicableLogLevel($durationInSeconds) ?? $this->defaultLogLevel);
108
    }
109
110 3
    private function getApplicableLogLevel(float $durationInSeconds): ?string
111
    {
112 3
        return $this->logLevelConfiguration instanceof LogLevelConfiguration
113 3
            ? $this->logLevelConfiguration->getApplicableLogLevel($durationInSeconds)
114 3
            : $this->defaultLogLevel;
115
    }
116
}
117