Completed
Push — master ( 97a5f4...919181 )
by Georgio
29:58 queued 28:20
created

Xing::getAuthorizationMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
/**
14
 * @see https://dev.xing.com/docs/authentication
15
 */
16
class Xing extends AbstractService
17
{
18
    public function __construct(
19
        CredentialsInterface $credentials,
20
        ClientInterface $httpClient,
21
        TokenStorageInterface $storage,
22
        $scopes = [],
23
        ?UriInterface $baseApiUri = null,
24
        $stateParameterInAutUrl = false,
25
        $apiVersion = ''
26
    ) {
27
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, $stateParameterInAutUrl, $apiVersion);
28
29
        if (null === $baseApiUri) {
30
            $this->baseApiUri = new Uri('https://api.xing.com/v1/');
31
        }
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getAuthorizationEndpoint()
38
    {
39
        return new Uri('https://api.xing.com/auth/oauth2/authorize');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getAccessTokenEndpoint()
46
    {
47
        return new Uri('https://api.xing.com/auth/oauth2/token');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function getAuthorizationMethod()
54
    {
55
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function parseAccessTokenResponse($responseBody)
62
    {
63
        $data = json_decode($responseBody, true);
64
65
        if (null === $data || !is_array($data)) {
66
            throw new TokenResponseException('Unable to parse response.');
67
        } elseif (isset($data['error'])) {
68
            throw new TokenResponseException(sprintf(
69
                    'Error in retrieving access token, error: "%s", description: "%s", uri: "%s".',
70
                    $data['error'],
71
                    $data['error_description'],
72
                    $data['error_uri']
73
                )
74
            );
75
        }
76
77
        $token = new StdOAuth2Token();
78
        $token->setAccessToken($data['access_token']);
79
        $token->setLifeTime($data['expires_in']);
80
        $token->setRefreshToken($data['refresh_token']);
81
82
        unset($data['access_token'], $data['expires_in'], $data['refresh_token']);
83
84
        $token->setExtraParams($data);
85
86
        return $token;
87
    }
88
}
89