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.
Completed
Push — master ( fe344e...b289bf )
by Freek
01:34
created

Response::withAttachments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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