1<?php
2/**
3 * Contains EveOnline class.
4 * PHP version 5.4
5 * @copyright 2014 Michael Cummings
6 * @author    Michael Cummings <mgcummings@yahoo.com>
7 */
8namespace OAuth\OAuth2\Service;
9
10use OAuth\Common\Consumer\CredentialsInterface;
11use OAuth\Common\Http\Client\ClientInterface;
12use OAuth\Common\Http\Exception\TokenResponseException;
13use OAuth\Common\Http\Uri\Uri;
14use OAuth\Common\Http\Uri\UriInterface;
15use OAuth\Common\Storage\TokenStorageInterface;
16use OAuth\Common\Token\TokenInterface;
17use OAuth\OAuth2\Token\StdOAuth2Token;
18
19/**
20 * Class EveOnline
21 */
22class 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