Completed
Pull Request — master (#479)
by Andrey
03:22
created

Linkedin::parseAccessTokenResponse()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 4
nop 1
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
use OAuth\Common\Consumer\CredentialsInterface;
9
use OAuth\Common\Http\Client\ClientInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\Common\Http\Uri\UriInterface;
12
13
/**
14
 * Linkedin service.
15
 *
16
 * @author Antoine Corcy <[email protected]>
17
 * @link http://developer.linkedin.com/documents/authentication
18
 */
19
class Linkedin extends AbstractService
20
{
21
    /**
22
     * Defined scopes
23
     * @link http://developer.linkedin.com/documents/authentication#granting
24
     */
25
    const SCOPE_R_BASICPROFILE      = 'r_basicprofile';
26
    const SCOPE_R_FULLPROFILE       = 'r_fullprofile';
27
    const SCOPE_R_EMAILADDRESS      = 'r_emailaddress';
28
    const SCOPE_R_NETWORK           = 'r_network';
29
    const SCOPE_R_CONTACTINFO       = 'r_contactinfo';
30
    const SCOPE_RW_NUS              = 'rw_nus';
31
    const SCOPE_RW_COMPANY_ADMIN    = 'rw_company_admin';
32
    const SCOPE_RW_GROUPS           = 'rw_groups';
33
    const SCOPE_W_MESSAGES          = 'w_messages';
34
    const SCOPE_W_SHARE             = 'w_share';
35
36
    public function __construct(
37
        CredentialsInterface $credentials,
38
        ClientInterface $httpClient,
39
        TokenStorageInterface $storage,
40
        $scopes = array(),
41
        UriInterface $baseApiUri = null
42
    ) {
43
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
44
45
        if (null === $baseApiUri) {
46
            $this->baseApiUri = new Uri('https://api.linkedin.com/v1/');
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getAuthorizationEndpoint()
54
    {
55
        return new Uri('https://www.linkedin.com/uas/oauth2/authorization');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getAccessTokenEndpoint()
62
    {
63
        return new Uri('https://www.linkedin.com/uas/oauth2/accessToken');
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function getAuthorizationMethod()
70
    {
71
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function parseAccessTokenResponse($responseBody)
78
    {
79
        $data = json_decode($responseBody, true);
80
81
        if (null === $data || !is_array($data)) {
82
            throw new TokenResponseException('Unable to parse response.');
83
        } elseif (isset($data['error'])) {
84
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
85
        }
86
87
        $token = new StdOAuth2Token();
88
        $token->setAccessToken($data['access_token']);
89
        $token->setLifeTime($data['expires_in']);
90
91
        if (isset($data['refresh_token'])) {
92
            $token->setRefreshToken($data['refresh_token']);
93
            unset($data['refresh_token']);
94
        }
95
96
        unset($data['access_token']);
97
        unset($data['expires_in']);
98
99
        $token->setExtraParams($data);
100
101
        return $token;
102
    }
103
}
104