Completed
Push — master ( 18b86f...ca8bd4 )
by Marcel
01:44
created

ErrorPageViewModel::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Facade\Ignition\ErrorPage;
4
5
use Closure;
6
use Exception;
7
use Throwable;
8
use Facade\Ignition\Ignition;
9
use Facade\FlareClient\Report;
10
use Laravel\Telescope\Telescope;
11
use Facade\Ignition\IgnitionConfig;
12
use Illuminate\Contracts\Support\Arrayable;
13
use Laravel\Telescope\IncomingExceptionEntry;
14
use Facade\Ignition\Solutions\SolutionTransformer;
15
use Laravel\Telescope\Http\Controllers\HomeController;
16
17
class ErrorPageViewModel implements Arrayable
18
{
19
    /** @var \Throwable */
20
    protected $throwable;
21
22
    /** @var array */
23
    protected $solutions;
24
25
    /** @var \Facade\Ignition\IgnitionConfig */
26
    protected $ignitionConfig;
27
28
    /** @var \Facade\FlareClient\Report */
29
    protected $report;
30
31
    public function __construct(Throwable $throwable, IgnitionConfig $ignitionConfig, Report $report, array $solutions)
32
    {
33
        $this->throwable = $throwable;
34
35
        $this->ignitionConfig = $ignitionConfig;
36
37
        $this->report = $report;
38
39
        $this->solutions = $solutions;
40
    }
41
42
    public function throwableString(): string
43
    {
44
        return sprintf(
45
            "%s: %s in file %s on line %d\n\n%s\n",
46
            get_class($this->throwable),
47
            $this->throwable->getMessage(),
48
            $this->throwable->getFile(),
49
            $this->throwable->getLine(),
50
            $this->report->getThrowable()->getTraceAsString()
51
        );
52
    }
53
54
    public function telescopeUrl(): ?string
55
    {
56
        try {
57
            if (! class_exists(Telescope::class)) {
58
                return null;
59
            }
60
61
            if (! count(Telescope::$entriesQueue)) {
62
                return null;
63
            }
64
65
            $telescopeEntry = collect(Telescope::$entriesQueue)->first(function ($entry) {
66
                return $entry instanceof IncomingExceptionEntry;
0 ignored issues
show
Bug introduced by
The class Laravel\Telescope\IncomingExceptionEntry does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
            });
68
69
            if (is_null($telescopeEntry)) {
70
                return null;
71
            }
72
73
            $telescopeEntryId = (string) $telescopeEntry->uuid;
74
75
            return url(action([HomeController::class, 'index'])."/exceptions/{$telescopeEntryId}");
76
        } catch (Exception $exception) {
77
            return null;
78
        }
79
    }
80
81
    public function title(): string
82
    {
83
        return "🧨 {$this->report->getMessage()}";
84
    }
85
86
    public function config(): array
87
    {
88
        return $this->ignitionConfig->toArray();
89
    }
90
91
    public function solutions(): array
92
    {
93
        $solutions = [];
94
95
        foreach ($this->solutions as $solution) {
96
            $solutions[] = (new SolutionTransformer($solution))->toArray();
97
        }
98
99
        return $solutions;
100
    }
101
102
    protected function shareEndpoint()
103
    {
104
        // use string notation as L5.5 and L5.6 don't support array notation yet
105
        return action('\Facade\Ignition\Http\Controllers\ShareReportController');
106
    }
107
108
    public function report(): array
109
    {
110
        return $this->report->toArray();
111
    }
112
113
    public function jsonEncode($data): string
114
    {
115
        $jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
116
117
        if (version_compare(phpversion(), '7.2', '>=')) {
118
            return json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | $jsonOptions);
119
        }
120
121
        return json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | $jsonOptions);
122
    }
123
124
    public function getAssetContents(string $asset): string
125
    {
126
        $assetPath = __DIR__."/../../resources/compiled/{$asset}";
127
128
        return file_get_contents($assetPath);
129
    }
130
131
    public function styles(): array
132
    {
133
        return array_keys(Ignition::styles());
134
    }
135
136
    public function scripts(): array
137
    {
138
        return array_keys(Ignition::scripts());
139
    }
140
141
    public function tabs(): string
142
    {
143
        return json_encode(Ignition::$tabs);
144
    }
145
146
    public function toArray(): array
147
    {
148
        return [
149
            'throwableString' => $this->throwableString(),
150
            'telescopeUrl' => $this->telescopeUrl(),
151
            'shareEndpoint' => $this->shareEndpoint(),
152
            'title' => $this->title(),
153
            'config' => $this->config(),
154
            'solutions' => $this->solutions(),
155
            'report' => $this->report(),
156
            'housekeepingEndpoint' => url(config('ignition.housekeeping_endpoint_prefix', '_ignition')),
157
            'styles' => $this->styles(),
158
            'scripts' => $this->scripts(),
159
            'tabs' => $this->tabs(),
160
            'jsonEncode' => Closure::fromCallable([$this, 'jsonEncode']),
161
            'getAssetContents' => Closure::fromCallable([$this, 'getAssetContents']),
162
        ];
163
    }
164
}
165