Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
27 / 27 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
MessageHandler | |
100.00% |
27 / 27 |
|
100.00% |
7 / 7 |
17 | |
100.00% |
1 / 1 |
addMessage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasMessages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMessages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
prepare | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
prepareHeaderAsString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
prepareAsString | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
log | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
9 |
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-2024 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 | /** |
17 | * Debug message handler class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Debug |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 2.2.0 |
25 | */ |
26 | class MessageHandler extends AbstractHandler |
27 | { |
28 | |
29 | /** |
30 | * Messages |
31 | * @var array |
32 | */ |
33 | protected array $messages = []; |
34 | |
35 | /** |
36 | * Add message |
37 | * |
38 | * @param string $message |
39 | * @return MessageHandler |
40 | */ |
41 | public function addMessage(string $message): MessageHandler |
42 | { |
43 | $this->messages[] = ['message' => $message, 'timestamp' => (string)microtime(true)]; |
44 | return $this; |
45 | } |
46 | |
47 | /** |
48 | * Determine if the handler has messages |
49 | * |
50 | * @return bool |
51 | */ |
52 | public function hasMessages(): bool |
53 | { |
54 | return (count($this->messages) > 0); |
55 | } |
56 | |
57 | /** |
58 | * Get messages |
59 | * |
60 | * @return array |
61 | */ |
62 | public function getMessages(): array |
63 | { |
64 | return $this->messages; |
65 | } |
66 | |
67 | /** |
68 | * Prepare handler data for storage |
69 | * |
70 | * @return array |
71 | */ |
72 | public function prepare(): array |
73 | { |
74 | return $this->messages; |
75 | } |
76 | |
77 | /** |
78 | * Prepare header string |
79 | * |
80 | * @return string |
81 | */ |
82 | public function prepareHeaderAsString(): string |
83 | { |
84 | $string = ((!empty($this->name)) ? $this->name . ' ' : '') . 'Message Handler'; |
85 | $string .= PHP_EOL . str_repeat('=', strlen($string)) . PHP_EOL; |
86 | |
87 | return $string; |
88 | } |
89 | |
90 | /** |
91 | * Prepare handler data as string |
92 | * |
93 | * @return string |
94 | */ |
95 | public function prepareAsString(): string |
96 | { |
97 | $string = ''; |
98 | foreach ($this->messages as $message) { |
99 | $string .= number_format($message['timestamp'], 5, '.', '') . "\t" . $message['message'] . PHP_EOL; |
100 | } |
101 | $string .= PHP_EOL; |
102 | |
103 | return $string; |
104 | } |
105 | |
106 | /** |
107 | * Trigger handler logging |
108 | * |
109 | * @throws Exception |
110 | * @return void |
111 | */ |
112 | public function log(): void |
113 | { |
114 | if (($this->hasLogger()) && ($this->hasLoggingParams())) { |
115 | $logLevel = $this->loggingParams['level'] ?? null; |
116 | $useContext = $this->loggingParams['context'] ?? null; |
117 | |
118 | if ($logLevel !== null) { |
119 | $context = []; |
120 | $message = (count($this->messages) > 1) ? |
121 | '(' . count($this->messages) . ') new messages have been triggered.' : |
122 | 'A new message has been triggered: ' . $this->messages[0]['message']; |
123 | |
124 | $context['messages'] = (($useContext !== null) && (($useContext !== null) && (strtolower($useContext) == 'text'))) ? |
125 | $this->prepareAsString() : $this->prepare(); |
126 | |
127 | if (is_string($useContext)) { |
128 | $context['format'] = $useContext; |
129 | } |
130 | |
131 | $this->logger->log($logLevel, $message, $context); |
132 | } else { |
133 | throw new Exception('Error: The log level parameter was not set.'); |
134 | } |
135 | } |
136 | } |
137 | |
138 | } |