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
Pull Request — master (#67)
by Sullivan
33:05 queued 30:00
created

Client::setStickyChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Maknz\Slack;
4
5
use GuzzleHttp\Client as Guzzle;
6
use RuntimeException;
7
8
class Client
9
{
10
    /**
11
     * The Slack incoming webhook endpoint.
12
     *
13
     * @var string
14
     */
15
    protected $endpoint;
16
17
    /**
18
     * The default channel to send messages to.
19
     *
20
     * @var string
21
     */
22
    protected $channel;
23
24
    /**
25
     * If set to true, all messages will be sent to the default channel,
26
     * even if you specify another one during the runtime.
27
     *
28
     * This is useful for dev environment.
29
     *
30
     * @var bool
31
     */
32
    protected $sticky_channel = false;
33
34
    /**
35
     * The default username to send messages as.
36
     *
37
     * @var string
38
     */
39
    protected $username;
40
41
    /**
42
     * The default icon to send messages with.
43
     *
44
     * @var string
45
     */
46
    protected $icon;
47
48
    /**
49
     * Whether to link names like @regan or leave
50
     * them as plain text.
51
     *
52
     * @var bool
53
     */
54
    protected $link_names = false;
55
56
    /**
57
     * Whether Slack should unfurl text-based URLs.
58
     *
59
     * @var bool
60
     */
61
    protected $unfurl_links = false;
62
63
    /**
64
     * Whether Slack should unfurl media URLs.
65
     *
66
     * @var bool
67
     */
68
    protected $unfurl_media = true;
69
70
    /**
71
     * Whether message text should be formatted with Slack's
72
     * Markdown-like language.
73
     *
74
     * @var bool
75
     */
76
    protected $allow_markdown = true;
77
78
    /**
79
     * The attachment fields which should be formatted with
80
     * Slack's Markdown-like language.
81
     *
82
     * @var array
83
     */
84
    protected $markdown_in_attachments = [];
85
86
    /**
87
     * The Guzzle HTTP client instance.
88
     *
89
     * @var \GuzzleHttp\Client
90
     */
91
    protected $guzzle;
92
93
    /**
94
     * Instantiate a new Client.
95
     *
96
     * @param string $endpoint
97
     * @param array $attributes
98
     * @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...
99
     */
100
    public function __construct($endpoint, array $attributes = [], Guzzle $guzzle = null)
101
    {
102
        $this->endpoint = $endpoint;
103
104
        if (isset($attributes['channel'])) {
105
            $this->setDefaultChannel($attributes['channel']);
106
        }
107
108
        if (isset($attributes['sticky_channel'])) {
109
            $this->setStickyChannel($attributes['sticky_channel']);
110
        }
111
112
        if (isset($attributes['username'])) {
113
            $this->setDefaultUsername($attributes['username']);
114
        }
115
116
        if (isset($attributes['icon'])) {
117
            $this->setDefaultIcon($attributes['icon']);
118
        }
119
120
        if (isset($attributes['link_names'])) {
121
            $this->setLinkNames($attributes['link_names']);
122
        }
123
124
        if (isset($attributes['unfurl_links'])) {
125
            $this->setUnfurlLinks($attributes['unfurl_links']);
126
        }
127
128
        if (isset($attributes['unfurl_media'])) {
129
            $this->setUnfurlMedia($attributes['unfurl_media']);
130
        }
131
132
        if (isset($attributes['allow_markdown'])) {
133
            $this->setAllowMarkdown($attributes['allow_markdown']);
134
        }
135
136
        if (isset($attributes['markdown_in_attachments'])) {
137
            $this->setMarkdownInAttachments($attributes['markdown_in_attachments']);
138
        }
139
140
        $this->guzzle = $guzzle ?: new Guzzle;
141
    }
142
143
    /**
144
     * Pass any unhandled methods through to a new Message
145
     * instance.
146
     *
147
     * @param string $name The name of the method
148
     * @param array $arguments The method arguments
149
     * @return \Maknz\Slack\Message
150
     */
151
    public function __call($name, $arguments)
152
    {
153
        return call_user_func_array([$this->createMessage(), $name], $arguments);
154
    }
155
156
    /**
157
     * Get the Slack endpoint.
158
     *
159
     * @return string
160
     */
161
    public function getEndpoint()
162
    {
163
        return $this->endpoint;
164
    }
165
166
    /**
167
     * Set the Slack endpoint.
168
     *
169
     * @param string $endpoint
170
     * @return void
171
     */
172
    public function setEndpoint($endpoint)
173
    {
174
        $this->endpoint = $endpoint;
175
    }
176
177
    /**
178
     * Get the default channel messages will be created for.
179
     *
180
     * @return string
181
     */
182
    public function getDefaultChannel()
183
    {
184
        return $this->channel;
185
    }
186
187
    /**
188
     * Set the default channel messages will be created for.
189
     *
190
     * @param string $channel
191
     * @return void
192
     */
193
    public function setDefaultChannel($channel)
194
    {
195
        $this->channel = $channel;
196
    }
197
198
    /**
199
     * @return bool
200
     */
201
    public function isStickyChannel()
202
    {
203
        return $this->sticky_channel;
204
    }
205
206
    /**
207
     * @param bool $sticky_channel
208
     */
209
    public function setStickyChannel($sticky_channel)
210
    {
211
        $this->sticky_channel = $sticky_channel;
212
    }
213
214
    /**
215
     * Get the default username messages will be created for.
216
     *
217
     * @return string
218
     */
219
    public function getDefaultUsername()
220
    {
221
        return $this->username;
222
    }
223
224
    /**
225
     * Set the default username messages will be created for.
226
     *
227
     * @param string $username
228
     * @return void
229
     */
230
    public function setDefaultUsername($username)
231
    {
232
        $this->username = $username;
233
    }
234
235
    /**
236
     * Get the default icon messages will be created with.
237
     *
238
     * @return string
239
     */
240
    public function getDefaultIcon()
241
    {
242
        return $this->icon;
243
    }
244
245
    /**
246
     * Set the default icon messages will be created with.
247
     *
248
     * @param string $icon
249
     * @return void
250
     */
251
    public function setDefaultIcon($icon)
252
    {
253
        $this->icon = $icon;
254
    }
255
256
    /**
257
     * Get whether messages sent will have names (like @regan)
258
     * will be converted into links.
259
     *
260
     * @return bool
261
     */
262
    public function getLinkNames()
263
    {
264
        return $this->link_names;
265
    }
266
267
    /**
268
     * Set whether messages sent will have names (like @regan)
269
     * will be converted into links.
270
     *
271
     * @param bool $value
272
     * @return void
273
     */
274
    public function setLinkNames($value)
275
    {
276
        $this->link_names = (bool) $value;
277
    }
278
279
    /**
280
     * Get whether text links should be unfurled.
281
     *
282
     * @return bool
283
     */
284
    public function getUnfurlLinks()
285
    {
286
        return $this->unfurl_links;
287
    }
288
289
    /**
290
     * Set whether text links should be unfurled.
291
     *
292
     * @param bool $value
293
     * @return void
294
     */
295
    public function setUnfurlLinks($value)
296
    {
297
        $this->unfurl_links = (bool) $value;
298
    }
299
300
    /**
301
     * Get whether media links should be unfurled.
302
     *
303
     * @return bool
304
     */
305
    public function getUnfurlMedia()
306
    {
307
        return $this->unfurl_media;
308
    }
309
310
    /**
311
     * Set whether media links should be unfurled.
312
     *
313
     * @param bool $value
314
     * @return void
315
     */
316
    public function setUnfurlMedia($value)
317
    {
318
        $this->unfurl_media = (bool) $value;
319
    }
320
321
    /**
322
     * Get whether message text should be formatted with
323
     * Slack's Markdown-like language.
324
     *
325
     * @return bool
326
     */
327
    public function getAllowMarkdown()
328
    {
329
        return $this->allow_markdown;
330
    }
331
332
    /**
333
     * Set whether message text should be formatted with
334
     * Slack's Markdown-like language.
335
     *
336
     * @param bool $value
337
     * @return void
338
     */
339
    public function setAllowMarkdown($value)
340
    {
341
        $this->allow_markdown = (bool) $value;
342
    }
343
344
    /**
345
     * Get the attachment fields which should be formatted
346
     * in Slack's Markdown-like language.
347
     *
348
     * @return array
349
     */
350
    public function getMarkdownInAttachments()
351
    {
352
        return $this->markdown_in_attachments;
353
    }
354
355
    /**
356
     * Set the attachment fields which should be formatted
357
     * in Slack's Markdown-like language.
358
     *
359
     * @param array $fields
360
     * @return void
361
     */
362
    public function setMarkdownInAttachments(array $fields)
363
    {
364
        $this->markdown_in_attachments = $fields;
365
    }
366
367
    /**
368
     * Create a new message with defaults.
369
     *
370
     * @return \Maknz\Slack\Message
371
     */
372
    public function createMessage()
373
    {
374
        $message = new Message($this);
375
376
        $message->setChannel($this->getDefaultChannel());
377
378
        $message->setUsername($this->getDefaultUsername());
379
380
        $message->setIcon($this->getDefaultIcon());
381
382
        $message->setAllowMarkdown($this->getAllowMarkdown());
383
384
        $message->setMarkdownInAttachments($this->getMarkdownInAttachments());
385
386
        return $message;
387
    }
388
389
    /**
390
     * Send a message.
391
     *
392
     * @param \Maknz\Slack\Message $message
393
     * @return void
394
     */
395
    public function sendMessage(Message $message)
396
    {
397
        $payload = $this->preparePayload($message);
398
399
        $encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);
400
401
        if ($encoded === false) {
402
            throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg()));
403
        }
