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