|
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 |
|
$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
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Use per user settings instead of default ones. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Notifiable $notifiable |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
3 |
|
private function changeTwitterSettingsIfNeeded($notifiable) |
|
49
|
|
|
{ |
|
50
|
3 |
|
if ($twitterSettings = $notifiable->routeNotificationFor('twitter')) { |
|
51
|
|
|
$this->twitter = new TwitterOAuth($twitterSettings[0], $twitterSettings[1], $twitterSettings[2], |
|
52
|
|
|
$twitterSettings[3]); |
|
53
|
|
|
} |
|
54
|
3 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* If it is a status update message and images are provided, add them. |
|
58
|
|
|
* |
|
59
|
|
|
* @param $twitterMessage |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
3 |
|
private function addImagesIfGiven($twitterMessage) |
|
63
|
|
|
{ |
|
64
|
3 |
|
if (is_a($twitterMessage, TwitterStatusUpdate::class) && $twitterMessage->getImages()) { |
|
65
|
1 |
|
$this->twitter->setTimeouts(10, 15); |
|
66
|
|
|
|
|
67
|
1 |
|
$twitterMessage->imageIds = collect($twitterMessage->getImages())->map(function (TwitterImage $image) { |
|
68
|
1 |
|
$media = $this->twitter->upload('media/upload', ['media' => $image->getPath()]); |
|
69
|
|
|
|
|
70
|
1 |
|
return $media->media_id_string; |
|
71
|
1 |
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $twitterMessage; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
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.