Completed
Pull Request — master (#484)
by
unknown
04:41
created

EveOnline::getAuthorizationMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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