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