HappyrLinkedInApiClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MartinGeorgiev\SocialPost\SocialNetwork\LinkedIn;
6
7
use Happyr\LinkedIn\LinkedIn;
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 LinkedIn page.
16
 * @see https://developer.linkedin.com/docs/company-pages
17
 * @see https://github.com/Happyr/LinkedIn-API-client
18
 *
19
 * @license https://opensource.org/licenses/MIT
20
 * @link https://github.com/martin-georgiev/social-post
21
 */
22
class HappyrLinkedInApiClient implements Publisher
23
{
24
    /**
25
     * @var LinkedIn
26
     */
27
    private $linkedIn;
28
29
    /**
30
     * @var string
31
     */
32
    private $companyPageId;
33
34
    /**
35
     * @param LinkedIn $linkedIn Ready to use instance of the Happyr's LinkedIn API client
36
     * @param string $accessToken Access token for a user with administrative rights of the page
37
     * @param string $companyPageId Identifier of the company page, on which the share will be published
38
     */
39
    public function __construct(LinkedIn $linkedIn, string $accessToken, string $companyPageId)
40
    {
41
        $linkedIn->setAccessToken($accessToken);
42
        $this->linkedIn = $linkedIn;
43
        $this->companyPageId = $companyPageId;
44
    }
45
46
    public function canPublish(Message $message): bool
47
    {
48
        $canPublish = !empty(array_intersect($message->getNetworksToPublishOn(), [Enum::ANY, Enum::LINKEDIN]));
49
50
        return $canPublish;
51
    }
52
53
    public function publish(Message $message): bool
54
    {
55
        if (!$this->canPublish($message)) {
56
            throw new MessageNotIntendedForPublisher(Enum::LINKEDIN);
57
        }
58
59
        try {
60
            $publishShareEndpoint = 'v1/companies/'.$this->companyPageId.'/shares';
61
            $options = ['json' => $this->prepareShareOptions($message)];
62
            $share = $this->linkedIn->post($publishShareEndpoint, $options);
63
64
            return isset($share['updateKey']) ? !empty($share['updateKey']) : false;
65
        } catch (\Exception $e) {
66
            throw new FailureWhenPublishingMessage($e);
67
        }
68
    }
69
70
    private function prepareShareOptions(Message $message): array
71
    {
72
        $share = [];
73
74
        $share['comment'] = $message->getMessage();
75
        $share['visibility']['code'] = 'anyone';
76
77
        if (filter_var($message->getLink(), FILTER_VALIDATE_URL) !== false) {
78
            $share['content']['submitted-url'] = $message->getLink();
79
        }
80
        if (filter_var($message->getPictureLink(), FILTER_VALIDATE_URL) !== false) {
81
            $share['content']['submitted-image-url'] = $message->getPictureLink();
82
        }
83
        if (!empty($message->getCaption())) {
84
            $share['content']['title'] = $message->getCaption();
85
        }
86
        if (!empty($message->getDescription())) {
87
            $share['content']['description'] = $message->getDescription();
88
        }
89
90
        return $share;
91
    }
92
}
93