EveOnline::getAuthorizationMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Contains EveOnline class.
4
 * PHP version 5.4.
5
 *
6
 * @author    Michael Cummings <[email protected]>
7
 */
8
9
namespace OAuth\OAuth2\Service;
10
11
use OAuth\Common\Consumer\CredentialsInterface;
12
use OAuth\Common\Http\Client\ClientInterface;
13
use OAuth\Common\Http\Exception\TokenResponseException;
14
use OAuth\Common\Http\Uri\Uri;
15
use OAuth\Common\Http\Uri\UriInterface;
16
use OAuth\Common\Storage\TokenStorageInterface;
17
use OAuth\Common\Token\TokenInterface;
18
use OAuth\OAuth2\Token\StdOAuth2Token;
19
20
/**
21
 * Class EveOnline.
22
 */
23
class EveOnline extends AbstractService
24
{
25
    public function __construct(
26
        CredentialsInterface $credentials,
27
        ClientInterface $httpClient,
28
        TokenStorageInterface $storage,
29
        $scopes = [],
30
        ?UriInterface $baseApiUri = null
31
    ) {
32
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
33
34
        if (null === $baseApiUri) {
35
            $this->baseApiUri = new Uri('https://login.eveonline.com');
36
        }
37
    }
38
39
    /**
40
     * Returns the authorization API endpoint.
41
     *
42
     * @return UriInterface
43
     */
44
    public function getAuthorizationEndpoint()
45
    {
46
        return new Uri($this->baseApiUri . '/oauth/authorize');
47
    }
48
49
    /**
50
     * Returns the access token API endpoint.
51
     *
52
     * @return UriInterface
53
     */
54
    public function getAccessTokenEndpoint()
55
    {
56
        return new Uri($this->baseApiUri . '/oauth/token');
57
    }
58
59
    /**
60
     * Parses the access token response and returns a TokenInterface.
61
     *
62
     * @param string $responseBody
63
     *
64
     * @return TokenInterface
65
     */
66
    protected function parseAccessTokenResponse($responseBody)
67
    {
68
        $data = json_decode($responseBody, true);
69
70
        if (null === $data || !is_array($data)) {
71
            throw new TokenResponseException('Unable to parse response.');
72
        } elseif (isset($data['error_description'])) {
73
            throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
74
        } elseif (isset($data['error'])) {
75
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
76
        }
77
78
        $token = new StdOAuth2Token();
79
        $token->setAccessToken($data['access_token']);
80
        $token->setLifeTime($data['expires_in']);
81
82
        if (isset($data['refresh_token'])) {
83
            $token->setRefreshToken($data['refresh_token']);
84
            unset($data['refresh_token']);
85
        }
86
87
        unset($data['access_token'], $data['expires_in']);
88
89
        $token->setExtraParams($data);
90
91
        return $token;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function getAuthorizationMethod()
98
    {
99
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
100
    }
101
}
102