Message::resolveAttachment()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php namespace nyx\notify\transports\slack;
2
3
// External dependencies
4
use nyx\core;
5
use nyx\diagnostics;
6
7
/**
8
 * Slack Message
9
 *
10
 * @package     Nyx\Notify
11
 * @version     0.1.0
12
 * @author      Michal Chojnacki <[email protected]>
13
 * @copyright   2012-2017 Nyx Dev Team
14
 * @link        https://github.com/unyx/nyx
15
 * @todo        PHP7.1 pass for nullable return types (missing ?string on most methods).
16
 */
17
class Message implements core\interfaces\Serializable
18
{
19
    /**
20
     * The parse modes that can be requested for a Message.
21
     */
22
    const PARSE_DEFAULT = null;
23
    const PARSE_FULL    = 'full';
24
    const PARSE_NONE    = 'none';
25
26
    /**
27
     * The traits of a Slack Message.
28
     */
29
    use core\traits\Serializable;
30
31
    /**
32
     * @var string  The endpoint for this Message. Note: Only has effect when using the Slack Webhook Transport, not
33
     *              the Web API.
34
     */
35
    protected $endpoint;
36
37
    /**
38
     * @var string  The text content of the Message.
39
     */
40
    protected $text;
41
42
    /**
43
     * @var string  The channel this Message shall be sent to.
44
     */
45
    protected $channel;
46
47
    /**
48
     * @var string  The name this Message shall be sent as.
49
     */
50
    protected $username;
51
52
    /**
53
     * @var string  The icon of the Message.
54
     */
55
    protected $icon;
56
57
    /**
58
     * @var message\Attachment[]    The Attachments of the Message.
59
     */
60
    protected $attachments = [];
61
62
    /**
63
     * @var string  The parse mode of this Message. One of the PARSE_* class constants.
64
     */
65
    protected $parse;
66
67
    /**
68
     * Creates a new Slack Message instance.
69
     *
70
     * @param   array   $attributes
71
     */
72
    public function __construct(array $attributes = null)
73
    {
74
        if (!empty($attributes)) {
75
            $this->setAttributes($attributes);
76
        }
77
    }
78
79
    /**
80
     * Sets the attributes of this Message.
81
     *
82
     * @param   array   $attributes
83
     * @return  $this
84
     */
85
    public function setAttributes(array $attributes) : Message
86
    {
87
        if (isset($attributes['endpoint'])) {
88
            $this->setEndpoint($attributes['endpoint']);
89
        }
90
91
        if (isset($attributes['text'])) {
92
            $this->setText($attributes['text']);
93
        }
94
95
        if (isset($attributes['channel'])) {
96
            $this->setChannel($attributes['channel']);
97
        }
98
99
        if (isset($attributes['username'])) {
100
            $this->setUsername($attributes['username']);
101
        }
102
103
        if (isset($attributes['icon'])) {
104
            $this->setIcon($attributes['icon']);
105
        }
106
107
        if (isset($attributes['attachments'])) {
108
            foreach($attributes['attachments'] as $attachment) {
109
                $this->attach($attachment);
110
            }
111
        }
112
113
        if (isset($attributes['parse'])) {
114
            $this->setParseMode($attributes['parse']);
115
        }
116
117
        return $this;
118
    }
119
120
    /**
121
     * Returns the endpoint for this Message.
122
     *
123
     * Note: Only has effect when using the Slack Webhook Transport, not the Web API.
124
     *
125
     * @return  string
126
     */
127
    public function getEndpoint()
128
    {
129
        return $this->endpoint;
130
    }
131
132
    /**
133
     * Sets the endpoint for this Message.
134
     *
135
     * Note: Only has effect when using the Slack Webhook Transport, not the Web API.
136
     *
137
     * @param   string  $url
138
     * @return  $this
139
     */
140
    public function setEndpoint(string $url) : Message
141
    {
142
        $this->endpoint = $url;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Returns the text content of the Message.
149
     *
150
     * @return  string
151
     */
152
    public function getText()
153
    {
154
        return $this->text;
155
    }
156
157
    /**
158
     * Sets the text content of the Message.
159
     *
160
     * @param   string  $text
161
     * @return  $this
162
     */
163
    public function setText(string $text) : Message
164
    {
165
        $this->text = $text;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Returns the channel this Message shall be sent to.
172
     *
173
     * @return  string
174
     */
175
    public function getChannel()
176
    {
177
        return $this->channel;
178
    }
179
180
    /**
181
     * Sets the channel this Message shall be sent to.
182
     *
183
     * @param   string  $channel
184
     * @return  $this
185
     */
186
    public function setChannel(string $channel = null) : Message
187
    {
188
        $this->channel = $channel;
189
190
        return $this;
191
    }
192
193
    /**
194
     * Returns the name this Message shall be sent as.
195
     *
196
     * @return  string
197
     */
198
    public function getUsername()
199
    {
200
        return $this->username;
201
    }
202
203
    /**
204
     * Sets the name this Message shall be sent as.
205
     *
206
     * @param   string  $name
207
     * @return  $this
208
     */
209
    public function setUsername(string $name = null) : Message
210
    {
211
        $this->username = $name;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Returns the icon of the Message.
218
     *
219
     * @return  string
220
     */
221
    public function getIcon()
222
    {
223
        return $this->icon;
224
    }
225
226
    /**
227
     * Sets the icon of the Message.
228
     *
229
     * @param   string  $icon
230
     * @return  $this
231
     */
232
    public function setIcon(string $icon = null) : Message
233
    {
234
        $this->icon = $icon;
235
236
        return $this;
237
    }
238
239
    /**
240
     * Adds an Attachment to the Message.
241
     *
242
     * @param   callable|array|message\Attachment   $attachment When a callable is given, an Attachment will be created
243
     *                                                          and passed to the callable. With an array, an Attachment
244
     *                                                          will be constructed based on the array. Otherwise
245
     *                                                          an existing instance of an Attachment can be passed in.
246
     * @return  $this
247
     * @throws  \InvalidArgumentException                       When $attachment is neither of the supported types.
248
     */
249 View Code Duplication
    public function attach($attachment) : Message
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
    {
251
        // Let's try to resolve the input into something usable.
252
        if (!($attachment = $this->resolveAttachment($attachment)) instanceof message\Attachment) {
253
            throw new \InvalidArgumentException("Expected a callable, an array or an instance of ".message\Attachment::class.", got [".diagnostics\Debug::getTypeName($attachment)."] instead.");
254
        }
255
256
        $this->attachments[] = $attachment;
257
258
        return $this;
259
    }
260
261
    /**
262
     * Checks whether an Attachment with the specified $index is set.
263
     *
264
     * @return  bool
265
     */
266
    public function hasAttachment(int $index) : bool
267
    {
268
        return isset($this->attachments[$index]);
269
    }
270
271
    /**
272
     * Returns the Attachment at the given $index.
273
     *
274
     * @return  message\Attachment
275
     * @throws  \OutOfBoundsException
276
     */
277
    public function getAttachment(int $index) : message\Attachment
278
    {
279
        if (!isset($this->attachments[$index])) {
280
            throw new \OutOfBoundsException("No Attachment at the specified index [$index].");
281
        }
282
283
        return $this->attachments[$index];
284
    }
285
286
    /**
287
     * Sets the Attachment at the given $index. Overwrites, if applicable.
288
     *
289
     * @see     attach()
290
     *
291
     * @param   callable|array|message\Attachment   $attachment
292
     * @return  $this
293
     * @throws  \InvalidArgumentException
294
     */
295 View Code Duplication
    public function setAttachment(int $index, $attachment) : Message
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
296
    {
297
        // Let's try to resolve the input into something usable.
298
        if (!($attachment = $this->resolveAttachment($attachment)) instanceof message\Attachment) {
299
            throw new \InvalidArgumentException("Expected a callable, an array or an instance of ".message\Attachment::class.", got [".diagnostics\Debug::getTypeName($attachment)."] instead.");
300
        }
301
302
        $this->attachments[$index] = $attachment;
303
304
        return $this;
305
    }
306
307
    /**
308
     * Returns the Attachments of the Message.
309
     *
310
     * @return  message\Attachment[]
311
     */
312
    public function getAttachments() : array
313
    {
314
        return $this->attachments;
315
    }
316
317
    /**
318
     * Sets the Attachments of the Message.
319
     *
320
     * @param   message\Attachment[]    $attachments
321
     * @return  $this
322
     */
323
    public function setAttachments(array $attachments) : Message
324
    {
325
        $this->attachments = [];
326
327
        foreach ($attachments as $attachment) {
328
            $this->attach($attachment);
0 ignored issues
show
Documentation introduced by
$attachment is of type object<nyx\notify\transp...ack\message\Attachment>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
329
        }
330
331
        return $this;
332
    }
333
334
    /**
335
     * Returns the number of Attachments in this Message.
336
     *
337
     * @return  int
338
     */
339
    public function countAttachments() : int
340
    {
341
        return count($this->attachments);
342
    }
343
344
    /**
345
     * Returns the parse mode of this Message.
346
     *
347
     * @return  string
348
     */
349
    public function getParseMode()
350
    {
351
        return $this->parse;
352
    }
353
354
    /**
355
     * Sets the parse mode of this Message.
356
     *
357
     * @param   string  $mode               One of the PARSE_* class constants.
358
     * @return  $this
359
     * @throws  \InvalidArgumentException   When passing a $mode which is not one of the PARSE_* class constants.
360
     */
361
    public function setParseMode(string $mode = self::PARSE_DEFAULT) : Message
362
    {
363
        if (!in_array($mode, [self::PARSE_DEFAULT, self::PARSE_FULL, self::PARSE_NONE])) {
364
            throw new \InvalidArgumentException("Unknown parse mode requested [$mode].");
365
        }
366
367
        $this->parse = $mode;
368
369
        return $this;
370
    }
371
372
    /**
373
     * {@inheritDoc}
374
     */
375
    public function unserialize($data)
376
    {
377
        $this->setAttributes(unserialize($data));
378
    }
379
380
    /**
381
     * {@inheritDoc}
382
     */
383
    public function toArray() : array
384
    {
385
        $data = [
386
            'endpoint'    => $this->getEndpoint(),
387
            'text'        => $this->getText(),
388
            'channel'     => $this->getChannel(),
389
            'username'    => $this->getUsername(),
390
            'icon'        => $this->getIcon(),
391
            'parse'       => $this->getParseMode(),
392
            'attachments' => [],
393
            'link_names'  => 1
394
        ];
395
396
        foreach ($this->getAttachments() as $attachment) {
397
            $data['attachments'][] = $attachment->toArray();
398
        }
399
400
        return $data;
401
    }
402
403
    /**
404
     * Attempts to create a slack\Attachment instance based on the $attachment data given.
405
     *
406
     * @param   mixed   $attachment
407
     * @return  mixed                   Either an instantiated slack\Attachment, or a passthrough of the input data.
408
     */
409
    protected function resolveAttachment($attachment)
410
    {
411
        if (is_callable($attachment)) {
412
            $callable   = $attachment;
413
            $attachment = new message\Attachment;
414
415
            call_user_func($callable, $attachment);
416
417
            return $attachment;
418
        }
419
420
        if (is_array($attachment)) {
421
            return new message\Attachment($attachment);
422
        }
423
424
        return $attachment;
425
    }
426
}
427