1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\SocialPost\SocialNetwork\Facebook; |
6
|
|
|
|
7
|
|
|
use Facebook\Facebook; |
8
|
|
|
use MartinGeorgiev\SocialPost\Message; |
9
|
|
|
use MartinGeorgiev\SocialPost\Publisher; |
10
|
|
|
use MartinGeorgiev\SocialPost\SocialNetwork\Enum; |
11
|
|
|
use MartinGeorgiev\SocialPost\SocialNetwork\Exception\FailureWhenPublishingMessage; |
12
|
|
|
use MartinGeorgiev\SocialPost\SocialNetwork\Exception\MessageNotIntendedForPublisher; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Provider for publishing on a Facebook page. Uses Facebook PHP SDK v5. |
16
|
|
|
* @see https://developers.facebook.com/docs/php/Facebook/5.0.0 |
17
|
|
|
* |
18
|
|
|
* @license https://opensource.org/licenses/MIT |
19
|
|
|
* @link https://github.com/martin-georgiev/social-post |
20
|
|
|
*/ |
21
|
|
|
class SDK5 implements Publisher |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Facebook |
25
|
|
|
*/ |
26
|
|
|
private $facebook; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $pageId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Facebook $facebook Ready to use instance of the Facebook PHP SDK |
35
|
|
|
* @param string $pageId Identifier of the page, on which the status update will be published |
36
|
|
|
*/ |
37
|
|
|
public function __construct(Facebook $facebook, string $pageId) |
38
|
|
|
{ |
39
|
|
|
$this->facebook = $facebook; |
40
|
|
|
$this->pageId = $pageId; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function canPublish(Message $message): bool |
44
|
|
|
{ |
45
|
|
|
$canPublish = !empty(array_intersect($message->getNetworksToPublishOn(), [Enum::ANY, Enum::FACEBOOK])); |
46
|
|
|
|
47
|
|
|
return $canPublish; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function publish(Message $message): bool |
51
|
|
|
{ |
52
|
|
|
if (!$this->canPublish($message)) { |
53
|
|
|
throw new MessageNotIntendedForPublisher(Enum::FACEBOOK); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
try { |
57
|
|
|
$publishPostEndpoint = '/'.$this->pageId.'/feed'; |
58
|
|
|
$response = $this->facebook->post( |
59
|
|
|
$publishPostEndpoint, |
60
|
|
|
$this->prepareParams($message) |
61
|
|
|
); |
62
|
|
|
$post = $response->getGraphNode(); |
63
|
|
|
|
64
|
|
|
return isset($post['id']) ? !empty($post['id']) : false; |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
throw new FailureWhenPublishingMessage($e); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function prepareParams(Message $message): array |
71
|
|
|
{ |
72
|
|
|
$params = []; |
73
|
|
|
|
74
|
|
|
$params['message'] = $message->getMessage(); |
75
|
|
|
|
76
|
|
|
if (filter_var($message->getLink(), FILTER_VALIDATE_URL) !== false) { |
77
|
|
|
$params['link'] = $message->getLink(); |
78
|
|
|
} |
79
|
|
|
if (filter_var($message->getPictureLink(), FILTER_VALIDATE_URL) !== false) { |
80
|
|
|
$params['picture'] = $message->getPictureLink(); |
81
|
|
|
} |
82
|
|
|
if (!empty($message->getCaption())) { |
83
|
|
|
$params['caption'] = $message->getCaption(); |
84
|
|
|
} |
85
|
|
|
if (!empty($message->getDescription())) { |
86
|
|
|
$params['description'] = $message->getDescription(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $params; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|