1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\SocialPost\Provider\LinkedIn; |
6
|
|
|
|
7
|
|
|
use Happyr\LinkedIn\LinkedIn; |
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 LinkedIn page. |
17
|
|
|
* @see https://developer.linkedin.com/docs/company-pages |
18
|
|
|
* @see https://github.com/Happyr/LinkedIn-API-client |
19
|
|
|
* |
20
|
|
|
* @since 2.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 HappyrLinkedInApiClient implements SocialNetworkPublisher |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var LinkedIn |
29
|
|
|
*/ |
30
|
|
|
private $linkedIn; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $companyPageId; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param LinkedIn $linkedIn Ready to use instance of the Happyr's LinkedIn API client |
39
|
|
|
* @param string $accessToken Access token for a user with administrative rights of the page |
40
|
|
|
* @param string $companyPageId Identifier of the company page, on which the share will be published |
41
|
|
|
*/ |
42
|
|
|
public function __construct(LinkedIn $linkedIn, string $accessToken, string $companyPageId) |
43
|
|
|
{ |
44
|
|
|
$linkedIn->setAccessToken($accessToken); |
45
|
|
|
$this->linkedIn = $linkedIn; |
46
|
|
|
$this->companyPageId = $companyPageId; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function canPublish(Message $message): bool |
53
|
|
|
{ |
54
|
|
|
$canPublish = !empty(array_intersect($message->getNetworksToPublishOn(), [SocialNetwork::ANY, SocialNetwork::LINKEDIN])); |
55
|
|
|
return $canPublish; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function publish(Message $message): bool |
62
|
|
|
{ |
63
|
|
|
if (!$this->canPublish($message)) { |
64
|
|
|
throw new MessageNotIntendedForPublisher(SocialNetwork::LINKEDIN); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$publishShareEndpoint = 'v1/companies/' . $this->companyPageId. '/shares'; |
69
|
|
|
$options = ['json' => $this->prepareShare($message)]; |
70
|
|
|
$share = $this->linkedIn->post($publishShareEndpoint, $options); |
71
|
|
|
|
72
|
|
|
return isset($share['updateKey']) ? !empty($share['updateKey']) : false; |
73
|
|
|
} catch (Throwable $t) { |
74
|
|
|
throw new FailureWhenPublishingMessage($t); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Message $message |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function prepareShare(Message $message): array |
83
|
|
|
{ |
84
|
|
|
$share = []; |
85
|
|
|
|
86
|
|
|
$share['comment'] = $message->getMessage(); |
87
|
|
|
$share['visibility']['code'] = 'anyone'; |
88
|
|
|
|
89
|
|
|
if (filter_var($message->getLink(), FILTER_VALIDATE_URL) !== false) { |
90
|
|
|
$share['content']['submitted-url'] = $message->getLink(); |
91
|
|
|
} |
92
|
|
|
if (filter_var($message->getPictureLink(), FILTER_VALIDATE_URL) !== false) { |
93
|
|
|
$share['content']['submitted-image-url'] = $message->getPictureLink(); |
94
|
|
|
} |
95
|
|
|
if (!empty($message->getCaption())) { |
96
|
|
|
$share['content']['title'] = $message->getCaption(); |
97
|
|
|
} |
98
|
|
|
if (!empty($message->getDescription())) { |
99
|
|
|
$share['content']['description'] = $message->getDescription(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $share; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|