GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Response::getPayload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\SlashCommand;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Http\Response as IlluminateResponse;
7
use Illuminate\Support\Str;
8
9
class Response
10
{
11
    /** @var \Spatie\SlashCommand\Request */
12
    protected $request;
13
14
    /** @var string */
15
    protected $text;
16
17
    /** @var string */
18
    protected $responseType;
19
20
    /** @var string */
21
    protected $channel;
22
23
    /** @var string */
24
    protected $icon = '';
25
26
    /** @var \Illuminate\Support\Collection */
27
    protected $attachments;
28
29
    /** @var \GuzzleHttp\Client */
30
    protected $client;
31
32
    public static function create(Request $request): self
33
    {
34
        $client = app(Client::class);
35
36
        return new static($client, $request);
37
    }
38
39
    public function __construct(Client $client, Request $request)
40
    {
41
        $this->client = $client;
42
43
        $this->request = $request;
44
45
        $this->channel = $request->channelName;
46
47
        $this->displayResponseToUserWhoTypedCommand();
48
49
        $this->attachments = new \Illuminate\Support\Collection();
50
    }
51
52
    /**
53
     * @param string $text
54
     *
55
     * @return $this
56
     */
57
    public function withText(string $text)
58
    {
59
        $this->text = $text;
60
61
        return $this;
62
    }
63
64
    public function onChannel(string $channelName)
65
    {
66
        $this->channel = $channelName;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return $this
73
     */
74
    public function displayResponseToUserWhoTypedCommand()
75
    {
76
        $this->responseType = 'ephemeral';
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param \Spatie\SlashCommand\Attachment $attachment
83
     *
84
     * @return $this
85
     */
86
    public function withAttachment(Attachment $attachment)
87
    {
88
        $this->attachments->push($attachment);
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param array|\Spatie\SlashCommand\Attachment $attachments
95
     *
96
     * @return $this
97
     */
98
    public function withAttachments($attachments)
99
    {
100
        if (! is_array($attachments)) {
101
            $attachments = [$attachments];
102
        }
103
104
        foreach ($attachments as $attachment) {
105
            $this->withAttachment($attachment);
106
        }
107
108
        return $this;
109
    }
110
111
    /**
112
     * @param string $channelName
113
     *
114
     * @return $this
115
     */
116
    public function displayResponseToEveryoneOnChannel(string $channelName = '')
117
    {
118
        $this->responseType = 'in_channel';
119
120
        if ($channelName !== '') {
121
            $this->onChannel($channelName);
122
        }
123
124
        return $this;
125
    }
126
127
    /**
128
     * Set the icon (either URL or emoji) we will post as.
129
     *
130
     * @param string $icon
131
     *
132
     * @return $this
133
     */
134
    public function useIcon(string $icon)
135
    {
136
        $this->icon = $icon;
137
138
        return $this;
139
    }
140
141
    public function getIconType(): string
142
    {
143
        if (empty($this->icon)) {
144
            return '';
145
        }
146
147
        if (Str::startsWith($this->icon, ':') && Str::endsWith($this->icon, ':')) {
148
            return 'icon_emoji';
149
        }
150
151
        return 'icon_url';
152
    }
153
154
    /**
155
     * Send the response to Slack.
156
     */
157
    public function send()
158
    {
159
        $this->client->post($this->request->responseUrl, ['json' => $this->getPayload()]);
160
    }
161
162
    public function getIlluminateResponse(): IlluminateResponse
163
    {
164
        return new IlluminateResponse($this->getPayload());
165
    }
166
167
    protected function getPayload(): array
168
    {
169
        $payload = [
170
            'text' => $this->text,
171
            'channel' => $this->channel,
172
            'link_names' => true,
173
            'unfurl_links' => true,
174
            'unfurl_media' => true,
175
            'mrkdwn' => true,
176
            'response_type' => $this->responseType,
177
            'attachments' => $this->attachments->map(function (Attachment $attachment) {
178
                return $attachment->toArray();
179
            })->toArray(),
180
        ];
181
182
        if (! empty($this->icon)) {
183
            $payload[$this->getIconType()] = $this->icon;
184
        }
185
186
        return $payload;
187
    }
188
}
189