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 ( 76f849...693789 )
by Regan
10s
created

Client::createMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 16
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Maknz\Slack;
4
5
use GuzzleHttp\Client as Guzzle;
6
7
class Client
8
{
9
    /**
10
     * The Slack incoming webhook endpoint.
11
     *
12
     * @var string
13
     */
14
    protected $endpoint;
15
16
    /**
17
     * The default channel to send messages to.
18
     *
19
     * @var string
20
     */
21
    protected $channel;
22
23
    /**
24
     * The default username to send messages as.
25
     *
26
     * @var string
27
     */
28
    protected $username;
29
30
    /**
31
     * The default icon to send messages with.
32
     *
33
     * @var string
34
     */
35
    protected $icon;
36
37
    /**
38
     * Whether to link names like @regan or leave
39
     * them as plain text.
40
     *
41
     * @var bool
42
     */
43
    protected $link_names = false;
44
45
    /**
46
     * Whether Slack should unfurl text-based URLs.
47
     *
48
     * @var bool
49
     */
50
    protected $unfurl_links = false;
51
52
    /**
53
     * Whether Slack should unfurl media URLs.
54
     *
55
     * @var bool
56
     */
57
    protected $unfurl_media = true;
58
59
    /**
60
     * Whether message text should be formatted with Slack's
61
     * Markdown-like language.
62
     *
63
     * @var bool
64
     */
65
    protected $allow_markdown = true;
66
67
    /**
68
     * The attachment fields which should be formatted with
69
     * Slack's Markdown-like language.
70
     *
71
     * @var array
72
     */
73
    protected $markdown_in_attachments = [];
74
75
    /**
76
     * The Guzzle HTTP client instance.
77
     *
78
     * @var \GuzzleHttp\Client
79
     */
80
    protected $guzzle;
81
82
    /**
83
     * Instantiate a new Client.
84
     *
85
     * @param string $endpoint
86
     * @param array $attributes
87
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
88
     */
89
    public function __construct($endpoint, array $attributes = [], Guzzle $guzzle = null)
90
    {
91
        $this->endpoint = $endpoint;
92
93
        if (isset($attributes['channel'])) {
94
            $this->setDefaultChannel($attributes['channel']);
95
        }
96
97
        if (isset($attributes['username'])) {
98
            $this->setDefaultUsername($attributes['username']);
99
        }
100
101
        if (isset($attributes['icon'])) {
102
            $this->setDefaultIcon($attributes['icon']);
103
        }
104
105
        if (isset($attributes['link_names'])) {
106
            $this->setLinkNames($attributes['link_names']);
107
        }
108
109
        if (isset($attributes['unfurl_links'])) {
110
            $this->setUnfurlLinks($attributes['unfurl_links']);
111
        }
112
113
        if (isset($attributes['unfurl_media'])) {
114
            $this->setUnfurlMedia($attributes['unfurl_media']);
115
        }
116
117
        if (isset($attributes['allow_markdown'])) {
118
            $this->setAllowMarkdown($attributes['allow_markdown']);
119
        }
120
121
        if (isset($attributes['markdown_in_attachments'])) {
122
            $this->setMarkdownInAttachments($attributes['markdown_in_attachments']);
123
        }
124
125
        $this->guzzle = $guzzle ?: new Guzzle;
126
    }
127
128
    /**
129
     * Pass any unhandled methods through to a new Message
130
     * instance.
131
     *
132
     * @param string $name The name of the method
133
     * @param array $arguments The method arguments
134
     * @return \Maknz\Slack\Message
135
     */
136
    public function __call($name, $arguments)
137
    {
138
        $message = $this->createMessage();
139
140
        call_user_func_array([$message, $name], $arguments);
141
142
        return $message;
143
    }
144
145
    /**
146
     * Get the Slack endpoint.
147
     *
148
     * @return string
149
     */
150
    public function getEndpoint()
151
    {
152
        return $this->endpoint;
153
    }
154
155
    /**
156
     * Set the Slack endpoint.
157
     *
158
     * @param string $endpoint
159
     * @return void
160
     */
161
    public function setEndpoint($endpoint)
162
    {
163
        $this->endpoint = $endpoint;
164
    }
165
166
    /**
167
     * Get the default channel messages will be created for.
168
     *
169
     * @return string
170
     */
171
    public function getDefaultChannel()
172
    {
173
        return $this->channel;
174
    }
175
176
    /**
177
     * Set the default channel messages will be created for.
178
     *
179
     * @param string $channel
180
     * @return void
181
     */
182
    public function setDefaultChannel($channel)
183
    {
184
        $this->channel = $channel;
185
    }
186
187
    /**
188
     * Get the default username messages will be created for.
189
     *
190
     * @return string
191
     */
192
    public function getDefaultUsername()
193
    {
194
        return $this->username;
195
    }
196
197
    /**
198
     * Set the default username messages will be created for.
199
     *
200
     * @param string $username
201
     * @return void
202
     */
203
    public function setDefaultUsername($username)
204
    {
205
        $this->username = $username;
206
    }
207
208
    /**
209
     * Get the default icon messages will be created with.
210
     *
211
     * @return string
212
     */
213
    public function getDefaultIcon()
214
    {
215
        return $this->icon;
216
    }
217
218
    /**
219
     * Set the default icon messages will be created with.
220
     *
221
     * @param string $icon
222
     * @return void
223
     */
224
    public function setDefaultIcon($icon)
225
    {
226
        $this->icon = $icon;
227
    }
228
229
    /**
230
     * Get whether messages sent will have names (like @regan)
231
     * will be converted into links.
232
     *
233
     * @return bool
234
     */
235
    public function getLinkNames()
236
    {
237
        return $this->link_names;
238
    }
239
240
    /**
241
     * Set whether messages sent will have names (like @regan)
242
     * will be converted into links.
243
     *
244
     * @param bool $value
245
     * @return void
246
     */
247
    public function setLinkNames($value)
248
    {
249
        $this->link_names = (bool) $value;
250
    }
251
252
    /**
253
     * Get whether text links should be unfurled.
254
     *
255
     * @return bool
256
     */
257
    public function getUnfurlLinks()
258
    {
259
        return $this->unfurl_links;
260
    }
261
262
    /**
263
     * Set whether text links should be unfurled.
264
     *
265
     * @param bool $value
266
     * @return void
267
     */
268
    public function setUnfurlLinks($value)
269
    {
270
        $this->unfurl_links = (bool) $value;
271
    }
272
273
    /**
274
     * Get whether media links should be unfurled.
275
     *
276
     * @return bool
277
     */
278
    public function getUnfurlMedia()
279
    {
280
        return $this->unfurl_media;
281
    }
282
283
    /**
284
     * Set whether media links should be unfurled.
285
     *
286
     * @param bool $value
287
     * @return void
288
     */
289
    public function setUnfurlMedia($value)
290
    {
291
        $this->unfurl_media = (bool) $value;
292
    }
293
294
    /**
295
     * Get whether message text should be formatted with
296
     * Slack's Markdown-like language.
297
     *
298
     * @return bool
299
     */
300
    public function getAllowMarkdown()
301
    {
302
        return $this->allow_markdown;
303
    }
304
305
    /**
306
     * Set whether message text should be formatted with
307
     * Slack's Markdown-like language.
308
     *
309
     * @param bool $value
310
     * @return void
311
     */
312
    public function setAllowMarkdown($value)
313
    {
314
        $this->allow_markdown = (bool) $value;
315
    }
316
317
    /**
318
     * Get the attachment fields which should be formatted
319
     * in Slack's Markdown-like language.
320
     *
321
     * @return array
322
     */
323
    public function getMarkdownInAttachments()
324
    {
325
        return $this->markdown_in_attachments;
326
    }
327
328
    /**
329
     * Set the attachment fields which should be formatted
330
     * in Slack's Markdown-like language.
331
     *
332
     * @param array $fields
333
     * @return void
334
     */
335
    public function setMarkdownInAttachments(array $fields)
336
    {
337
        $this->markdown_in_attachments = $fields;
338
    }
339
340
    /**
341
     * Create a new message with defaults.
342
     *
343
     * @return \Maknz\Slack\Message
344
     */
345
    public function createMessage()
346
    {
347
        $message = new Message($this);
348
349
        $message->setChannel($this->getDefaultChannel());
350
351
        $message->setUsername($this->getDefaultUsername());
352
353
        $message->setIcon($this->getDefaultIcon());
354
355
        $message->setAllowMarkdown($this->getAllowMarkdown());
356
357
        $message->setMarkdownInAttachments($this->getMarkdownInAttachments());
358
359
        return $message;
360
    }
361
362
    /**
363
     * Send a message.
364
     *
365
     * @param \Maknz\Slack\Message $message
366
     * @return void
367
     */
368
    public function sendMessage(Message $message)
369
    {
370
        $payload = $this->preparePayload($message);
371
372
        $encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);
373
374
        $this->guzzle->post($this->endpoint, ['body' => $encoded]);
375
    }
