Completed
Pull Request — master (#498)
by Dragonqos
04:00
created

EveOnline::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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