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 |
||
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) |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public function canPublish(Message $message): bool |
||
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 |
||
104 | } |
||
105 |