376
377
    /**
378
     * Prepares the payload to be sent to the webhook.
379
     *
380
     * @param \Maknz\Slack\Message $message The message to send
381
     * @return array
382
     */
383
    public function preparePayload(Message $message)
384
    {
385
        $payload = [
386
            'text' => $message->getText(),
387
            'channel' => $message->getChannel(),
388
            'username' => $message->getUsername(),
389
            'link_names' => $this->getLinkNames() ? 1 : 0,
390
            'unfurl_links' => $this->getUnfurlLinks(),
391
            'unfurl_media' => $this->getUnfurlMedia(),
392
            'mrkdwn' => $message->getAllowMarkdown(),
393
        ];
394
395
        if ($icon = $message->getIcon()) {
396
            $payload[$message->getIconType()] = $icon;
397
        }
398
399
        $payload['attachments'] = $this->getAttachmentsAsArrays($message);
400
401
        return $payload;
402
    }
403
404
    /**
405
     * Get the attachments in array form.
406
     *
407
     * @param \Maknz\Slack\Message $message
408
     * @return array
409
     */
410
    protected function getAttachmentsAsArrays(Message $message)
411
    {
412
        $attachments = [];
413
414
        foreach ($message->getAttachments() as $attachment) {
415
            $attachments[] = $attachment->toArray();
416
        }
417
418
        return $attachments;
419
    }
420
}
421