1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twitter; |
4
|
|
|
|
5
|
|
|
use Kylewm\Brevity\Brevity; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification; |
8
|
|
|
|
9
|
|
|
class TwitterStatusUpdate |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
protected $content; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $images; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Collection |
21
|
|
|
*/ |
22
|
|
|
public $imageIds; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $apiEndpoint = 'statuses/update'; |
28
|
|
|
|
29
|
|
|
/* |
30
|
|
|
* @param string $content |
31
|
|
|
*/ |
32
|
10 |
|
public function __construct($content) |
33
|
|
|
{ |
34
|
10 |
|
if ($exceededLength = $this->messageIsTooLong($content, new Brevity())) { |
35
|
2 |
|
throw CouldNotSendNotification::statusUpdateTooLong($exceededLength); |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
$this->content = $content; |
39
|
8 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set Twitter media files. |
43
|
|
|
* |
44
|
|
|
* @param array|string $images |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function withImage($images) |
48
|
|
|
{ |
49
|
3 |
|
collect($images)->each(function ($image) { |
50
|
3 |
|
$this->images[] = new TwitterImage($image); |
51
|
3 |
|
}); |
52
|
|
|
|
53
|
3 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get Twitter status update content. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
7 |
|
public function getContent() |
62
|
|
|
{ |
63
|
7 |
|
return $this->content; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get Twitter images list. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
6 |
|
public function getImages() |
72
|
|
|
{ |
73
|
6 |
|
return $this->images; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return Twitter status update api endpoint. |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
public function getApiEndpoint() |
81
|
|
|
{ |
82
|
3 |
|
return $this->apiEndpoint; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Build Twitter request body. |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
4 |
|
public function getRequestBody() |
90
|
|
|
{ |
91
|
|
|
$body = [ |
92
|
4 |
|
'status' => $this->getContent(), |
93
|
|
|
]; |
94
|
|
|
|
95
|
4 |
|
if ($this->imageIds) { |
96
|
2 |
|
$body['media_ids'] = $this->imageIds->implode(','); |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
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
|
10 |
|
private function messageIsTooLong($content, Brevity $brevity) |
109
|
|
|
{ |
110
|
10 |
|
$tweetLength = $brevity->tweetLength($content); |
111
|
10 |
|
$exceededLength = $tweetLength - 280; |
112
|
|
|
|
113
|
10 |
|
return $exceededLength > 0 ? $exceededLength : 0; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|