Passed
Push — master ( 93501e...841558 )
by Dmitry
10:30
created

CheckException::getNestedDebug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace TonicHealthCheck\Check;
4
5
use Exception;
6
7
/**
8
 * Class CheckException.
9
 */
10
class CheckException extends \Exception
11
{
12
    const EXCEPTION_NAME = 'healthCheck';
13
14
    /**
15
     * HttpCheckException constructor.
16
     *
17
     * @param string          $message
18
     * @param null|int        $code
19
     * @param null|\Exception $previous
20
     */
21 3
    public function __construct($message = '', $code = null, \Exception $previous = null)
22
    {
23 3
        if (0 === $code || null === $code) {
24 2
            $code = -1;
25 2
        }
26 3
        parent::__construct(static::EXCEPTION_NAME.': '.$message, $code, $previous);
27 3
    }
28
29
    /**
30
     * @param int $depth
31
     *
32
     * @return string
33
     */
34 1
    public function getNestedDebug($depth = -1)
35
    {
36 1
        return static::getDebug($this, $depth);
37
    }
38
39
    /**
40
     * @param Exception $exception
41
     *
42
     * @return string
43
     */
44 1
    protected static function getDebug(Exception $exception, $depth = -1)
45
    {
46 1
        $errorDebugMsg = sprintf(
47 1
            "ERROR CLASS:%s\nERROR CODE:%d\nERROR FILE:%s\nERROR MESSAGE:%s\nERROR TRACE:\n%s",
48 1
            get_class($exception),
49 1
            $exception->getCode(),
50 1
            $exception->getFile(),
51 1
            $exception->getMessage(),
52 1
            $exception->getTraceAsString()
53 1
        );
54
55 1
        if (($depth == -1 || $depth > 0) && $exception->getPrevious() instanceof Exception) {
56 1
            $depth -= $depth > 0 ? 1 : 0;
57 1
            $errorDebugMsg = sprintf(
58 1
                "%s\n\nPRIVIOUS EXCEPTION:\n%s",
59 1
                $errorDebugMsg,
60 1
                static::getDebug($exception->getPrevious(), $depth)
61 1
            );
62 1
        }
63
64 1
        return $errorDebugMsg;
65
    }
66
}
67