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