404
405
        $this->guzzle->post($this->endpoint, ['body' => $encoded]);
406
    }
407
408
    /**
409
     * Prepares the payload to be sent to the webhook.
410
     *
411
     * @param \Maknz\Slack\Message $message The message to send
412
     * @return array
413
     */
414
    public function preparePayload(Message $message)
415
    {
416
        $payload = [
417
            'text' => $message->getText(),
418
            'channel' => $this->isStickyChannel() ? $this->getDefaultChannel() : $message->getChannel(),
419
            'username' => $message->getUsername(),
420
            'link_names' => $this->getLinkNames() ? 1 : 0,
421
            'unfurl_links' => $this->getUnfurlLinks(),
422
            'unfurl_media' => $this->getUnfurlMedia(),
423
            'mrkdwn' => $message->getAllowMarkdown(),
424
        ];
425
426
        if ($icon = $message->getIcon()) {
427
            $payload[$message->getIconType()] = $icon;
428
        }
429
430
        $payload['attachments'] = $this->getAttachmentsAsArrays($message);
431
432
        return $payload;
433
    }
434
435
    /**
436
     * Get the attachments in array form.
437
     *
438
     * @param \Maknz\Slack\Message $message
439
     * @return array
440
     */
441
    protected function getAttachmentsAsArrays(Message $message)
442
    {
443
        $attachments = [];
444
445
        foreach ($message->getAttachments() as $attachment) {
446
            $attachments[] = $attachment->toArray();
447
        }
448
449
        return $attachments;
450
    }
451
}
452