Passed
Push — master ( 13f991...757eec )
by Sullivan
04:42 queued 02:44
created

Client::getMarkdownInAttachments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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