Passed
Pull Request — master (#504)
by Def
02:12
created

SimpleProfiler::end()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Profiler;
6
7
use Psr\Log\LoggerInterface;
8
9
class SimpleProfiler implements ProfilerInterface
10
{
11
    private array $tokens = [];
12
13
    public function __construct(private LoggerInterface $logger)
14
    {
15
    }
16
17
    public function begin(string $token, array $context = []): void
18
    {
19
        $key = $this->getKey([$token, $context]);
20
        $this->tokens[$key] = microtime(true);
21
        $this->logger->info(sprintf('Begin of query "%s"', $token), $context);
22
    }
23
24
    private function getKey(array $params): string
25
    {
26
        return json_encode($params);
27
    }
28
29
    public function end(string $token, array $context = []): void
30
    {
31
        $key = $this->getKey([$token, $context]);
32
        if (isset($this->tokens[$key])) {
33
            $elapsed = microtime(true) - $this->tokens[$key];
34
        }
35
36
        $this->logger->info(sprintf('End of query "%s"', $token), ['elapsed' => $elapsed ?? 'undefined'] + $context);
37
    }
38
}
39