Completed
Push — master ( c29312...7ea0a7 )
by Tobias
04:47
created

QueryLogger::finish()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 19
cp 0
rs 9.3142
cc 3
eloc 15
nc 4
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neo4j\Neo4jBundle\Collector;
6
7
use GraphAware\Common\Cypher\StatementInterface;
8
use GraphAware\Common\Result\StatementResult as StatementResultInterface;
9
10
/**
11
 * @author Xavier Coureau <[email protected]>
12
 */
13
class QueryLogger implements \Countable
14
{
15
    /**
16
     * @var int
17
     */
18
    private $nbQueries = 0;
19
20
    /**
21
     * @var array
22
     */
23
    private $statements = [];
24
25
    /**
26
     * @var array
27
     */
28
    private $statementsHash = [];
29
30
    /**
31
     * @param StatementInterface $statement
32
     */
33
    public function record(StatementInterface $statement)
34
    {
35
        $statementText = $statement->text();
36
        $statementParams = json_encode($statement->parameters());
37
        $tag = $statement->getTag() ?: -1;
38
39
        if (isset($this->statementsHash[$statementText][$statementParams][$tag])) {
40
            return;
41
        }
42
43
        $idx = $this->nbQueries++;
44
        $this->statements[$idx] = [
45
            'start_time' => microtime(true) * 1000,
46
            'end_time' => microtime(true) * 1000, // same
47
            'nb_results' => 0,
48
            'query' => $statementText,
49
            'parameters' => $statementParams,
50
            'tag' => $statement->getTag(),
51
        ];
52
        $this->statementsHash[$statementText][$statementParams][$tag] = $idx;
53
    }
54
55
    /**
56
     * @param StatementResultInterface $statementResult
57
     */
58
    public function finish(StatementResultInterface $statementResult)
59
    {
60
        $statement = $statementResult->statement();
61
        $statementText = $statement->text();
62
        $statementParams = $statement->parameters();
63
        $encodedParameters = json_encode($statementParams);
64
        $tag = $statement->getTag() ?: -1;
65
66
        if (!isset($this->statementsHash[$statementText][$encodedParameters][$tag])) {
67
            $idx = $this->nbQueries++;
68
            $this->statements[$idx]['start_time'] = null;
69
            $this->statementsHash[$idx] = $idx;
70
        } else {
71
            $idx = $this->statementsHash[$statementText][$encodedParameters][$tag];
72
        }
73
74
        $this->statements[$idx] = array_merge($this->statements[$idx], [
75
            'end_time' => microtime(true) * 1000,
76
            'nb_results' => $statementResult->size(),
77
        ]);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function count()
84
    {
85
        return $this->nbQueries;
86
    }
87
88
    /**
89
     * @return array[]
90
     */
91
    public function getStatements()
92
    {
93
        return $this->statements;
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function getStatementsHash()
100
    {
101
        return $this->statementsHash;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function getElapsedTime()
108
    {
109
        $time = 0;
110
111
        foreach ($this->statements as $statement) {
112
            if (!isset($statement['start_time'], $statement['end_time'])) {
113
                continue;
114
            }
115
116
            $time += $statement['end_time'] - $statement['start_time'];
117
        }
118
119
        return $time;
120
    }
121
}
122