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 ( d55492...5a1c56 )
by Freek
02:41
created

Response::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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 string $channelName
94
     *
95
     * @return $this
96
     */
97
    public function displayResponseToEveryoneOnChannel(string $channelName = '')
98
    {
99
        $this->responseType = 'in_channel';
100
101
        if ($channelName !== '') {
102
            $this->onChannel($channelName);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * Set the icon (either URL or emoji) we will post as.
110
     *
111
     * @param string $icon
112
     *
113
     * @return this
114
     */
115
    public function useIcon(string $icon)
116
    {
117
        $this->icon = $icon;
118
119
        return $this;
120
    }
121
122
    public function getIconType(): string
123
    {
124
        if (empty($this->icon)) {
125
            return '';
126
        }
127
128
        if (starts_with($this->icon, ':') && ends_with($this->icon, ':')) {
129
            return 'icon_emoji';
130
        }
131
132
        return 'icon_url';
133
    }
134
135
    /**
136
     * Send the response to Slack.
137
     */
138
    public function send()
139
    {
140
        $this->client->post($this->request->responseUrl, ['json' => $this->getPayload()]);
141
    }
142
143
    public function getIlluminateResponse(): IlluminateResponse
144
    {
145
        return new IlluminateResponse($this->getPayload());
146
    }
147
148
    protected function getPayload(): array
149
    {
150
        $payload = [
151
            'text' => $this->text,
152
            'channel' => $this->channel,
153
            'link_names' => true,
154
            'unfurl_links' => true,
155
            'unfurl_media' => true,
156
            'mrkdwn' => true,
157
            'response_type' => $this->responseType,
158
            'attachments' => $this->attachments->map(function (Attachment $attachment) {
159
                return $attachment->toArray();
160
            })->toArray(),
161
        ];
162
163
        if (!empty($this->icon)) {
164
            $payload[$this->getIconType()] = $this->icon;
165
        }
166
    }
167
}
168