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 (#44)
by Gadoma
05:21
created

Message::setEndpoint()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
1
<?php namespace Maknz\Slack;
2
3
use InvalidArgumentException;
4
5
class Message {
6
7
  /**
8
   * Reference to the Slack client responsible for sending
9
   * the message
10
   *
11
   * @var \Maknz\Slack\Client
12
   */
13
  protected $client;
14
15
  /**
16
   * The text to send with the message
17
   *
18
   * @var string
19
   */
20
  protected $text;
21
22
  /**
23
   * The target endpoint the message should be sent to
24
   *
25
   * @var string
26
   */
27
  protected $endpoint;
28
  
29
  /**
30
   * The channel the message should be sent to
31
   *
32
   * @var string
33
   */
34
  protected $channel;
35
36
  /**
37
   * The username the message should be sent as
38
   *
39
   * @var string
40
   */
41
  protected $username;
42
43
  /**
44
   * The URL to the icon to use
45
   *
46
   * @var string
47
   */
48
  protected $icon;
49
50
  /**
51
   * The type of icon we are using
52
   *
53
   * @var enum
54
   */
55
  protected $iconType;
56
57
  /**
58
   * Whether the message text should be interpreted in Slack's
59
   * Markdown-like language
60
   *
61
   * @var boolean
62
   */
63
  protected $allow_markdown = true;
64
65
  /**
66
   * The attachment fields which should be formatted with
67
   * Slack's Markdown-like language
68
   *
69
   * @var array
70
   */
71
  protected $markdown_in_attachments = [];
72
73
  /**
74
   * An array of attachments to send
75
   *
76
   * @var array
77
   */
78
  protected $attachments = [];
79
80
  /**
81
   *
82
   * @var string
83
   */
84
  const ICON_TYPE_URL = 'icon_url';
85
86
  /**
87
   *
88
   * @var string
89
   */
90
  const ICON_TYPE_EMOJI = 'icon_emoji';
91
92
  /**
93
   * Instantiate a new Message
94
   *
95
   * @param \Maknz\Slack\Client $client
96
   * @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...
97
   */
98
  public function __construct(Client $client)
99
  {
100
    $this->client = $client;
101
  }
102
103
  /**
104
   * Get the message text
105
   *
106
   * @return string
107
   */
108
  public function getText()
109
  {
110
    return $this->text;
111
  }
112
113
  /**
114
   * Set the message text
115
   *
116
   * @param string $text
117
   * @return $this
118
   */
119
  public function setText($text)
120
  {
121
    $this->text = $text;
122
123
    return $this;
124
  }
125
126
   /**
127
   * Get the message endpoint
128
   *
129
   * @return string
130
   */
131
  public function getEndpoint()
132
  {
133
    return $this->endpoint;
134
  }
135
136
  /**
137
   * Set the message endpoint
138
   *
139
   * @param string $endpoint
140
   * @return $this
141
   * @throws \InvalidArgumentException
142
   */
143
  public function setEndpoint($endpoint)
144
  {
145
    if(empty($endpoint) || !is_string($endpoint)) {
146
          throw new InvalidArgumentException('The message endpoint must be a non-empty string');
147
    }
148
    
149
    $this->endpoint = $endpoint;
150
151
    return $this;
152
  }
153
  
154
  /**
155
   * Get the channel we will post to
156
   *
157
   * @return string
158
   */
159
  public function getChannel()
160
  {
161
    return $this->channel;
162
  }
163
164
  /**
165
   * Set the channel we will post to
166
   *
167
   * @param string $channel
168
   * @return $this
169
   */
170
  public function setChannel($channel)
171
  {
172
    $this->channel = $channel;
173
174
    return $this;
175
  }
176
177
  /**
178
   * Get the username we will post as
179
   *
180
   * @return string
181
   */
182
  public function getUsername()
183
  {
184
    return $this->username;
185
  }
186
187
  /**
188
   * Set the username we will post as
189
   *
190
   * @param string $username
191
   * @return $this
192
   */
193
  public function setUsername($username)
194
  {
195
    $this->username = $username;
196
197
    return $this;
198
  }
199
200
  /**
201
   * Get the icon (either URL or emoji) we will post as
202
   *
203
   * @return string
204
   */
205
  public function getIcon()
206
  {
207
    return $this->icon;
208
  }
209
210
  /**
211
   * Set the icon (either URL or emoji) we will post as.
212
   *
213
   * @param string $icon
214
   * @return this
215
   */
216
  public function setIcon($icon)
217
  {
218
    if ($icon == null)
219
    {
220
      $this->icon = $this->iconType = null;
221
222
      return;
223
    }
224
225
    if (mb_substr($icon, 0, 1) == ":" && mb_substr($icon, mb_strlen($icon) - 1, 1) == ":")
226
    {
227
      $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 object<Maknz\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...
228
    }
229
230
    else
231
    {
232
      $this->iconType = self::ICON_TYPE_URL;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::ICON_TYPE_URL of type string is incompatible with the declared type object<Maknz\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...
233
    }
234
235
    $this->icon = $icon;
236
237
    return $this;
238
  }
239
240
  /**
241
   * Get the icon type being used, if an icon is set
242
   *
243
   * @return string
244
   */
245
  public function getIconType()
246
  {
247
    return $this->iconType;
248
  }
249
250
  /**
251
   * Get whether message text should be formatted with
252
   * Slack's Markdown-like language
253
   *
254
   * @return boolean
255
   */
256
  public function getAllowMarkdown()
257
  {
258
    return $this->allow_markdown;
259
  }
260
261
  /**
262
   * Set whether message text should be formatted with
263
   * Slack's Markdown-like language
264
   *
265
   * @param boolean $value
266
   * @return void
267
   */
268
  public function setAllowMarkdown($value)
269
  {
270
    $this->allow_markdown = (boolean) $value;
271
272
    return $this;
273
  }
274
275
  /**
276
   * Enable Markdown formatting for the message
277
   *
278
   * @return void
279
   */
280
  public function enableMarkdown()
281
  {
282
    $this->setAllowMarkdown(true);
283
284
    return $this;
285
  }
286
287
  /**
288
   * Disable Markdown formatting for the message
289
   *
290
   * @return void
291
   */
292
  public function disableMarkdown()
293
  {
294
    $this->setAllowMarkdown(false);
295
296
    return $this;
297
  }
298
299
  /**
300
   * Get the attachment fields which should be formatted
301
   * in Slack's Markdown-like language
302
   *
303
   * @return array
304
   */
305
  public function getMarkdownInAttachments()
306
  {
307
    return $this->markdown_in_attachments;
308
  }
309
310
  /**
311
   * Set the attachment fields which should be formatted
312
   * in Slack's Markdown-like language
313
   *
314
   * @param array $fields
315
   * @return void
316
   */
317
  public function setMarkdownInAttachments(array $fields)
318
  {
319
    $this->markdown_in_attachments = $fields;
320
321
    return $this;
322
  }
323
324
  /**
325
   * Change the name of the user the post will be made as
326
   *
327
   * @param string $username
328
   * @return $this
329
   */
330
  public function from($username)
331
  {
332
    $this->setUsername($username);
333
334
    return $this;
335
  }
336
337
  /**
338
   * Change the endpoint the post will be made to
339
   *
340
   * @param string $endpoint
341
   * @return $this
342
   */
343
  public function endpoint($endpoint)
344
  {
345
      return $this->setEndpoint($endpoint);
346
  }
347
  
348
  /**
349
   * Change the channel the post will be made to
350
   *
351
   * @param string $channel
352
   * @return $this
353
   */
354
  public function to($channel)
355
  {
356
    $this->setChannel($channel);
357
358
    return $this;
359
  }
360
361
  /**
362
   * Chainable method for setting the icon
363
   *
364
   * @param string $icon
365
   * @return $this
366
   */
367
  public function withIcon($icon)
368
  {
369
    $this->setIcon($icon);
370
371
    return $this;
372
  }
373
374
  /**
375
   * Add an attachment to the message
376
   *
377
   * @param mixed $attachment
378
   * @return $this
379
   * @throws \InvalidArgumentException
380
   */
381
  public function attach($attachment)
382
  {
383
    if ($attachment instanceof Attachment)
384
    {
385
      $this->attachments[] = $attachment;
386
387
      return $this;
388
    }
389
390
    elseif (is_array($attachment))
391
    {
392
      $attachmentObject = new Attachment($attachment);
393
394
      if ( ! isset($attachment['mrkdwn_in']))
395
      {
396
        $attachmentObject->setMarkdownFields($this->getMarkdownInAttachments());
397
      }
398
399
      $this->attachments[] = $attachmentObject;
400
401
      return $this;
402
    }
403
404
    throw new InvalidArgumentException('Attachment must be an instance of Maknz\\Slack\\Attachment or a keyed array');
405
  }
406
407
  /**
408
   * Get the attachments for the message
409
   *
410
   * @return array
411
   */
412
  public function getAttachments()
413
  {
414
    return $this->attachments;
415
  }
416
417
  /**
418
   * Set the attachments for the message
419
   *
420
   * @param string $attachments
421
   * @return $this
422
   */
423
  public function setAttachments(array $attachments)
424
  {
425
    $this->clearAttachments();
426
427
    foreach ($attachments as $attachment)
428
    {
429
      $this->attach($attachment);
430
    }
431
432
    return $this;
433
  }
434
435
  /**
436
   * Remove all attachments for the message
437
   *
438
   * @return $this
439
   */
440
  public function clearAttachments()
441
  {
442
    $this->attachments = [];
443
444
    return $this;
445
  }
446
447
  /**
448
   * Send the message
449
   *
450
   * @param string $text The text to send
451
   * @return void
452
   */
453
  public function send($text = null)
454
  {
455
    if ($text) $this->setText($text);
0 ignored issues
show
Bug Best Practice introduced by
The expression $text of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
456
    
457
    $this->client->sendMessage($this);
458
  }
459
460
}
461