Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
ExceptionHandler
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
9 / 9
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setVerbose
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isVerbose
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addException
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasExceptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExceptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prepare
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
 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 <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Debug\Handler;
16
17use Psr\Log\LoggerInterface;
18
19/**
20 * Debug exception handler class
21 *
22 * @category   Pop
23 * @package    Pop\Debug
24 * @author     Nick Sagona, III <dev@noladev.com>
25 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    4.0.0
28 */
29class ExceptionHandler extends AbstractHandler
30{
31
32    /**
33     * Verbose flag
34     * @var bool
35     */
36    protected bool $verbose = false;
37
38    /**
39     * Constructor
40     *
41     * Instantiate a handler object
42     *
43     * @param bool             $verbose
44     * @param ?string          $name
45     * @param ?LoggerInterface $logger
46     * @param array            $loggingParams
47     */
48    public function __construct(bool $verbose = false, ?string $name = null, ?LoggerInterface $logger = null, array $loggingParams = [])
49    {
50        parent::__construct($name, $logger, $loggingParams);
51        $this->setVerbose($verbose);
52    }
53
54    /**
55     * Set verbose
56     *
57     * @param  bool $verbose
58     * @return ExceptionHandler
59     */
60    public function setVerbose(bool $verbose = true): ExceptionHandler
61    {
62        $this->verbose = $verbose;
63        return $this;
64    }
65
66    /**
67     * Is verbose
68     *
69     * @return bool
70     */
71    public function isVerbose(): bool
72    {
73        return $this->verbose;
74    }
75
76
77    /**
78     * Add exception
79     *
80     * @param  \Exception $exception
81     * @return ExceptionHandler
82     */
83    public function addException(\Exception $exception): ExceptionHandler
84    {
85        $this->data[] = ['exception' => $exception, 'timestamp' => (string)microtime(true)];
86        return $this;
87    }
88
89    /**
90     * Determine if the handler has exceptions
91     *
92     * @return bool
93     */
94    public function hasExceptions(): bool
95    {
96        return (count($this->data) > 0);
97    }
98
99    /**
100     * Get exceptions
101     *
102     * @return array
103     */
104    public function getExceptions(): array
105    {
106        return $this->data;
107    }
108
109    /**
110     * Prepare handler data for storage
111     *
112     * @return array
113     */
114    public function prepare(): array
115    {
116        if ($this->isVerbose()) {
117            $exceptions = [];
118            foreach ($this->data as $exception) {
119                $exceptions[] = [
120                    'exception' => get_class($exception['exception']),
121                    'code'      => $exception['exception']->getCode(),
122                    'line'      => $exception['exception']->getLine(),
123                    'file'      => $exception['exception']->getFile(),
124                    'message'   => $exception['exception']->getMessage(),
125                    'trace'     => $exception['exception']->getTrace(),
126                    'timestamp' => $exception['timestamp'],
127                ];
128            }
129            return $exceptions;
130        } else {
131            return $this->data;
132        }
133    }
134
135    /**
136     * Prepare handler message
137     *
138     * @param  ?array $context
139     * @return string
140     */
141    public function prepareMessage(?array $context = null): string
142    {
143        if ($context === null) {
144            $context = $this->prepare();
145        }
146
147        return (count($context) > 1) ?
148            '(' . count($context) . ') exceptions have been thrown.' :
149            '(1) exception has been thrown.';
150    }
151
152    /**
153     * Trigger handler logging
154     *
155     * @throws Exception
156     * @return void
157     */
158    public function log(): void
159    {
160        $logLevel = $this->resolveLogLevel();
161        if ($logLevel === null) {
162            return;
163        }
164
165        $context = $this->prepare();
166        $this->logger->log($logLevel, $this->prepareMessage($context), $context);
167    }
168
169}