Completed
Push — profiler ( 93e31d...158132 )
by Akihito
01:29
created

DbProfiler::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the Ray.AuraSqlModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\AuraSqlModule;
8
9
use Aura\Sql\ExtendedPdoInterface;
10
use Aura\Sql\Profiler;
11
use Psr\Log\LoggerInterface;
12
use Ray\Di\InjectorInterface;
13
14
final class DbProfiler
15
{
16
    /**
17
     * @var ExtendedPdoInterface
18
     */
19
    private $pdo;
20
21
    /**
22
     * @var LoggerInterface
23
     */
24
    private $logger;
25
26
    public function __construct(InjectorInterface $injector)
27
    {
28
        $this->pdo = $injector->getInstance(ExtendedPdoInterface::class);
29
        $this->logger = $injector->getInstance(LoggerInterface::class);
30
    }
31
32
    public function setup() : void
33
    {
34
        $this->pdo->setProfiler(new Profiler);
35
        $this->pdo->getProfiler()->setActive(true);
36
    }
37
38
    public function tearDown() : void
39
    {
40
        $profiles = $this->pdo->getProfiler()->getProfiles();
41
        foreach ($profiles as &$profile) {
42
            unset($profile['trace'], $profile['duration']);
43
        }
44
        unset($profile);
45
        if ($profiles) {
46
            $this->logger->debug('sql:', $profiles);
47
        }
48
    }
49
}
50