Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.43% |
40 / 51 |
|
77.78% |
7 / 9 |
CRAP | |
0.00% |
0 / 1 |
QueryHandler | |
78.43% |
40 / 51 |
|
77.78% |
7 / 9 |
26.86 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setProfiler | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasProfiler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getProfiler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
profiler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
prepare | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
prepareHeaderAsString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
prepareAsString | |
56.52% |
13 / 23 |
|
0.00% |
0 / 1 |
15.66 | |||
__get | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Debug\Handler; |
15 | |
16 | use Pop\Db\Adapter\Profiler\Profiler; |
17 | |
18 | /** |
19 | * Debug query handler class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Debug |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 1.3.0 |
27 | */ |
28 | class QueryHandler extends AbstractHandler |
29 | { |
30 | |
31 | /** |
32 | * Profiler |
33 | * @var Profiler |
34 | */ |
35 | protected $profiler = null; |
36 | |
37 | /** |
38 | * Constructor |
39 | * |
40 | * Instantiate a query handler object |
41 | * |
42 | * @param Profiler $profiler |
43 | * @param string $name |
44 | */ |
45 | public function __construct(Profiler $profiler = null, $name = null) |
46 | { |
47 | parent::__construct($name); |
48 | |
49 | if (null !== $profiler) { |
50 | $this->setProfiler($profiler); |
51 | } |
52 | } |
53 | |
54 | /** |
55 | * Set profiler |
56 | * |
57 | * @param Profiler $profiler |
58 | * @return self |
59 | */ |
60 | public function setProfiler(Profiler $profiler) |
61 | { |
62 | $this->profiler = $profiler; |
63 | return $this; |
64 | } |
65 | |
66 | /** |
67 | * Determine if the handler has a profiler |
68 | * |
69 | * @return boolean |
70 | */ |
71 | public function hasProfiler() |
72 | { |
73 | return (null !== $this->profiler); |
74 | } |
75 | |
76 | /** |
77 | * Get profiler |
78 | * |
79 | * @return Profiler |
80 | */ |
81 | public function getProfiler() |
82 | { |
83 | return $this->profiler; |
84 | } |
85 | |
86 | /** |
87 | * Get profiler (alias method) |
88 | * |
89 | * @return Profiler |
90 | */ |
91 | public function profiler() |
92 | { |
93 | return $this->profiler; |
94 | } |
95 | |
96 | /** |
97 | * Prepare handler data for storage |
98 | * |
99 | * @return array |
100 | */ |
101 | public function prepare() |
102 | { |
103 | $data = [ |
104 | 'start' => number_format((float)$this->profiler->getStart(), 5, '.', ''), |
105 | 'finish' => number_format((float)$this->profiler->getFinish(), 5, '.', ''), |
106 | 'elapsed' => $this->profiler->getElapsed(), |
107 | 'steps' => [] |
108 | ]; |
109 | |
110 | foreach ($this->profiler->getSteps() as $step) { |
111 | $data['steps'][] = [ |
112 | 'start' => number_format($step->getStart(), 5, '.', ''), |
113 | 'finish' => number_format($step->getFinish(), 5, '.', ''), |
114 | 'elapsed' => $step->getElapsed(), |
115 | 'query' => $step->getQuery(), |
116 | 'params' => $step->getParams(), |
117 | 'errors' => $step->getErrors() |
118 | ]; |
119 | } |
120 | |
121 | return $data; |
122 | } |
123 | |
124 | /** |
125 | * Prepare header string |
126 | * |
127 | * @return string |
128 | */ |
129 | public function prepareHeaderAsString() |
130 | { |
131 | $string = ((!empty($this->name)) ? $this->name . ' ' : '') . 'Query Handler'; |
132 | $string .= PHP_EOL . str_repeat('=', strlen($string)) . PHP_EOL; |
133 | |
134 | return $string; |
135 | } |
136 | |
137 | /** |
138 | * Prepare handler data as string |
139 | * |
140 | * @return string |
141 | */ |
142 | public function prepareAsString() |
143 | { |
144 | $string = "Start:\t\t\t" . number_format((float)$this->profiler->getStart(), 5, '.', '') . PHP_EOL; |
145 | $string .= "Finish:\t\t\t" . number_format((float)$this->profiler->getFinish(), 5, '.', '') . PHP_EOL; |
146 | $string .= "Elapsed:\t\t" . $this->profiler->getElapsed() . ' seconds' . PHP_EOL . PHP_EOL; |
147 | |
148 | $string .= "Queries:" . PHP_EOL; |
149 | $string .= "--------" . PHP_EOL; |
150 | foreach ($this->profiler->getSteps() as $step) { |
151 | $string .= $step->getQuery() . ' [' . $step->getElapsed() . ']' . PHP_EOL; |
152 | $string .= "Start:\t\t\t" . number_format($step->getStart(), 5, '.', '') . PHP_EOL; |
153 | $string .= "Finish:\t\t\t" . number_format($step->getFinish(), 5, '.', '') . PHP_EOL; |
154 | if ($step->hasParams()) { |
155 | $string .= "Params:" . PHP_EOL; |
156 | foreach ($step->getParams() as $name => $value) { |
157 | if (is_array($value)) { |
158 | foreach ($value as $v) { |
159 | $string .= "\t" . $name . ' => ' . $v . PHP_EOL; |
160 | } |
161 | } else { |
162 | $string .= "\t" . $name . ' => ' . $value . PHP_EOL; |
163 | } |
164 | } |
165 | } |
166 | if ($step->hasErrors()) { |
167 | $string .= "Errors:" . PHP_EOL; |
168 | foreach ($step->getErrors() as $time => $error) { |
169 | $string .= "\t[" . number_format($time, 5, '.', '') . "]" . $error['error'] . |
170 | ((!empty($error['number'])) ? ' [' . $error['number'] . ']' : '') . PHP_EOL; |
171 | } |
172 | |
173 | } |
174 | $string .= PHP_EOL; |
175 | } |
176 | |
177 | return $string; |
178 | } |
179 | |
180 | /** |
181 | * Magic get method to return the profiler. |
182 | * |
183 | * @param string $name |
184 | * @return mixed |
185 | */ |
186 | public function __get($name) |
187 | { |
188 | switch ($name) { |
189 | case 'profiler': |
190 | return $this->profiler; |
191 | break; |
192 | default: |
193 | return null; |
194 | } |
195 | } |
196 | |
197 | } |