1 | <?php |
||
11 | class FacebookPosterPost |
||
12 | { |
||
13 | /** |
||
14 | * The post message. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $message; |
||
19 | |||
20 | /** |
||
21 | * The post link. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $link; |
||
26 | |||
27 | /** |
||
28 | * The post image. |
||
29 | * |
||
30 | * @var \NotificationChannels\FacebookPoster\Attachments\Image |
||
31 | */ |
||
32 | protected $image; |
||
33 | |||
34 | /** |
||
35 | * The post video. |
||
36 | * |
||
37 | * @var \NotificationChannels\FacebookPoster\Attachments\Video |
||
38 | */ |
||
39 | protected $video; |
||
40 | |||
41 | /** |
||
42 | * The post API endpoint. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $apiEndpoint = 'me/feed'; |
||
47 | |||
48 | /** |
||
49 | * Create a new post instance. |
||
50 | * |
||
51 | * @param string $message |
||
52 | * @return void |
||
53 | */ |
||
54 | public function __construct($message) |
||
55 | { |
||
56 | $this->message = $message; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get post message. |
||
61 | 7 | * |
|
62 | * @return string |
||
63 | 7 | */ |
|
64 | 7 | public function getMessage() |
|
65 | { |
||
66 | return $this->message; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Set facebook post main link. |
||
71 | 7 | * |
|
72 | * @param string $link |
||
73 | 7 | * |
|
74 | * @return $this |
||
75 | */ |
||
76 | public function withLink($link) |
||
77 | { |
||
78 | $this->link = $link; |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | 1 | /** |
|
84 | * Set a post image. |
||
85 | 1 | * |
|
86 | * @param string $path |
||
87 | 1 | * @param string $endpoint |
|
88 | * @return $this |
||
89 | */ |
||
90 | public function withImage($path, $endpoint = 'me/photos') |
||
91 | { |
||
92 | $this->image = new Image($path, $endpoint); |
||
93 | |||
94 | return $this; |
||
95 | } |
||
96 | |||
97 | 1 | /** |
|
98 | * Set a post video. |
||
99 | 1 | * |
|
100 | * @param string $path |
||
101 | 1 | * @param string $title |
|
102 | * @param string $description |
||
103 | * @param string $endpoint |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function withVideo($path, $title = null, $description = null, $endpoint = 'me/videos') |
||
107 | { |
||
108 | $this->video = new Video($path, $title, $description, $endpoint); |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | 1 | /** |
|
114 | * Schedule the post for a date in the future. |
||
115 | 1 | * |
|
116 | * @param \DateTimeInterface|int $timestamp |
||
117 | 1 | * @return $this |
|
118 | */ |
||
119 | public function scheduledFor($timestamp) |
||
120 | { |
||
121 | $timestamp = $timestamp instanceof DateTimeInterface |
||
122 | ? $timestamp->getTimestamp() |
||
123 | : $timestamp; |
||
124 | |||
125 | $this->published = false; |
||
126 | 3 | $t |
|
127 | $this->params['published'] = false; |
||
|
|||
128 | 3 | $this->params['scheduled_publish_time'] = $timestamp; |
|
129 | 2 | ||
130 | 3 | return $this; |
|
131 | } |
||
132 | 3 | ||
133 | 3 | /** |
|
134 | * Return Facebook Post api endpoint. |
||
135 | 3 | * |
|
136 | * @return string |
||
137 | */ |
||
138 | public function getApiEndpoint() |
||
139 | { |
||
140 | return $this->apiEndpoint; |
||
141 | } |
||
142 | |||
143 | 4 | public function getMedia() |
|
144 | { |
||
145 | 4 | return $this->image ?: $this->video; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Build Facebook post request body. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | 7 | public function getContent() |
|
154 | { |
||
155 | 7 | $this->validate(); |
|
156 | |||
157 | $body = [ |
||
158 | 7 | 'message' => $this->getMessage(), |
|
159 | ]; |
||
160 | |||
161 | 7 | if ($this->link != null) { |
|
162 | 1 | $body['link'] = $this->link; |
|
163 | } |
||
164 | |||
165 | 7 | if ($this->params != null) { |
|
166 | 1 | $body = array_merge($body, $this->params); |
|
167 | } |
||
168 | |||
169 | 7 | return $body; |
|
170 | 1 | } |
|
171 | |||
172 | /** |
||
173 | 7 | * Validate that there is acceptable post content. |
|
174 | 3 | * |
|
175 | * @throws \NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent |
||
176 | */ |
||
177 | 7 | protected function validate() |
|
178 | { |
||
179 | if ($this->message || $this->link || $this->media) { |
||
180 | return; |
||
181 | } |
||
182 | |||
183 | throw InvalidPostContent::noContentSet(); |
||
184 | } |
||
185 | } |
||
186 |