Completed
Push — master ( 8dce14...ef59ee )
by Freek
13:50
created

ErrorPageViewModel::defaultTab()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 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|null */
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
    /** @var string */
32
    protected $defaultTab;
33
34
    /** @var array */
35
    protected $defaultTabProps = [];
36
37
    public function __construct(?Throwable $throwable, IgnitionConfig $ignitionConfig, Report $report, array $solutions)
38
    {
39
        $this->throwable = $throwable;
40
41
        $this->ignitionConfig = $ignitionConfig;
42
43
        $this->report = $report;
44
45
        $this->solutions = $solutions;
46
    }
47
48
    public function throwableString(): string
49
    {
50
        if (! $this->throwable) {
51
            return '';
52
        }
53
54
        return sprintf(
55
            "%s: %s in file %s on line %d\n\n%s\n",
56
            get_class($this->throwable),
57
            $this->throwable->getMessage(),
58
            $this->throwable->getFile(),
59
            $this->throwable->getLine(),
60
            $this->report->getThrowable()->getTraceAsString()
61
        );
62
    }
63
64
    public function telescopeUrl(): ?string
65
    {
66
        try {
67
            if (! class_exists(Telescope::class)) {
68
                return null;
69
            }
70
71
            if (! count(Telescope::$entriesQueue)) {
72
                return null;
73
            }
74
75
            $telescopeEntry = collect(Telescope::$entriesQueue)->first(function ($entry) {
76
                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...
77
            });
78
79
            if (is_null($telescopeEntry)) {
80
                return null;
81
            }
82
83
            $telescopeEntryId = (string) $telescopeEntry->uuid;
84
85
            return url(action([HomeController::class, 'index'])."/exceptions/{$telescopeEntryId}");
86
        } catch (Exception $exception) {
87
            return null;
88
        }
89
    }
90
91
    public function title(): string
92
    {
93
        return "🧨 {$this->report->getMessage()}";
94
    }
95
96
    public function config(): array
97
    {
98
        return $this->ignitionConfig->toArray();
99
    }
100
101
    public function solutions(): array
102
    {
103
        $solutions = [];
104
105
        foreach ($this->solutions as $solution) {
106
            $solutions[] = (new SolutionTransformer($solution))->toArray();
107
        }
108
109
        return $solutions;
110
    }
111
112
    protected function shareEndpoint()
113
    {
114
        // use string notation as L5.5 and L5.6 don't support array notation yet
115
        return action('\Facade\Ignition\Http\Controllers\ShareReportController');
116
    }
117
118
    public function report(): array
119
    {
120
        return $this->report->toArray();
121
    }
122
123
    public function jsonEncode($data): string
124
    {
125
        $jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
126
127
        if (version_compare(phpversion(), '7.2', '>=')) {
128
            return json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | $jsonOptions);
129
        }
130
131
        return json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | $jsonOptions);
132
    }
133
134
    public function getAssetContents(string $asset): string
135
    {
136
        $assetPath = __DIR__."/../../resources/compiled/{$asset}";
137
138
        return file_get_contents($assetPath);
139
    }
140
141
    public function styles(): array
142
    {
143
        return array_keys(Ignition::styles());
144
    }
145
146
    public function scripts(): array
147
    {
148
        return array_keys(Ignition::scripts());
149
    }
150
151
    public function tabs(): string
152
    {
153
        return json_encode(Ignition::$tabs);
154
    }
155
156
    public function defaultTab(?string $defaultTab, ?array $defaultTabProps)
157
    {
158
        $this->defaultTab = $defaultTab ?? 'StackTab';
159
160
        if ($defaultTabProps) {
161
            $this->defaultTabProps = $defaultTabProps;
162
        }
163
    }
164
165
    public function toArray(): array
166
    {
167
        return [
168
            'throwableString' => $this->throwableString(),
169
            'telescopeUrl' => $this->telescopeUrl(),
170
            'shareEndpoint' => $this->shareEndpoint(),
171
            'title' => $this->title(),
172
            'config' => $this->config(),
173
            'solutions' => $this->solutions(),
174
            'report' => $this->report(),
175
            'housekeepingEndpoint' => url(config('ignition.housekeeping_endpoint_prefix', '_ignition')),
176
            'styles' => $this->styles(),
177
            'scripts' => $this->scripts(),
178
            'tabs' => $this->tabs(),
179
            'jsonEncode' => Closure::fromCallable([$this, 'jsonEncode']),
180
            'getAssetContents' => Closure::fromCallable([$this, 'getAssetContents']),
181
            'defaultTab' => $this->defaultTab,
182
            'defaultTabProps' => $this->defaultTabProps,
183
        ];
184
    }
185
}
186