These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace NotificationChannels\FacebookPoster; |
||
4 | |||
5 | use DateTimeInterface; |
||
6 | use NotificationChannels\FacebookPoster\Attachments\Link; |
||
7 | use NotificationChannels\FacebookPoster\Attachments\Image; |
||
8 | use NotificationChannels\FacebookPoster\Attachments\Video; |
||
9 | use NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent; |
||
10 | |||
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 | * Additional post parameters. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $params; |
||
47 | |||
48 | /** |
||
49 | * The post API endpoint. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $apiEndpoint = 'me/feed'; |
||
54 | |||
55 | /** |
||
56 | * Create a new post instance. |
||
57 | * |
||
58 | * @param string $message |
||
59 | * @return void |
||
60 | */ |
||
61 | 7 | public function __construct($message) |
|
62 | { |
||
63 | 7 | $this->message = $message; |
|
64 | 7 | } |
|
65 | |||
66 | /** |
||
67 | * Get post message. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | 7 | public function getMessage() |
|
72 | { |
||
73 | 7 | return $this->message; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Set facebook post main link. |
||
78 | * |
||
79 | * @param string $link |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | 1 | public function withLink($link) |
|
84 | { |
||
85 | 1 | $this->link = $link; |
|
86 | |||
87 | 1 | return $this; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Set a post image. |
||
92 | * |
||
93 | * @param string $path |
||
94 | * @param string $endpoint |
||
95 | * @return $this |
||
96 | */ |
||
97 | 1 | public function withImage($path, $endpoint = 'me/photos') |
|
98 | { |
||
99 | 1 | $this->image = new Image($path, $endpoint); |
|
100 | |||
101 | 1 | return $this; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Set a post video. |
||
106 | * |
||
107 | * @param string $path |
||
108 | * @param string $title |
||
109 | * @param string $description |
||
110 | * @param string $endpoint |
||
111 | * @return $this |
||
112 | */ |
||
113 | 1 | public function withVideo($path, $title = null, $description = null, $endpoint = 'me/videos') |
|
114 | { |
||
115 | 1 | $this->video = new Video($path, $title, $description, $endpoint); |
|
116 | |||
117 | 1 | return $this; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Schedule the post for a date in the future. |
||
122 | * |
||
123 | * @param \DateTimeInterface|int $timestamp |
||
124 | * @return $this |
||
125 | */ |
||
126 | 3 | public function scheduledFor($timestamp) |
|
127 | { |
||
128 | 3 | $timestamp = $timestamp instanceof DateTimeInterface |
|
129 | 2 | ? $timestamp->getTimestamp() |
|
130 | 3 | : $timestamp; |
|
131 | |||
132 | 3 | $this->params['published'] = false; |
|
133 | 3 | $this->params['scheduled_publish_time'] = $timestamp; |
|
134 | |||
135 | 3 | return $this; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Return Facebook Post api endpoint. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 4 | public function getApiEndpoint() |
|
144 | { |
||
145 | 4 | return $this->apiEndpoint; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Build Facebook post request body. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | 7 | public function getPostBody() |
|
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->image != null) { |
|
166 | 1 | $body['media'] = $this->image; |
|
167 | } |
||
168 | |||
169 | 7 | if ($this->video != null) { |
|
170 | 1 | $body['media'] = $this->video; |
|
171 | } |
||
172 | |||
173 | 7 | if ($this->params != null) { |
|
174 | 3 | $body = array_merge($body, $this->params); |
|
175 | } |
||
176 | |||
177 | 7 | return $body; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Validate that there is acceptable post content. |
||
182 | * |
||
183 | * @throws \NotificationChannels\FacebookPoster\Exceptions\InvalidPostContent |
||
184 | */ |
||
185 | 7 | protected function validate() |
|
186 | { |
||
187 | 7 | if ($this->message || $this->link || $this->media) { |
|
0 ignored issues
–
show
|
|||
188 | 7 | return; |
|
189 | } |
||
190 | |||
191 | throw InvalidPostContent::noContentSet(); |
||
192 | } |
||
193 | } |
||
194 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: