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

SimpleProfiler::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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