|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twitter; |
|
4
|
|
|
|
|
5
|
|
|
use Kylewm\Brevity\Brevity; |
|
6
|
|
|
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification; |
|
7
|
|
|
use NotificationChannels\Twitter\TwitterImage; |
|
8
|
|
|
|
|
9
|
|
|
class TwitterStatusUpdate |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** @var string */ |
|
13
|
|
|
protected $content; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private $images; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
public $imageIds; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
7 |
|
private $apiEndpoint = 'statuses/update'; |
|
29
|
|
|
|
|
30
|
7 |
|
/* |
|
31
|
7 |
|
* @param string $content |
|
32
|
7 |
|
*/ |
|
33
|
|
|
public function __construct($content) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($exceededLength = $this->messageIsTooLong($content, new Brevity())) { |
|
36
|
|
|
throw CouldNotSendNotification::statusUpdateTooLong($exceededLength); |
|
37
|
|
|
} |
|
38
|
|
|
$this->content = $content; |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
6 |
|
/** |
|
42
|
|
|
* Set Twitter media files. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array|string $images |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
|
|
public function withImage($images) |
|
48
|
|
|
{ |
|
49
|
5 |
|
collect($images)->each(function ($image) { |
|
50
|
|
|
$this->images[] = new TwitterImage($image); |
|
51
|
5 |
|
}); |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get Twitter status update content. |
|
58
|
3 |
|
* |
|
59
|
|
|
* @return string |
|
60
|
3 |
|
*/ |
|
61
|
|
|
public function getContent() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->content; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
4 |
|
* Get Twitter images list. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
4 |
|
*/ |
|
71
|
4 |
|
public function getImages() |
|
72
|
|
|
{ |
|
73
|
4 |
|
return $this->images; |
|
74
|
2 |
|
} |
|
75
|
2 |
|
|
|
76
|
|
|
/** |
|
77
|
4 |
|
* Return Twitter status update api endpoint. |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getApiEndpoint() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->apiEndpoint; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Build Twitter request body. |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getRequestBody() |
|
90
|
|
|
{ |
|
91
|
|
|
$body = [ |
|
92
|
|
|
'status' => $this->getContent(), |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
if ($this->imageIds) { |
|
|
|
|
|
|
96
|
|
|
$body['media_ids'] = $this->imageIds->implode(','); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $body; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Check if the message length is too long |
|
104
|
|
|
* @param $content |
|
105
|
|
|
* @param $brevity |
|
106
|
|
|
* @return int |
|
107
|
|
|
*/ |
|
108
|
|
|
private function messageIsTooLong($content, $brevity) |
|
109
|
|
|
{ |
|
110
|
|
|
$tweetLength = $brevity->tweetLength($content); |
|
111
|
|
|
$exceededLength = $tweetLength - 140; |
|
112
|
|
|
|
|
113
|
|
|
return $exceededLength > 0 ? $exceededLength : 0; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.