Linkedin   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 171
Duplicated Lines 28.07 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 8
dl 48
loc 171
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B request() 16 28 4
B post() 0 33 2
B getProfile() 24 24 1
A getStats() 0 6 1
A getPermissions() 0 4 1
B getPages() 8 27 3
B getGroups() 0 35 4

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
namespace Borfast\Socializr\Connectors;
4
5
use Borfast\Socializr\Exceptions\ExpiredTokenException;
6
use Borfast\Socializr\Exceptions\GenericPostingException;
7
use Borfast\Socializr\Post;
8
use Borfast\Socializr\Profile;
9
use Borfast\Socializr\Page;
10
use Borfast\Socializr\Group;
11
use Borfast\Socializr\Response;
12
13
class Linkedin extends AbstractConnector
14
{
15
    public static $provider = 'linkedin';
16
17
    public function request($path, $method = 'GET', $params = [], $headers = [])
18
    {
19
        $headers['Content-Type'] = 'application/json';
20
        $result = parent::request($path, $method, $params, $headers);
21
22
        $json_result = json_decode($result, true);
23
24
        // dd($json_result);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
26 View Code Duplication
        if (isset($json_result['status']) && $json_result['status'] != 200) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
27
            $msg = 'Error accessing LinkedIn API. Status: %s. Error code: %s. Message: %s';
28
            $msg = sprintf(
29
                $msg,
30
                $json_result['status'],
31
                $json_result['errorCode'],
32
                $json_result['message']
33
            );
34
35
            if ($json_result['status'] == '401') {
36
                throw new ExpiredTokenException($msg);
37
            } else {
38
                throw new GenericPostingException($msg);
39
            }
40
41
        }
42
43
        return $result;
44
    }
45
46
47
    public function post(Post $post)
48
    {
49
        $path = '/people/~/shares?format=json';
50
        $method = 'POST';
51
        $params = [
52
            'visibility' => [
53
                'code' => 'anyone'
54
            ],
55
            'comment' => '',
56
            'content' => [
57
                'title' => $post->title,
58
                'submitted-url' => $post->url,
59
                'description' => $post->body,
60
            ]
61
        ];
62
63
        if (!empty($post->media)) {
64
            $params['content']['submitted-image-url'] = $post->media[0];
65
        }
66
67
        $params = json_encode($params);
68
69
        $result = $this->request($path, $method, $params);
0 ignored issues
show
Documentation introduced by
$params is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
71
        $response = new Response;
72
        $response->setRawResponse($result); // This is already JSON.
73
        $response->setProvider(static::$provider);
74
        $result_json = json_decode($result);
75
        $response->setPostId($result_json->updateKey);
76
        $response->setPostUrl($result_json->updateUrl);
77
78
        return $response;
79
    }
80
81
82 View Code Duplication
    public function getProfile()
0 ignored issues
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...
83
    {
84
        $path = '/people/~:(id,first-name,last-name,maiden-name,public-profile-url,formatted-name,num-connections,email-address,num-recommenders)?format=json';
85
        $result = $this->request($path);
86
        $json_result = json_decode($result, true);
87
88
        $mapping = [
89
            'id' => 'id',
90
            'email' => 'emailAddress',
91
            'name' => 'formattedName',
92
            'first_name' => 'firstName',
93
            'middle_name' => 'maidenName',
94
            'last_name' => 'lastName',
95
            // 'username' => 'username',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
            'link' => 'publicProfileUrl',
97
            'likes' => 'numConnections'
98
        ];
99
100
        $profile = Profile::create($mapping, $json_result);
101
        $profile->provider = static::$provider;
102
        $profile->raw_response = $result;
103
104
        return $profile;
105
    }
106
107
    public function getStats()
108
    {
109
        $profile = $this->getProfile();
110
111
        return $profile->likes;
112
    }
113
114
    public function getPermissions()
115
    {
116
        return null;
117
    }
118
119
    public function getPages()
120
    {
121
        $path = '/companies:(id,name,universal-name,square-logo-url,num-followers)?is-company-admin=true&format=json';
122
        $result = $this->request($path);
123
        $json_result = json_decode($result, true);
124
125
        $pages = [];
126
127
        $mapping = [
128
            'id' => 'id',
129
            'name' => 'name',
130
            'picture' => 'squareLogoUrl',
131
            'link' => 'publicProfileUrl'
132
        ];
133
134
        // Make th epage IDs available as the array keys and get their picture
135 View Code Duplication
        if (!empty($json_result['values'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
136
            foreach ($json_result['values'] as $company) {
137
                $pages[$company['id']] = Page::create($mapping, $company);
138
                $pages[$company['id']]->link = 'http://www.linkedin.com/company/'.$company['universalName'];
139
                $pages[$company['id']]->provider = static::$provider;
140
                $pages[$company['id']]->raw_response = $result;
141
            }
142
        }
143
144
        return $pages;
145
    }
146
147
148
    public function getGroups()
149
    {
150
        $path = '/people/~/group-memberships:(group:(id,name,site-group-url,small-logo-url,num-members,relation-to-viewer))?&format=json&count=999';
151
        $response = $this->request($path);
152
        $groups = json_decode($response, true);
153
154
        $group_pages = [];
155
156
        $mapping = [
157
            'id' => 'id',
158
            'name' => 'name',
159
            'picture' => 'smallLogoUrl',
160
            'link' => 'siteGroupUrl'
161
        ];
162
163
        // Make the page IDs available as the array keys and get their picture
164
        if (!empty($groups['values'])) {
165
            foreach ($groups['values'] as $group) {
166
                $group_pages[$group['_key']] = Group::create($mapping, $group['group']);
167
                $group_pages[$group['_key']]->provider = static::$provider;
168
                $group_pages[$group['_key']]->raw_response = $response;
169
170
                // Let's check if our user can post to this group.
171
                // Thank you for this wonder, LinkedIn! It's so fun parsing infinitely nested arrays...
172
                $actions = $group['group']['relationToViewer']['availableActions']['values'];
173
                array_walk($actions, function ($value) use ($group, $group_pages) {
174
                    if ($value['code'] === 'add-post') {
175
                        $group_pages[$group['_key']]->can_post = true;
176
                    }
177
                });
178
            }
179
        }
180
181
        return $group_pages;
182
    }
183
}
184