Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| TimeHandler | |
100.00% |
28 / 28 |
|
100.00% |
3 / 3 |
9 | |
100.00% |
1 / 1 |
| prepare | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| prepareMessage | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| log | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <dev@noladev.com> |
| 8 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Debug\Handler; |
| 16 | |
| 17 | /** |
| 18 | * Debug time handler class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Debug |
| 22 | * @author Nick Sagona, III <dev@noladev.com> |
| 23 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 4.0.0 |
| 26 | */ |
| 27 | class TimeHandler extends AbstractHandler |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Prepare handler data for storage |
| 32 | * |
| 33 | * @return array |
| 34 | */ |
| 35 | public function prepare(): array |
| 36 | { |
| 37 | if ($this->end === null) { |
| 38 | $this->setEnd(); |
| 39 | } |
| 40 | |
| 41 | if (!$this->hasData()) { |
| 42 | $this->setData([ |
| 43 | 'start' => number_format($this->start, 5, '.', ''), |
| 44 | 'end' => number_format($this->end, 5, '.', ''), |
| 45 | 'elapsed' => $this->getElapsed() |
| 46 | ]); |
| 47 | } |
| 48 | |
| 49 | return $this->getData(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Prepare handler message |
| 54 | * |
| 55 | * @param ?array $context |
| 56 | * @return string |
| 57 | */ |
| 58 | public function prepareMessage(?array $context = null): string |
| 59 | { |
| 60 | $elapsedTime = $this->getElapsed(); |
| 61 | return (!empty($elapsedTime)) ? |
| 62 | 'A new ' . (int)$elapsedTime . ' second event has been triggered.' : |
| 63 | 'A new timed event has been triggered.'; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Trigger handler logging |
| 68 | * |
| 69 | * @throws Exception |
| 70 | * @return void |
| 71 | */ |
| 72 | public function log(): void |
| 73 | { |
| 74 | $logLevel = $this->resolveLogLevel(); |
| 75 | if ($logLevel === null) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | $timeLimit = $this->loggingParams['limit'] ?? null; |
| 80 | $elapsedTime = $this->getElapsed(); |
| 81 | $context = $this->prepare(); |
| 82 | |
| 83 | if ($timeLimit !== null) { |
| 84 | if ($elapsedTime >= $timeLimit) { |
| 85 | $context['time_limit'] = $timeLimit; |
| 86 | $this->logger->log( |
| 87 | $logLevel, 'The time limit of '. $timeLimit . ' second(s) has been exceeded by ' . |
| 88 | (int)($elapsedTime - $timeLimit) . ' second(s). The timed event was a total of ' . |
| 89 | $elapsedTime . ' second(s).', $context |
| 90 | ); |
| 91 | } |
| 92 | } else { |
| 93 | $this->logger->log($logLevel, $this->prepareMessage(), $context); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | } |