Completed
Pull Request — master (#38)
by Armelle
04:32
created

DatabaseDataCollector::collect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 3
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
    public function __construct($unused = null, Stopwatch $stopwatch = null)
38
    {
39
        if ($unused !== null) {
40
            trigger_error("The parameter Pomm has been deleted for to delete the high dependency.", E_USER_DEPRECATED);
41
        }
42
43
        $this->stopwatch = $stopwatch;
44
        $this->initData();
45
    }
46
47
    /**
48
     * @param string $name
49
     * @param array $data
50
     * @param $session
51
     *
52
     * @return null
53
     */
54
    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...
55
    {
56
        switch ($name) {
57
            case 'query:post':
58
                $this->data['time'] += $data['time_ms'];
59
                $data += array_pop($this->data['queries']);
60
                /* fall-through */
61
            case 'query:pre':
62
                $this->data['queries'][] = $data;
63
                break;
64
        }
65
66
        $this->watch($name);
67
    }
68
69
    private function watch($name)
70
    {
71
        if ($this->stopwatch !== null) {
72
            switch ($name) {
73
                case 'query:pre':
74
                    $this->stopwatch->start('query.pomm', 'pomm');
75
                    break;
76
                case 'query:post':
77
                    $this->stopwatch->stop('query.pomm');
78
                    break;
79
            }
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function collect(Request $request, Response $response, \Exception $exception = null)
87
    {
88
        if ($exception instanceof SqlException) {
89
            $this->data['exception'] = $exception->getMessage();
90
        }
91
    }
92
93
    /**
94
     * Return the list of queries sent.
95
     *
96
     * @return array
97
     */
98
    public function getQueries()
99
    {
100
        return $this->data['queries'];
101
    }
102
103
    /**
104
     * Return the number of queries sent.
105
     *
106
     * @return integer
107
     */
108
    public function getQuerycount()
109
    {
110
        return count($this->data['queries']);
111
    }
112
113
    /**
114
     * Return queries total time.
115
     *
116
     * @return float
117
     */
118
    public function getTime()
119
    {
120
        return $this->data['time'];
121
    }
122
123
    /**
124
     * Return sql exception.
125
     *
126
     * @return \PommProject\Foundation\Exception\SqlException|null
127
     */
128
    public function getException()
129
    {
130
        return $this->data['exception'];
131
    }
132
133
    /**
134
     * Return profiler identifier.
135
     *
136
     * @return string
137
     */
138
    public function getName()
139
    {
140
        return 'pomm';
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function reset()
147
    {
148
        $this->stopwatch->reset();
149
        $this->initData();
150
    }
151
152
    /**
153
     * Init data array
154
     */
155
    private function initData()
156
    {
157
        $this->data = [
158
            'time' => 0,
159
            'queries' => [],
160
            'exception' => null,
161
        ];
162
    }
163
}
164