Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
MessageHandler
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
11
100.00% covered (success)
100.00%
1 / 1
 addMessage
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasMessages
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessages
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prepare
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prepareMessage
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 log
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
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 */
14namespace Pop\Debug\Handler;
15
16/**
17 * Debug message handler class
18 *
19 * @category   Pop
20 * @package    Pop\Debug
21 * @author     Nick Sagona, III <dev@noladev.com>
22 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
23 * @license    https://www.popphp.org/license     New BSD License
24 * @version    3.0.0
25 */
26class MessageHandler extends AbstractHandler
27{
28
29    /**
30     * Add message
31     *
32     * @param  string $message
33     * @return MessageHandler
34     */
35    public function addMessage(string $message): MessageHandler
36    {
37        $this->data[] = ['message' => $message, 'timestamp' => (string)microtime(true)];
38        return $this;
39    }
40
41    /**
42     * Determine if the handler has messages
43     *
44     * @return bool
45     */
46    public function hasMessages(): bool
47    {
48        return (count($this->data) > 0);
49    }
50
51    /**
52     * Get messages
53     *
54     * @return array
55     */
56    public function getMessages(): array
57    {
58        return $this->data;
59    }
60
61    /**
62     * Prepare handler data for storage
63     *
64     * @return array
65     */
66    public function prepare(): array
67    {
68        return $this->data;
69    }
70
71    /**
72     * Prepare handler message
73     *
74     * @param  ?array $context
75     * @return string
76     */
77    public function prepareMessage(?array $context = null): string
78    {
79        if ($context === null) {
80            $context = $this->prepare();
81        }
82
83        return (count($context) > 1) ?
84            '(' . count($context) . ') new messages have been logged.' :
85            'A new message has been logged.';
86    }
87
88    /**
89     * Trigger handler logging
90     *
91     * @throws Exception
92     * @return void
93     */
94    public function log(): void
95    {
96        if (($this->hasLogger()) && ($this->hasLoggingParams())) {
97            $logLevel = $this->loggingParams['level'] ?? null;
98
99            if ($logLevel !== null) {
100                $context = $this->prepare();
101                $this->logger->log($logLevel, $this->prepareMessage($context), $context);
102            } else {
103                throw new Exception('Error: The log level parameter was not set.');
104            }
105        }
106    }
107
108}