Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
|
100.00% |
23 / 23 |
|
100.00% |
6 / 6 |
13 | |
100.00% |
1 / 1 |
|
getAuthorizeUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAccessTokenUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getScope | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUserProfile | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
8 | |||
getLogoSource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getThemeColor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Miniframe\SocialLogin\Provider; |
4 | |
5 | use Miniframe\SocialLogin\Model\User; |
6 | |
7 | class Linkedin extends AbstractOAuth2Provider |
8 | { |
9 | /** |
10 | * Returns the Authorize URL |
11 | * |
12 | * @return string |
13 | */ |
14 | protected function getAuthorizeUrl(): string |
15 | { |
16 | return 'https://www.linkedin.com/oauth/v2/authorization'; |
17 | } |
18 | |
19 | /** |
20 | * Returns the Access Token URL |
21 | * |
22 | * @return string |
23 | */ |
24 | protected function getAccessTokenUrl(): string |
25 | { |
26 | return 'https://www.linkedin.com/oauth/v2/accessToken'; |
27 | } |
28 | |
29 | /** |
30 | * Returns the requested scope |
31 | * |
32 | * @return string|null |
33 | */ |
34 | protected function getScope(): ?string |
35 | { |
36 | return 'r_liteprofile'; |
37 | } |
38 | |
39 | /** |
40 | * Returns the user profile |
41 | * |
42 | * @param array $accessToken The access token. |
43 | * |
44 | * @return User |
45 | */ |
46 | protected function getUserProfile(array $accessToken): User |
47 | { |
48 | $data = $this->curlRequest( |
49 | 'https://api.linkedin.com/v2/me?projection=(id,localizedFirstName,localizedLastName,' |
50 | . 'profilePicture(displayImage~:playableStreams))', |
51 | 'GET', |
52 | null, |
53 | [ |
54 | 'Authorization: Bearer ' . $accessToken['access_token'], |
55 | ] |
56 | ); |
57 | |
58 | // Tries to find a profile picture |
59 | $profilePicture = null; |
60 | if ( |
61 | isset($data['profilePicture']) |
62 | && isset($data['profilePicture']['displayImage~']) |
63 | && isset($data['profilePicture']['displayImage~']['elements']) |
64 | ) { |
65 | foreach ($data['profilePicture']['displayImage~']['elements'] as $profilePictureElement) { |
66 | if ( |
67 | isset($profilePictureElement['identifiers'][0]['identifier']) |
68 | && $profilePictureElement['identifiers'][0]['identifierType'] == 'EXTERNAL_URL' |
69 | ) { |
70 | $profilePicture = $profilePictureElement['identifiers'][0]['identifier']; |
71 | break; |
72 | } |
73 | } |
74 | } |
75 | |
76 | // When no profile picture can be found, use a default one |
77 | if ($profilePicture === null) { |
78 | $profilePicture = 'data:image/svg+xml;base64,' |
79 | . base64_encode(file_get_contents(__DIR__ . '/../../templates/unknown-user.svg')); |
80 | } |
81 | |
82 | return new User( |
83 | $data['id'], |
84 | $data['id'], |
85 | $data['localizedFirstName'] . ' ' . $data['localizedLastName'], |
86 | $profilePicture, |
87 | static::class, |
88 | ['accessToken' => $accessToken, 'userData' => $data] |
89 | ); |
90 | } |
91 | |
92 | /** |
93 | * Returns the image source for the logo of this provider. |
94 | * |
95 | * @return string |
96 | */ |
97 | public static function getLogoSource(): string |
98 | { |
99 | return 'data:image/svg+xml;base64,' |
100 | . base64_encode(file_get_contents(__DIR__ . '/../../templates/logos/Linkedin.svg')); |
101 | } |
102 | |
103 | /** |
104 | * Returns the theme color for this provider. |
105 | * |
106 | * @return string |
107 | */ |
108 | public static function getThemeColor(): string |
109 | { |
110 | return 'rgb(40,103,178)'; |
111 | } |
112 | } |