Completed
Push — feature/add-linkedin-support ( d0c362 )
by Martin
05:15
created

HappyrLinkedInApiClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 20.51 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 16
loc 78
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A canPublish() 0 5 1
A publish() 16 16 3
B prepareShare() 0 22 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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;
0 ignored issues
show
Bug introduced by
The property pageId does not seem to exist. Did you mean companyPageId?

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.

Loading history...
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
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        if (!$this->canPublish($message)) {
62
            throw new MessageNotIntendedForPublisher(SocialNetwork::LINKEDIN);
63
        }
64
65
        try {
66
            $publishShareEndpoint = 'v1/companies/' . $this->pageId. '/shares';
0 ignored issues
show
Bug introduced by
The property pageId does not seem to exist. Did you mean companyPageId?

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.

Loading history...
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