Completed
Pull Request — master (#602)
by Tom
08:16
created

SQLLoggerCollector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 88
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule\Collector;
6
7
use Doctrine\DBAL\Logging\DebugStack;
8
use Laminas\DeveloperTools\Collector\AutoHideInterface;
9
use Laminas\DeveloperTools\Collector\CollectorInterface;
10
use Laminas\Mvc\MvcEvent;
11
use function count;
12
13
/**
14
 * Collector to be used in Laminas\DeveloperTools to record and display SQL queries
15
 *
16
 * @link    www.doctrine-project.org
17
 */
18
class SQLLoggerCollector implements CollectorInterface, AutoHideInterface
19
{
20
    /**
21
     * Collector priority
22
     */
23
    public const PRIORITY = 10;
24
25
    protected DebugStack $sqlLogger;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
26
27
    protected string $name;
28
29
    public function __construct(DebugStack $sqlLogger, string $name)
30
    {
31
        $this->sqlLogger = $sqlLogger;
32
        $this->name      = (string) $name;
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function getName()
39
    {
40 11
        return $this->name;
41
    }
42 11
43 11
    /**
44 11
     * {@inheritDoc}
45
     */
46
    public function getPriority()
47
    {
48
        return self::PRIORITY;
49 2
    }
50
51 2
    /**
52
     * {@inheritDoc}
53
     */
54
    public function collect(MvcEvent $mvcEvent)
55
    {
56
    }
57 1
58
    /**
59 1
     * {@inheritDoc}
60
     */
61
    public function canHide()
62
    {
63
        return empty($this->sqlLogger->queries);
64
    }
65 1
66
    public function getQueryCount() : int
67 1
    {
68
        return count($this->sqlLogger->queries);
69
    }
70
71
    /**
72 1
     * @return mixed[]
73
     */
74 1
    public function getQueries() : array
75
    {
76
        return $this->sqlLogger->queries;
77
    }
78
79
    public function getQueryTime() : float
80 1
    {
81
        $time = 0.0;
82 1
83
        foreach ($this->sqlLogger->queries as $query) {
84
            $time += $query['executionMS'];
85
        }
86
87
        return $time;
88
    }
89
}
90