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); |
|
|
|
|
25
|
|
|
|
26
|
|
View Code Duplication |
if (isset($json_result['status']) && $json_result['status'] != 200) { |
|
|
|
|
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); |
|
|
|
|
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() |
|
|
|
|
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', |
|
|
|
|
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'])) { |
|
|
|
|
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
|
|
|
|
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.