ShareReportAction.php
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace Facade\Ignition\Actions;
use Exception;
use Facade\FlareClient\Http\Client;
use Facade\FlareClient\Truncation\ReportTrimmer;
use Facade\Ignition\Exceptions\UnableToShareErrorException;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class ShareReportAction
{
/** @var array */
protected $tabs;
/** @var \Facade\FlareClient\Http\Client */
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function handle(array $report, array $tabs, ?string $lineSelection = null)
{
$this->tabs = $tabs;
$report = $this->filterReport($report);
try {
return $this->client->post('public-reports', [
'report' => $this->trimReport($report),
'tabs' => $tabs,
'lineSelection' => $lineSelection,
]);
} catch (Exception $exception) {
throw new UnableToShareErrorException($exception->getMessage());
}
}
public function filterReport(array $report): array
{
if (! $this->hasTab('stackTraceTab')) {
$report['stacktrace'] = array_slice($report['stacktrace'], 0, 1);
}
if (! $this->hasTab('debugTab')) {
$report['glows'] = [];
}
$report['context'] = $this->filterContextItems($report['context']);
return $report;
}
protected function hasTab(string $tab): bool
{
return in_array($tab, $this->tabs);
}
protected function filterContextItems(array $contextItems): array
{
if (! $this->hasTab('requestTab')) {
$contextItems = $this->removeRequestInformation($contextItems);
}
if (! $this->hasTab('appTab')) {
$contextItems = $this->removeAppInformation($contextItems);
}
if (! $this->hasTab('userTab')) {
$contextItems = $this->removeUserInformation($contextItems);
}
if (! $this->hasTab('contextTab')) {
$contextItems = $this->removeContextInformation($contextItems);
}
if (! $this->hasTab('debugTab')) {
$contextItems = $this->removeDebugInformation($contextItems);
}
return $contextItems;
}
protected function removeRequestInformation(array $contextItems): array
{
Arr::forget($contextItems, 'request');
Arr::forget($contextItems, 'request_data');
Arr::forget($contextItems, 'headers');
Arr::forget($contextItems, 'session');
Arr::forget($contextItems, 'cookies');
return $contextItems;
}
protected function removeAppInformation(array $contextItems): array
{
Arr::forget($contextItems, 'view');
Arr::forget($contextItems, 'route');
return $contextItems;
}
protected function removeUserInformation(array $contextItems): array
{
Arr::forget($contextItems, 'user');
Arr::forget($contextItems, 'request.ip');
Arr::forget($contextItems, 'request.useragent');
return $contextItems;
}
protected function removeContextInformation(array $contextItems): array
{
Arr::forget($contextItems, 'env');
Arr::forget($contextItems, 'git');
Arr::forget($contextItems, 'context');
Arr::forget($contextItems, $this->getCustomContextGroups($contextItems));
return $contextItems;
}
protected function removeDebugInformation(array $contextItems): array
{
Arr::forget($contextItems, 'dumps');
Arr::forget($contextItems, 'glows');
Arr::forget($contextItems, 'logs');
Arr::forget($contextItems, 'queries');
return $contextItems;
}
protected function getCustomContextGroups(array $contextItems): array
{
$predefinedContextItemGroups = [
'request',
'request_data',
'headers',
'session',
'cookies',
'view',
'queries',
'route',
'user',
'env',
'git',
'context',
'logs',
'dumps',
'exception',
];
return Collection::make($contextItems)
->reject(function ($_value, $group) use ($predefinedContextItemGroups) {
return in_array($group, $predefinedContextItemGroups);
})
->keys()
->toArray();
}
protected function trimReport(array $report): array
{
return (new ReportTrimmer())->trim($report);
}
}