Completed
Push — master ( 7128b2...2441a8 )
by Marcel
02:59
created

Report::createForMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Facade\FlareClient;
4
5
use Throwable;
6
use Facade\FlareClient\Glows\Glow;
7
use Facade\IgnitionContracts\Solution;
8
use Facade\FlareClient\Concerns\UsesTime;
9
use Facade\FlareClient\Concerns\HasContext;
10
use Facade\FlareClient\Stacktrace\Stacktrace;
11
use Facade\FlareClient\Context\ContextInterface;
12
use Facade\FlareClient\Solutions\ReportSolution;
13
use Facade\FlareClient\Contracts\ProvidesFlareContext;
14
15
class Report
16
{
17
    use UsesTime, HasContext;
18
19
    /** @var \Facade\FlareClient\Stacktrace\Stacktrace */
20
    private $stacktrace;
21
22
    /** @var string */
23
    private $exceptionClass;
24
25
    /** @var string */
26
    private $message;
27
28
    /** @var array */
29
    private $glows = [];
30
31
    /** @var array */
32
    private $solutions = [];
33
34
    /** @var ContextInterface */
35
    private $context;
36
37
    /** @var string */
38
    private $applicationPath;
39
40
    /** @var array */
41
    private $userProvidedContext = [];
42
43
    /** @var array */
44
    private $exceptionContext = [];
45
46
    /** @var Throwable */
47
    private $throwable;
48
49
    /** @var string */
50
    private $notifierName;
51
52
    /** @var string */
53
    private $languageVersion;
54
55
    /** @var string */
56
    private $frameworkVersion;
57
58
    /** @var int */
59
    private $openFrameIndex;
60
61
    public static function createForThrowable(Throwable $throwable, ContextInterface $context, ?string $applicationPath = null): self
62
    {
63
        return (new static())
64
            ->setApplicationPath($applicationPath)
65
            ->throwable($throwable)
66
            ->useContext($context)
67
            ->exceptionClass(get_class($throwable))
68
            ->message($throwable->getMessage())
69
            ->stackTrace(Stacktrace::createForThrowable($throwable, $applicationPath))
70
            ->exceptionContext($throwable);
71
    }
72
73
    public static function createForMessage(string $message, string $logLevel, ContextInterface $context, ?string $applicationPath = null): self
74
    {
75
        $stacktrace = Stacktrace::create($applicationPath);
76
77
        return (new static())
78
            ->setApplicationPath($applicationPath)
79
            ->message($message)
80
            ->useContext($context)
81
            ->exceptionClass($logLevel)
82
            ->stacktrace($stacktrace)
83
            ->openFrameIndex($stacktrace->firstApplicationFrameIndex());
84
    }
85
86
    public function exceptionClass(string $exceptionClass)
87
    {
88
        $this->exceptionClass = $exceptionClass;
89
90
        return $this;
91
    }
92
93
    public function getExceptionClass(): string
94
    {
95
        return $this->exceptionClass;
96
    }
97
98
    public function throwable(Throwable $throwable)
99
    {
100
        $this->throwable = $throwable;
101
102
        return $this;
103
    }
104
105
    public function getThrowable(): ?Throwable
106
    {
107
        return $this->throwable;
108
    }
109
110
    public function message(string $message)
111
    {
112
        $this->message = $message;
113
114
        return $this;
115
    }
116
117
    public function getMessage(): string
118
    {
119
        return $this->message;
120
    }
121
122
    public function stacktrace(Stacktrace $stacktrace)
123
    {
124
        $this->stacktrace = $stacktrace;
125
126
        return $this;
127
    }
128
129
    public function getStacktrace(): Stacktrace
130
    {
131
        return $this->stacktrace;
132
    }
133
134
    public function notifierName(string $notifierName)
135
    {
136
        $this->notifierName = $notifierName;
137
138
        return $this;
139
    }
140
141
    public function languageVersion(string $languageVersion)
142
    {
143
        $this->languageVersion = $languageVersion;
144
145
        return $this;
146
    }
147
148
    public function frameworkVersion(string $frameworkVersion)
149
    {
150
        $this->frameworkVersion = $frameworkVersion;
151
152
        return $this;
153
    }
154
155
    public function useContext(ContextInterface $request)
156
    {
157
        $this->context = $request;
158
159
        return $this;
160
    }
161
162
    public function openFrameIndex(?int $index)
163
    {
164
        $this->openFrameIndex = $index;
165
166
        return $this;
167
    }
168
169
    public function setApplicationPath(?string $applicationPath)
170
    {
171
        $this->applicationPath = $applicationPath;
172
173
        return $this;
174
    }
175
176
    public function getApplicationPath(): ?string
177
    {
178
        return $this->applicationPath;
179
    }
180
181
    public function view(?View $view)
182
    {
183
        $this->view = $view;
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
184
185
        return $this;
186
    }
187
188
    public function addGlow(Glow $glow)
189
    {
190
        $this->glows[] = $glow->toArray();
191
192
        return $this;
193
    }
194
195
    public function addSolution(Solution $solution)
196
    {
197
        $this->solutions[] = ReportSolution::fromSolution($solution)->toArray();
198
199
        return $this;
200
    }
201
202
    public function userProvidedContext(array $userProvidedContext)
203
    {
204
        $this->userProvidedContext = $userProvidedContext;
205
206
        return $this;
207
    }
208
209
    public function allContext(): array
210
    {
211
        $context = $this->context->toArray();
212
213
        $context = array_merge_recursive_distinct($context, $this->exceptionContext);
214
215
        return array_merge_recursive_distinct($context, $this->userProvidedContext);
216
    }
217
218
    private function exceptionContext(Throwable $throwable)
219
    {
220
        if ($throwable instanceof ProvidesFlareContext) {
221
            $this->exceptionContext = $throwable->context();
222
        }
223
224
        return $this;
225
    }
226
227
    public function toArray()
228
    {
229
        return [
230
            'notifier' => $this->notifierName ?? 'Flare Client',
231
            'language' => 'PHP',
232
            'framework_version' => $this->frameworkVersion,
233
            'language_version' => $this->languageVersion ?? phpversion(),
234
            'exception_class' => $this->exceptionClass,
235
            'seen_at' => $this->getCurrentTime(),
236
            'message' => $this->message,
237
            'glows' => $this->glows,
238
            'solutions' => $this->solutions,
239
            'stacktrace' => $this->stacktrace->toArray(),
240
            'context' => $this->allContext(),
241
            'stage' => $this->stage,
242
            'message_level' => $this->messageLevel,
243
            'open_frame_index' => $this->openFrameIndex,
244
            'application_path' => $this->applicationPath,
245
        ];
246
    }
247
}
248