Completed
Pull Request — master (#498)
by Dragonqos
04:53 queued 02:07
created

Linkedin   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 83
rs 10
c 2
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
B parseAccessTokenResponse() 0 26 5
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
9
/**
10
 * Linkedin service.
11
 *
12
 * @author Antoine Corcy <[email protected]>
13
 * @link http://developer.linkedin.com/documents/authentication
14
 */
15
class Linkedin extends AbstractService
16
{
17
    /**
18
     * Defined scopes
19
     * @link http://developer.linkedin.com/documents/authentication#granting
20
     */
21
    const SCOPE_R_BASICPROFILE      = 'r_basicprofile';
22
    const SCOPE_R_FULLPROFILE       = 'r_fullprofile';
23
    const SCOPE_R_EMAILADDRESS      = 'r_emailaddress';
24
    const SCOPE_R_NETWORK           = 'r_network';
25
    const SCOPE_R_CONTACTINFO       = 'r_contactinfo';
26
    const SCOPE_RW_NUS              = 'rw_nus';
27
    const SCOPE_RW_COMPANY_ADMIN    = 'rw_company_admin';
28
    const SCOPE_RW_GROUPS           = 'rw_groups';
29
    const SCOPE_W_MESSAGES          = 'w_messages';
30
    const SCOPE_W_SHARE             = 'w_share';
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function init()
36
    {
37
        $this->stateParameterInAuthUrl = true;
38
39
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
40
            $this->baseApiUri = new Uri('https://api.linkedin.com/v1/');
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getAuthorizationEndpoint()
48
    {
49
        return new Uri('https://www.linkedin.com/uas/oauth2/authorization');
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getAccessTokenEndpoint()
56
    {
57
        return new Uri('https://www.linkedin.com/uas/oauth2/accessToken');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function getAuthorizationMethod()
64
    {
65
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function parseAccessTokenResponse($responseBody)
72
    {
73
        $data = json_decode($responseBody, true);
74
75
        if (null === $data || !is_array($data)) {
76
            throw new TokenResponseException('Unable to parse response.');
77
        } elseif (isset($data['error'])) {
78
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
79
        }
80
81
        $token = new StdOAuth2Token();
82
        $token->setAccessToken($data['access_token']);
83
        $token->setLifeTime($data['expires_in']);
84
85
        if (isset($data['refresh_token'])) {
86
            $token->setRefreshToken($data['refresh_token']);
87
            unset($data['refresh_token']);
88
        }
89
90
        unset($data['access_token']);
91
        unset($data['expires_in']);
92
93
        $token->setExtraParams($data);
94
95
        return $token;
96
    }
97
}
98