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
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%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 prepareHeaderAsString
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 prepareAsString
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
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-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://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@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    1.3.0
25 */
26class MessageHandler extends AbstractHandler
27{
28
29    /**
30     * Messages
31     * @var array
32     */
33    protected $messages = [];
34
35    /**
36     * Add message
37     *
38     * @param  string $message
39     * @return self
40     */
41    public function addMessage($message)
42    {
43        $this->messages[(string)microtime(true)] = $message;
44        return $this;
45    }
46
47    /**
48     * Determine if the handler has messages
49     *
50     * @return boolean
51     */
52    public function hasMessages()
53    {
54        return (count($this->messages) > 0);
55    }
56
57    /**
58     * Get messages
59     *
60     * @return array
61     */
62    public function getMessages()
63    {
64        return $this->messages;
65    }
66
67    /**
68     * Prepare handler data for storage
69     *
70     * @return array
71     */
72    public function prepare()
73    {
74        $data = [];
75
76        foreach ($this->messages as $time => $message) {
77            $data[number_format($time, 5, '.', '')] = $message;
78        }
79
80        return $data;
81    }
82
83    /**
84     * Prepare header string
85     *
86     * @return string
87     */
88    public function prepareHeaderAsString()
89    {
90        $string  = ((!empty($this->name)) ? $this->name . ' ' : '') . 'Message Handler';
91        $string .= PHP_EOL . str_repeat('=', strlen($string)) . PHP_EOL;
92
93        return $string;
94    }
95
96    /**
97     * Prepare handler data as string
98     *
99     * @return string
100     */
101    public function prepareAsString()
102    {
103        $string = '';
104        foreach ($this->messages as $time => $message) {
105            $string .= number_format($time, 5, '.', '') . "\t" . $message . PHP_EOL;
106        }
107        $string .= PHP_EOL;
108
109        return $string;
110    }
111
112}