Passed
Push — master ( abeea0...8d7325 )
by Alec
01:28
created

DoesProcessException::dumpExceptionClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * User: alec
4
 * Date: 28.12.18
5
 * Time: 15:21
6
 */
7
8
namespace AlecRabbit\Traits;
9
10
trait DoesProcessException
11
{
12
    /** @var bool */
13
    protected $throwOnError = true;
14
15
    /** @var bool */
16
    protected $dumpTrace = true;
17
18
    /** @var bool */
19
    protected $doDumpException = false;
20
21
    /** @return self */
22 1
    public function doNotThrowOnError(): self
23
    {
24 1
        $this->throwOnError = false;
25 1
        return $this;
26
    }
27
28
    /** @return self */
29 1
    public function throwOnError(): self
30
    {
31 1
        $this->throwOnError = true;
32 1
        return $this;
33
    }
34
35
    /** @return bool */
36 2
    public function doesThrowsOnError(): bool
37
    {
38 2
        return $this->throwOnError;
39
    }
40
41
    /**
42
     * @param bool $dumpTrace
43
     * @return self
44
     */
45 2
    public function setDumpTrace(bool $dumpTrace): self
46
    {
47 2
        $this->dumpTrace = $dumpTrace;
48 2
        return $this;
49
    }
50
51
    /**
52
     * @param \Throwable $e
53
     * @throws \Throwable
54
     */
55 2
    protected function processException(\Throwable $e): void
56
    {
57 2
        $this->dumpException($e);
58 2
        if ($this->throwOnError) {
59 1
            throw $e;
60
        }
61 1
    }
62
63
    /**
64
     * @param \Throwable $e
65
     */
66 2
    protected function dumpException(\Throwable $e): void
67
    {
68 2
        if (\defined('APP_DEBUG') && APP_DEBUG) {
69 2
            $this->checkEnv();
70 2
            $this->dumpExceptionClass($e);
71 2
            $this->dumpExceptionMessage($e);
72 2
            if ($this->doesDumpTrace()) {
73 1
                $this->dumpExceptionTrace($e);
74
            }
75 2
            $this->dumpExceptionObject($e);
76
        }
77 2
    }
78
79 2
    protected function checkEnv(): void
80
    {
81 2
        if (\defined('DEBUG_DUMP_EXCEPTION') && DEBUG_DUMP_EXCEPTION) {
82 2
            $this->doDumpException = true;
83
        }
84 2
    }
85
86
        /**
87
     * @param \Throwable $e
88
     */
89 2
    protected function dumpExceptionClass(\Throwable $e): void
90
    {
91 2
        if ($this->doDumpException) {
92 2
            $this->dump('[' . \get_class($e) . ']');
93
        }
94 2
    }
95
96
    /**
97
     * @param mixed ...$that
98
     */
99
    protected function dump(...$that): void // @codeCoverageIgnoreStart
100
    {
101
        if (\function_exists('dump')) {
102
            dump(...$that);
103
        } else {
104
            var_dump(...$that);
105
        }
106
    } // @codeCoverageIgnoreEnd
107
108
    /**
109
     * @param \Throwable $e
110
     */
111 2
    protected function dumpExceptionMessage(\Throwable $e): void
112
    {
113 2
        if ($this->doDumpException) {
114 2
            $this->dump($e->getMessage());
115
        }
116 2
    }
117
118
    /**
119
     * @return bool
120
     */
121 2
    public function doesDumpTrace(): bool
122
    {
123 2
        return $this->dumpTrace;
124
    }
125
126
    /**
127
     * @param \Throwable $e
128
     */
129 1
    protected function dumpExceptionTrace(\Throwable $e): void
130
    {
131 1
        if ($this->doDumpException) {
132 1
            $this->dump($e->getTraceAsString());
133
        }
134 1
    }
135
136
    /**
137
     * @param \Throwable $e
138
     */
139 2
    protected function dumpExceptionObject(\Throwable $e): void
140
    {
141 2
        if (\defined('DEBUG_DUMP_EXCEPTION_OBJECT') && DEBUG_DUMP_EXCEPTION_OBJECT) {
142 2
            $this->dump($e);
143
        }
144 2
    }
145
}
146