LinkedIn   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 59
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDefaultScope() 0 3 1
A initUserAttributes() 0 3 1
A getTitle() 0 3 1
A applyAccessTokenToRequest() 0 6 1
A defaultNormalizeUserAttributeMap() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\AuthClient\Client;
6
7
use Psr\Http\Message\RequestInterface;
8
use Yiisoft\Yii\AuthClient\OAuth2;
9
use Yiisoft\Yii\AuthClient\OAuthToken;
10
use Yiisoft\Yii\AuthClient\RequestUtil;
11
12
/**
13
 * LinkedIn allows authentication via LinkedIn OAuth.
14
 *
15
 * In order to use linkedIn OAuth you must register your application at <https://www.linkedin.com/secure/developer>.
16
 *
17
 * @link https://developer.linkedin.com/docs/oauth2
18
 * @link https://www.linkedin.com/secure/developer
19
 * @link https://developer.linkedin.com/docs/rest-api
20
 */
21
final class LinkedIn extends OAuth2
22
{
23
    protected string $authUrl = 'https://www.linkedin.com/oauth/v2/authorization';
24
    protected string $tokenUrl = 'https://www.linkedin.com/oauth/v2/accessToken';
25
    protected string $endpoint = 'https://api.linkedin.com/v1';
26
    /**
27
     * @var array list of attribute names, which should be requested from API to initialize user attributes.
28
     */
29
    private array $attributeNames = [
30
        'id',
31
        'email-address',
32
        'first-name',
33
        'last-name',
34
        'public-profile-url',
35
    ];
36
37
    public function applyAccessTokenToRequest(RequestInterface $request, OAuthToken $accessToken): RequestInterface
38
    {
39
        return RequestUtil::addParams(
40
            $request,
41
            [
42
                'oauth2_access_token' => $accessToken->getToken(),
43
            ]
44
        );
45
    }
46
47
    /**
48
     * @return string service name.
49
     */
50
    public function getName(): string
51
    {
52
        return 'linkedin';
53
    }
54
55
    /**
56
     * @return string service title.
57
     */
58
    public function getTitle(): string
59
    {
60
        return 'LinkedIn';
61
    }
62
63
    protected function getDefaultScope(): string
64
    {
65
        return 'r_basicprofile r_emailaddress';
66
    }
67
68
    protected function defaultNormalizeUserAttributeMap(): array
69
    {
70
        return [
71
            'email' => 'email-address',
72
            'first_name' => 'first-name',
73
            'last_name' => 'last-name',
74
        ];
75
    }
76
77
    protected function initUserAttributes(): array
78
    {
79
        return $this->api('people/~:(' . implode(',', $this->attributeNames) . ')', 'GET');
80
    }
81
}
82