Issues (11)

src/Message.php (8 issues)

1
<?php
2
3
namespace Eloquentcoder\Slack;
4
5
use InvalidArgumentException;
6
7
class Message
8
{
9
    /**
10
     * Reference to the Slack client responsible for sending
11
     * the message.
12
     *
13
     * @var \Eloquentcoder\Slack\Client
14
     */
15
    protected $client;
16
17
    /**
18
     * The text to send with the message.
19
     *
20
     * @var string
21
     */
22
    protected $text;
23
24
    /**
25
     * The channel the message should be sent to.
26
     *
27
     * @var string
28
     */
29
    protected $channel;
30
31
    /**
32
     * The username the message should be sent as.
33
     *
34
     * @var string
35
     */
36
    protected $username;
37
38
    /**
39
     * The URL to the icon to use.
40
     *
41
     * @var string
42
     */
43
    protected $icon;
44
45
    /**
46
     * The type of icon we are using.
47
     *
48
     * @var enum
0 ignored issues
show
The type Eloquentcoder\Slack\enum 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...
49
     */
50
    protected $iconType;
51
52
    /**
53
     * Whether the message text should be interpreted in Slack's
54
     * Markdown-like language.
55
     *
56
     * @var bool
57
     */
58
    protected $allow_markdown = true;
59
60
    /**
61
     * The attachment fields which should be formatted with
62
     * Slack's Markdown-like language.
63
     *
64
     * @var array
65
     */
66
    protected $markdown_in_attachments = [];
67
68
    /**
69
     * An array of attachments to send.
70
     *
71
     * @var array
72
     */
73
    protected $attachments = [];
74
75
    /**
76
     * @var string
77
     */
78
    const ICON_TYPE_URL = 'icon_url';
79
80
    /**
81
     * @var string
82
     */
83
    const ICON_TYPE_EMOJI = 'icon_emoji';
84
85
    /**
86
     * Instantiate a new Message.
87
     *
88
     * @param \Maknz\Slack\Client $client
0 ignored issues
show
The type Maknz\Slack\Client 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...
89
     *
90
     * @return void
91
     */
92
    public function __construct(Client $client)
93
    {
94
        $this->client = $client;
95
    }
96
97
    /**
98
     * Get the message text.
99
     *
100
     * @return string
101
     */
102
    public function getText()
103
    {
104
        return $this->text;
105
    }
106
107
    /**
108
     * Set the message text.
109
     *
110
     * @param string $text
111
     *
112
     * @return $this
113
     */
114
    public function setText($text)
115
    {
116
        $this->text = $text;
117
118
        return $this;
119
    }
120
121
    /**
122
     * Get the channel we will post to.
123
     *
124
     * @return string
125
     */
126
    public function getChannel()
127
    {
128
        return $this->channel;
129
    }
130
131
    /**
132
     * Set the channel we will post to.
133
     *
134
     * @param string $channel
135
     *
136
     * @return $this
137
     */
138
    public function setChannel($channel)
139
    {
140
        $this->channel = $channel;
141
142
        return $this;
143
    }
144
145
    /**
146
     * Get the username we will post as.
147
     *
148
     * @return string
149
     */
150
    public function getUsername()
151
    {
152
        return $this->username;
153
    }
154
155
    /**
156
     * Set the username we will post as.
157
     *
158
     * @param string $username
159
     *
160
     * @return $this
161
     */
162
    public function setUsername($username)
163
    {
164
        $this->username = $username;
165
166
        return $this;
167
    }
168
169
    /**
170
     * Get the icon (either URL or emoji) we will post as.
171
     *
172
     * @return string
173
     */
174
    public function getIcon()
175
    {
176
        return $this->icon;
177
    }
178
179
    /**
180
     * Set the icon (either URL or emoji) we will post as.
181
     *
182
     * @param string $icon
183
     *
184
     * @return $this
185
     */
186
    public function setIcon($icon)
187
    {
188
        if ($icon == null) {
189
            $this->icon = $this->iconType = null;
190
191
            return;
192
        }
193
194
        if (mb_substr($icon, 0, 1) == ':' && mb_substr($icon, mb_strlen($icon) - 1, 1) == ':') {
195
            $this->iconType = self::ICON_TYPE_EMOJI;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::ICON_TYPE_EMOJI of type string is incompatible with the declared type Eloquentcoder\Slack\enum of property $iconType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
196
        } else {
197
            $this->iconType = self::ICON_TYPE_URL;
198
        }
199
200
        $this->icon = $icon;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Get the icon type being used, if an icon is set.
207
     *
208
     * @return string
209
     */
210
    public function getIconType()
211
    {
212
        return $this->iconType;
213
    }
214
215
    /**
216
     * Get whether message text should be formatted with
217
     * Slack's Markdown-like language.
218
     *
219
     * @return bool
220
     */
221
    public function getAllowMarkdown()
222
    {
223
        return $this->allow_markdown;
224
    }
225
226
    /**
227
     * Set whether message text should be formatted with
228
     * Slack's Markdown-like language.
229
     *
230
     * @param bool $value
231
     *
232
     * @return void
233
     */
234
    public function setAllowMarkdown($value)
235
    {
236
        $this->allow_markdown = (bool) $value;
237
238
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Eloquentcoder\Slack\Message which is incompatible with the documented return type void.
Loading history...
239
    }
240
241
    /**
242
     * Enable Markdown formatting for the message.
243
     *
244
     * @return void
245
     */
246
    public function enableMarkdown()
247
    {
248
        $this->setAllowMarkdown(true);
249
250
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Eloquentcoder\Slack\Message which is incompatible with the documented return type void.
Loading history...
251
    }
252
253
    /**
254
     * Disable Markdown formatting for the message.
255
     *
256
     * @return void
257
     */
258
    public function disableMarkdown()
259
    {
260
        $this->setAllowMarkdown(false);
261
262
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Eloquentcoder\Slack\Message which is incompatible with the documented return type void.
Loading history...
263
    }
264
265
    /**
266
     * Get the attachment fields which should be formatted
267
     * in Slack's Markdown-like language.
268
     *
269
     * @return array
270
     */
271
    public function getMarkdownInAttachments()
272
    {
273
        return $this->markdown_in_attachments;
274
    }
275
276
    /**
277
     * Set the attachment fields which should be formatted
278
     * in Slack's Markdown-like language.
279
     *
280
     * @param array $fields
281
     *
282
     * @return void
283
     */
284
    public function setMarkdownInAttachments(array $fields)
285
    {
286
        $this->markdown_in_attachments = $fields;
287
288
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Eloquentcoder\Slack\Message which is incompatible with the documented return type void.
Loading history...
289
    }
290
291
    /**
292
     * Change the name of the user the post will be made as.
293
     *
294
     * @param string $username
295
     *
296
     * @return $this
297
     */
298
    public function from($username)
299
    {
300
        $this->setUsername($username);
301
302
        return $this;
303
    }
304
305
    /**
306
     * Change the channel the post will be made to.
307
     *
308
     * @param string $channel
309
     *
310
     * @return $this
311
     */
312
    public function to($channel)
313
    {
314
        $this->setChannel($channel);
315
316
        return $this;
317
    }
318
319
    /**
320
     * Chainable method for setting the icon.
321
     *
322
     * @param string $icon
323
     *
324
     * @return $this
325
     */
326
    public function withIcon($icon)
327
    {
328
        $this->setIcon($icon);
329
330
        return $this;
331
    }
332
333
    /**
334
     * Add an attachment to the message.
335
     *
336
     * @param mixed $attachment
337
     *
338
     * @return $this
339
     */
340
    public function attach($attachment)
341
    {
342
        if ($attachment instanceof Attachment) {
343
            $this->attachments[] = $attachment;
344
345
            return $this;
346
        } elseif (is_array($attachment)) {
347
            $attachmentObject = new Attachment($attachment);
348
349
            if (!isset($attachment['mrkdwn_in'])) {
350
                $attachmentObject->setMarkdownFields($this->getMarkdownInAttachments());
351
            }
352
353
            $this->attachments[] = $attachmentObject;
354
355
            return $this;
356
        }
357
358
        throw new InvalidArgumentException('Attachment must be an instance of Maknz\\Slack\\Attachment or a keyed array');
359
    }
360
361
    /**
362
     * Get the attachments for the message.
363
     *
364
     * @return array
365
     */
366
    public function getAttachments()
367
    {
368
        return $this->attachments;
369
    }
370
371
    /**
372
     * Set the attachments for the message.
373
     *
374
     * @param array $attachments
375
     *
376
     * @return $this
377
     */
378
    public function setAttachments(array $attachments)
379
    {
380
        $this->clearAttachments();
381
382
        foreach ($attachments as $attachment) {
383
            $this->attach($attachment);
384
        }
385
386
        return $this;
387
    }
388
389
    /**
390
     * Remove all attachments for the message.
391
     *
392
     * @return $this
393
     */
394
    public function clearAttachments()
395
    {
396
        $this->attachments = [];
397
398
        return $this;
399
    }
400
401
    /**
402
     * Send the message.
403
     *
404
     * @param string $text The text to send
405
     *
406
     * @return void
407
     */
408
    public function send($text = null)
409
    {
410
        if ($text) {
411
            $this->setText($text);
412
        }
413
414
        $this->client->sendMessage($this);
415
416
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Eloquentcoder\Slack\Message which is incompatible with the documented return type void.
Loading history...
417
    }
418
}
419