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 $companyPageId Identifier of the company page, on which the share will be published |
40
|
|
|
*/ |
41
|
|
|
public function __construct(LinkedIn $linkedIn, string $companyPageId) |
42
|
|
|
{ |
43
|
|
|
$this->linkedIn = $linkedIn; |
44
|
|
|
$this->pageId = $companyPageId; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function canPublish(Message $message): bool |
51
|
|
|
{ |
52
|
|
|
$canPublish = !empty(array_intersect($message->getNetworksToPublishOn(), [SocialNetwork::ANY, SocialNetwork::LINKEDIN])); |
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::LINKEDIN); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$publishShareEndpoint = 'v1/companies/' . $this->pageId. '/shares'; |
|
|
|
|
67
|
|
|
$options = ['json' => $this->prepareShare($message)]; |
68
|
|
|
$share = $this->linkedIn->post($publishShareEndpoint, $options); |
69
|
|
|
|
70
|
|
|
return !empty($share['updateKey']); |
71
|
|
|
} catch (Throwable $t) { |
72
|
|
|
throw new FailureWhenPublishingMessage($t); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Message $message |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function prepareShare(Message $message): array |
81
|
|
|
{ |
82
|
|
|
$share = []; |
83
|
|
|
|
84
|
|
|
$share['comment'] = $message->getMessage(); |
85
|
|
|
$share['visibility']['code'] = 'anyone'; |
86
|
|
|
|
87
|
|
|
if (filter_var($message->getLink(), FILTER_VALIDATE_URL) !== false) { |
88
|
|
|
$share['content']['submitted-url'] = $message->getLink(); |
89
|
|
|
} |
90
|
|
|
if (filter_var($message->getPictureLink(), FILTER_VALIDATE_URL) !== false) { |
91
|
|
|
$share['content']['submitted-image-url'] = $message->getPictureLink(); |
92
|
|
|
} |
93
|
|
|
if (!empty($message->getCaption())) { |
94
|
|
|
$share['content']['title'] = $message->getCaption(); |
95
|
|
|
} |
96
|
|
|
if (!empty($message->getDescription())) { |
97
|
|
|
$share['content']['description'] = $message->getDescription(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $share; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.