Error::getTrace()   A
last analyzed

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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Monolog;
4
5
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface;
6
7
/**
8
 * A custom error class to collect all the data we need.
9
 *
10
 * @author Jelmer Prins <[email protected]>
11
 */
12
class Error implements ErrorInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $message;
18
19
    /**
20
     * @var int
21
     */
22
    private $line;
23
24
    /**
25
     * @var string
26
     */
27
    private $file;
28
29
    /**
30
     * @var array
31
     */
32
    private $trace;
33
34
    /**
35
     * @var array
36
     */
37
    private $parameters = [];
38
39
    /**
40
     * Create a new error wrapping the given error context info.
41
     *
42
     * @param string $message
43
     * @param int $line
44
     * @param string $file
45
     * @param array|null $trace
46
     * @param array|null $parameters
47
     */
48 17
    public function __construct($message, $line, $file, array $trace = null, array $parameters = null)
49
    {
50 17
        $this->setMessage($message);
51 17
        $this->setLine($line);
52 17
        $this->setFile($file);
53 17
        $this->setTrace($trace);
54 17
        $this->setParameters($parameters);
55 17
    }
56
57
    /**
58
     * @param string $file
59
     */
60 17
    private function setFile($file)
61
    {
62 17
        $this->file = $file;
63 17
    }
64
65
    /**
66
     * @return string
67
     */
68 16
    public function getFile()
69
    {
70 16
        return $this->file;
71
    }
72
73
    /**
74
     * @param int $line
75
     */
76 17
    private function setLine($line)
77
    {
78 17
        $this->line = $line;
79 17
    }
80
81
    /**
82
     * @return int
83
     */
84 16
    public function getLine()
85
    {
86 16
        return $this->line;
87
    }
88
89
    /**
90
     * @param string $message
91
     */
92 17
    private function setMessage($message)
93
    {
94 17
        $this->message = $message;
95 17
    }
96
97
    /**
98
     * @return string
99
     */
100 7
    public function getMessage()
101
    {
102 7
        return $this->message;
103
    }
104
105
    /**
106
     * @param array|null $parameters
107
     */
108 17
    private function setParameters(array $parameters = null)
109
    {
110 17
        $this->parameters = (array) $parameters;
111
112
        // make sure we have at least the POST, GET, COOKIE and SESSION data
113 17
        $this->addParameterFallback('_POST', $_POST);
114 17
        $this->addParameterFallback('_GET', $_GET);
115 17
        $this->addParameterFallback('_COOKIE', $_COOKIE);
116 17
        if (isset($_SESSION)) {
117 7
            $this->addParameterFallback('_SESSION', $_SESSION);
118 7
        }
119 17
    }
120
121
    /**
122
     * This will add fallback data to the parameters if the key is not set.
123
     *
124
     * @param string $name
125
     * @param mixed $fallbackData
126
     */
127 17
    private function addParameterFallback($name, $fallbackData = null)
128
    {
129 17
        if (!isset($this->parameters[$name]) && !empty($fallbackData)) {
130 10
            $this->parameters[$name] = $fallbackData;
131 10
        }
132 17
    }
133
134
    /**
135
     * @return array
136
     */
137 11
    public function getParameters()
138
    {
139 11
        return $this->parameters;
140
    }
141
142
    /**
143
     * @param array|null $trace
144
     */
145 17
    private function setTrace(array $trace = null)
146
    {
147 17
        if ($trace === null) {
148
            $trace = [
149
                [
150 10
                    'line' => $this->getLine(),
151 10
                    'file' => $this->getFile(),
152 10
                    'function' => 'unknown',
153 10
                ],
154 10
            ];
155 10
        }
156 17
        $this->trace = $trace;
157 17
    }
158
159
    /**
160
     * @return array
161
     */
162 8
    public function getTrace()
163
    {
164 8
        return $this->trace;
165
    }
166
}
167