1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\FacebookPoster; |
4
|
|
|
|
5
|
|
|
use Facebook\Facebook; |
6
|
|
|
use Illuminate\Notifications\Notification; |
7
|
|
|
use NotificationChannels\FacebookPoster\Attachments\Video; |
8
|
|
|
use NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent; |
9
|
|
|
|
10
|
|
|
class FacebookPosterChannel |
11
|
|
|
{ |
12
|
|
|
/** @var \Facebook\Facebook */ |
13
|
|
|
protected $facebook; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \Facebook\Facebook $facebook |
17
|
|
|
*/ |
18
|
|
|
public function __construct(Facebook $facebook) |
19
|
|
|
{ |
20
|
|
|
$this->facebook = $facebook; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Send the given notification. |
25
|
|
|
* |
26
|
|
|
* @param mixed $notifiable |
27
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
28
|
|
|
* |
29
|
|
|
* @throws InvalidPostContentException |
30
|
|
|
*/ |
31
|
|
|
public function send($notifiable, Notification $notification) |
32
|
|
|
{ |
33
|
|
|
$facebookMessage = $notification->toFacebookPoster($notifiable); |
|
|
|
|
34
|
|
|
|
35
|
|
|
$postBody = $facebookMessage->getPostBody(); |
36
|
|
|
|
37
|
|
|
$endpoint = $facebookMessage->getApiEndpoint(); |
38
|
|
|
|
39
|
|
|
$this->guardAgainstInvalidPostContent($postBody); |
40
|
|
|
|
41
|
|
|
// here we check if post body has image or video to upload it first to facebook |
42
|
|
|
if (isset($postBody['media'])) { |
43
|
|
|
$endpoint = $postBody['media']->getApiEndpoint(); |
44
|
|
|
|
45
|
|
|
if ($postBody['media'] instanceof Video) { |
46
|
|
|
$postBody = array_merge($postBody, $postBody['media']->getData()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$method = $postBody['media']->getMethod(); |
50
|
|
|
|
51
|
|
|
$postBody['source'] = $this->facebook->{$method}($postBody['media']->getPath()); |
52
|
|
|
|
53
|
|
|
unset($postBody['media']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->facebook->post($endpoint, $postBody); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $postBody |
61
|
|
|
* |
62
|
|
|
* @throws \NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent |
63
|
|
|
*/ |
64
|
|
|
protected function guardAgainstInvalidPostContent($postBody) |
65
|
|
|
{ |
66
|
|
|
if (! is_null($postBody['message'])) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (isset($postBody['link'])) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (isset($postBody['media'])) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw InvalidPostContent::noContentSet(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.