Issues (11)

src/Client.php (2 issues)

1
<?php
2
3
namespace Eloquentcoder\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
     * The default username to send messages as.
26
     *
27
     * @var string
28
     */
29
    protected $username;
30
31
    /**
32
     * The default icon to send messages with.
33
     *
34
     * @var string
35
     */
36
    protected $icon;
37
38
    /**
39
     * Whether to link names like @regan or leave
40
     * them as plain text.
41
     *
42
     * @var bool
43
     */
44
    protected $link_names = false;
45
46
    /**
47
     * Whether Slack should unfurl text-based URLs.
48
     *
49
     * @var bool
50
     */
51
    protected $unfurl_links = false;
52
53
    /**
54
     * Whether Slack should unfurl media URLs.
55
     *
56
     * @var bool
57
     */
58
    protected $unfurl_media = true;
59
60
    /**
61
     * Whether message text should be formatted with Slack's
62
     * Markdown-like language.
63
     *
64
     * @var bool
65
     */
66
    protected $allow_markdown = true;
67
68
    /**
69
     * The attachment fields which should be formatted with
70
     * Slack's Markdown-like language.
71
     *
72
     * @var array
73
     */
74
    protected $markdown_in_attachments = [];
75
76
    /**
77
     * The Guzzle HTTP client instance.
78
     *
79
     * @var \GuzzleHttp\Client
80
     */
81
    protected $guzzle;
82
83
    /**
84
     * Instantiate a new Client.
85
     *
86
     * @param string $endpoint
87
     * @param array  $attributes
88
     *
89
     * @return void
90
     */
91
    public function __construct($endpoint, array $attributes = [], Guzzle $guzzle = null)
92
    {
93
        $this->endpoint = $endpoint;
94
95
        if (isset($attributes['channel'])) {
96
            $this->setDefaultChannel($attributes['channel']);
97
        }
98
99
        if (isset($attributes['username'])) {
100
            $this->setDefaultUsername($attributes['username']);
101
        }
102
103
        if (isset($attributes['icon'])) {
104
            $this->setDefaultIcon($attributes['icon']);
105
        }
106
107
        if (isset($attributes['link_names'])) {
108
            $this->setLinkNames($attributes['link_names']);
109
        }
110
111
        if (isset($attributes['unfurl_links'])) {
112
            $this->setUnfurlLinks($attributes['unfurl_links']);
113
        }
114
115
        if (isset($attributes['unfurl_media'])) {
116
            $this->setUnfurlMedia($attributes['unfurl_media']);
117
        }
118
119
        if (isset($attributes['allow_markdown'])) {
120
            $this->setAllowMarkdown($attributes['allow_markdown']);
121
        }
122
123
        if (isset($attributes['markdown_in_attachments'])) {
124
            $this->setMarkdownInAttachments($attributes['markdown_in_attachments']);
125
        }
126
127
        $this->guzzle = $guzzle ?: new Guzzle();
128
    }
129
130
    /**
131
     * Pass any unhandled methods through to a new Message
132
     * instance.
133
     *
134
     * @param string $name      The name of the method
135
     * @param array  $arguments The method arguments
136
     *
137
     * @return \Maknz\Slack\Message
0 ignored issues
show
The type Maknz\Slack\Message was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
138
     */
139
    public function __call($name, $arguments)
140
    {
141
        return call_user_func_array([$this->createMessage(), $name], $arguments);
142
    }
143
144
    /**
145
     * Get the Slack endpoint.
146
     *
147
     * @return string
148
     */
149
    public function getEndpoint()
150
    {
151
        return $this->endpoint;
152
    }
153
154
    /**
155
     * Set the Slack endpoint.
156
     *
157
     * @param string $endpoint
158
     *
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
     *
181
     * @return void
182
     */
183
    public function setDefaultChannel($channel)
184
    {
185
        $this->channel = $channel;
186
    }
187
188
    /**
189
     * Get the default username messages will be created for.
190
     *
191
     * @return string
192
     */
193
    public function getDefaultUsername()
194
    {
195
        return $this->username;
196
    }
197
198
    /**
199
     * Set the default username messages will be created for.
200
     *
201
     * @param string $username
202
     *
203
     * @return void
204
     */
205
    public function setDefaultUsername($username)
206
    {
207
        $this->username = $username;
208
    }
209
210
    /**
211
     * Get the default icon messages will be created with.
212
     *
213
     * @return string
214
     */
215
    public function getDefaultIcon()
216
    {
217
        return $this->icon;
218
    }
219
220
    /**
221
     * Set the default icon messages will be created with.
222
     *
223
     * @param string $icon
224
     *
225
     * @return void
226
     */
227
    public function setDefaultIcon($icon)
228
    {
229
        $this->icon = $icon;
230
    }
231
232
    /**
233
     * Get whether messages sent will have names (like @regan)
234
     * will be converted into links.
235
     *
236
     * @return bool
237
     */
238
    public function getLinkNames()
239
    {
240
        return $this->link_names;
241
    }
