Completed
Pull Request — master (#38)
by Armelle
02:29
created

DatabaseDataCollector::getInitData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of Pomm's SymfonyBidge package.
4
 *
5
 * (c) 2014 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PommProject\SymfonyBridge;
12
13
use PommProject\Foundation\Exception\SqlException;
14
use PommProject\Foundation\Listener\Listener;
15
use PommProject\Foundation\Session\Session;
16
17
use Symfony\Component\Stopwatch\Stopwatch;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
21
22
/**
23
 * Data collector for the database profiler.
24
 *
25
 * @package PommSymfonyBridge
26
 * @copyright 2014 Grégoire HUBERT
27
 * @author Jérôme MACIAS
28
 * @author Grégoire HUBERT
29
 * @license X11 {@link http://opensource.org/licenses/mit-license.php}
30
 * @see DataCollector
31
 */
32
class DatabaseDataCollector extends DataCollector
33
{
34
    /** @var Stopwatch */
35
    private $stopwatch;
36
37
    /** @var array */
38
    private $data;
39
40
    public function __construct($unused = null, Stopwatch $stopwatch = null)
41
    {
42
        if ($unused !== null) {
43
            trigger_error("The parameter Pomm has been deleted for to delete the high dependency.", E_USER_DEPRECATED);
44
        }
45
46
        $this->stopwatch = $stopwatch;
47
        $this->data = $this->getInitData();
48
    }
49
50
    /**
51
     * @param string $name
52
     * @param array $data
53
     * @param $session
54
     *
55
     * @return null
56
     */
57
    public function execute($name, $data, Session $session)
0 ignored issues
show
Unused Code introduced by
The parameter $session is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        switch ($name) {
60
            case 'query:post':
61
                $this->data['time'] += $data['time_ms'];
62
                $data += array_pop($this->data['queries']);
63
                /* fall-through */
64
            case 'query:pre':
65
                $this->data['queries'][] = $data;
66
                break;
67
        }
68
69
        $this->watch($name);
70
    }
71
72
    private function watch($name)
73
    {
74
        if ($this->stopwatch !== null) {
75
            switch ($name) {
76
                case 'query:pre':
77
                    $this->stopwatch->start('query.pomm', 'pomm');
78
                    break;
79
                case 'query:post':
80
                    $this->stopwatch->stop('query.pomm');
81
                    break;
82
            }
83
        }
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function collect(Request $request, Response $response, \Exception $exception = null)
90
    {
91
        if ($exception instanceof SqlException) {
92
            $this->data['exception'] = $exception->getMessage();
93
        }
94
    }
95
96
    /**
97
     * Return the list of queries sent.
98
     *
99
     * @return array
100
     */
101
    public function getQueries()
102
    {
103
        return $this->data['queries'];
104
    }
105
106
    /**
107
     * Return the number of queries sent.
108
     *
109
     * @return integer
110
     */
111
    public function getQuerycount()
112
    {
113
        return count($this->data['queries']);
114
    }
115
116
    /**
117
     * Return queries total time.
118
     *
119
     * @return float
120
     */
121
    public function getTime()
122
    {
123
        return $this->data['time'];
124
    }
125
126
    /**
127
     * Return sql exception.
128
     *
129
     * @return \PommProject\Foundation\Exception\SqlException|null
130
     */
131
    public function getException()
132
    {
133
        return $this->data['exception'];
134
    }
135
136
    /**
137
     * Return profiler identifier.
138
     *
139
     * @return string
140
     */
141
    public function getName()
142
    {
143
        return 'pomm';
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function reset()
150
    {
151
        $this->stopwatch->reset();
152
        $this->data = $this->getInitData();
153
    }
154
155
    /**
156
     * Get initial data array
157
     */
158
    private function getInitData()
159
    {
160
        return [
161
            'time' => 0,
162
            'queries' => [],
163
            'exception' => null,
164
        ];
165
    }
166
}
167