ShareReportAction   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 0
loc 156
rs 10
c 0
b 0
f 0

12 Methods

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