242
243
    /**
244
     * Set whether messages sent will have names (like @regan)
245
     * will be converted into links.
246
     *
247
     * @param bool $value
248
     *
249
     * @return void
250
     */
251
    public function setLinkNames($value)
252
    {
253
        $this->link_names = (bool) $value;
254
    }
255
256
    /**
257
     * Get whether text links should be unfurled.
258
     *
259
     * @return bool
260
     */
261
    public function getUnfurlLinks()
262
    {
263
        return $this->unfurl_links;
264
    }
265
266
    /**
267
     * Set whether text links should be unfurled.
268
     *
269
     * @param bool $value
270
     *
271
     * @return void
272
     */
273
    public function setUnfurlLinks($value)
274
    {
275
        $this->unfurl_links = (bool) $value;
276
    }
277
278
    /**
279
     * Get whether media links should be unfurled.
280
     *
281
     * @return bool
282
     */
283
    public function getUnfurlMedia()
284
    {
285
        return $this->unfurl_media;
286
    }
287
288
    /**
289
     * Set whether media links should be unfurled.
290
     *
291
     * @param bool $value
292
     *
293
     * @return void
294
     */
295
    public function setUnfurlMedia($value)
296
    {
297
        $this->unfurl_media = (bool) $value;
298
    }
299
300
    /**
301
     * Get whether message text should be formatted with
302
     * Slack's Markdown-like language.
303
     *
304
     * @return bool
305
     */
306
    public function getAllowMarkdown()
307
    {
308
        return $this->allow_markdown;
309
    }
310
311
    /**
312
     * Set whether message text should be formatted with
313
     * Slack's Markdown-like language.
314
     *
315
     * @param bool $value
316
     *
317
     * @return void
318
     */
319
    public function setAllowMarkdown($value)
320
    {
321
        $this->allow_markdown = (bool) $value;
322
    }
323
324
    /**
325
     * Get the attachment fields which should be formatted
326
     * in Slack's Markdown-like language.
327
     *
328
     * @return array
329
     */
330
    public function getMarkdownInAttachments()
331
    {
332
        return $this->markdown_in_attachments;
333
    }
334
335
    /**
336
     * Set the attachment fields which should be formatted
337
     * in Slack's Markdown-like language.
338
     *
339
     * @param array $fields
340
     *
341
     * @return void
342
     */
343
    public function setMarkdownInAttachments(array $fields)
344
    {
345
        $this->markdown_in_attachments = $fields;
346
    }
347
348
    /**
349
     * Create a new message with defaults.
350
     *
351
     * @return \Maknz\Slack\Message
352
     */
353
    public function createMessage()
354
    {
355
        $message = new Message($this);
356
357
        $message->setChannel($this->getDefaultChannel());
358
359
        $message->setUsername($this->getDefaultUsername());
360
361
        $message->setIcon($this->getDefaultIcon());
362
363
        $message->setAllowMarkdown($this->getAllowMarkdown());
364
365
        $message->setMarkdownInAttachments($this->getMarkdownInAttachments());
366
367
        return $message;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $message returns the type Eloquentcoder\Slack\Message which is incompatible with the documented return type Maknz\Slack\Message.
Loading history...
368
    }
369
370
    /**
371
     * Send a message.
372
     *
373
     * @param \Eloquentcoder\Slack\Message $message
374
     *
375
     * @return void
376
     */
377
    public function sendMessage(Message $message)
378
    {
379
        $payload = $this->preparePayload($message);
380
381
        $encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);
382
383
        if ($encoded === false) {
384
            throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg()));
385
        }
386
387
        $this->guzzle->post($this->endpoint, ['body' => $encoded]);
388
    }
389
390
    /**
391
     * Prepares the payload to be sent to the webhook.
392
     *
393
     * @param \Eloquentcoder\Slack\Message $message The message to send
394
     *
395
     * @return array
396
     */
397
    public function preparePayload(Message $message)
398
    {
399
        $payload = [
400
            'text'         => $message->getText(),
401
            'channel'      => $message->getChannel(),
402
            'username'     => $message->getUsername(),
403
            'link_names'   => $this->getLinkNames() ? 1 : 0,
404
            'unfurl_links' => $this->getUnfurlLinks(),
405
            'unfurl_media' => $this->getUnfurlMedia(),
406
            'mrkdwn'       => $message->getAllowMarkdown(),
407
        ];
408
409
        if ($icon = $message->getIcon()) {
410
            $payload[$message->getIconType()] = $icon;
411
        }
412
413
        $payload['attachments'] = $this->getAttachmentsAsArrays($message);
414
415
        return $payload;
416
    }
417
418
    /**
419
     * Get the attachments in array form.
420
     *
421
     * @param \Maknz\Slack\Message $message
422
     *
423
     * @return array
424
     */
425
    protected function getAttachmentsAsArrays(Message $message)
426
    {
427
        $attachments = [];
428
429
        foreach ($message->getAttachments() as $attachment) {
430
            $attachments[] = $attachment->toArray();
431
        }
432
433
        return $attachments;
434
    }
435
}
436