1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twitter; |
4
|
|
|
|
5
|
|
|
use Abraham\TwitterOAuth\TwitterOAuth; |
6
|
|
|
use Illuminate\Notifications\Notifiable; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification; |
9
|
|
|
|
10
|
|
|
class TwitterChannel |
11
|
|
|
{ |
12
|
|
|
/** @var TwitterOAuth */ |
13
|
|
|
protected $twitter; |
14
|
|
|
|
15
|
|
|
/** @param TwitterOAuth $twitter */ |
16
|
3 |
|
public function __construct(TwitterOAuth $twitter) |
17
|
|
|
{ |
18
|
3 |
|
$this->twitter = $twitter; |
19
|
3 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Send the given notification. |
23
|
|
|
* |
24
|
|
|
* @param mixed $notifiable |
25
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
26
|
|
|
* @throws CouldNotSendNotification |
27
|
|
|
*/ |
28
|
3 |
|
public function send($notifiable, Notification $notification) |
29
|
|
|
{ |
30
|
3 |
|
$this->changeTwitterSettingsIfNeeded($notifiable); |
31
|
|
|
|
32
|
3 |
|
$twitterMessage = $notification->toTwitter($notifiable); |
|
|
|
|
33
|
3 |
|
$twitterMessage = $this->addImagesIfGiven($twitterMessage); |
34
|
|
|
|
35
|
3 |
|
$twitterApiResponse = $this->twitter->post($twitterMessage->getApiEndpoint(), $twitterMessage->getRequestBody($this->twitter), |
36
|
3 |
|
$twitterMessage->isJsonRequest); |
37
|
|
|
|
38
|
3 |
|
if ($this->twitter->getLastHttpCode() !== 200) { |
39
|
1 |
|
throw CouldNotSendNotification::serviceRespondsNotSuccessful($this->twitter->getLastBody()); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
return $twitterApiResponse; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Use per user settings instead of default ones. |
47
|
|
|
* |
48
|
|
|
* @param Notifiable $notifiable |
|
|
|
|
49
|
|
|
*/ |
50
|
3 |
|
private function changeTwitterSettingsIfNeeded($notifiable) |
51
|
|
|
{ |
52
|
3 |
|
if ($twitterSettings = $notifiable->routeNotificationFor('twitter')) { |
53
|
|
|
$this->twitter = new TwitterOAuth($twitterSettings[0], $twitterSettings[1], $twitterSettings[2], |
54
|
|
|
$twitterSettings[3]); |
55
|
|
|
} |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* If it is a status update message and images are provided, add them. |
60
|
|
|
* |
61
|
|
|
* @param $twitterMessage |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
3 |
|
private function addImagesIfGiven($twitterMessage) |
65
|
|
|
{ |
66
|
3 |
|
if (is_a($twitterMessage, TwitterStatusUpdate::class) && $twitterMessage->getImages()) { |
67
|
1 |
|
$this->twitter->setTimeouts(10, 15); |
68
|
|
|
|
69
|
1 |
|
$twitterMessage->imageIds = collect($twitterMessage->getImages())->map(function (TwitterImage $image) { |
70
|
1 |
|
$media = $this->twitter->upload('media/upload', ['media' => $image->getPath()]); |
71
|
|
|
|
72
|
1 |
|
return $media->media_id_string; |
73
|
1 |
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
return $twitterMessage; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.