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
25    const SCOPE_PUBLIC_DATA = 'publicData';
26
27    public function __construct(
28        CredentialsInterface $credentials,
29        ClientInterface $httpClient,
30        TokenStorageInterface $storage,
31        $scopes = array(),
32        UriInterface $baseApiUri = null
33    ) {
34        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
35
36        if (null === $baseApiUri) {
37            $this->baseApiUri = new Uri('https://login.eveonline.com');
38        }
39    }
40
41    /**
42     * Returns the authorization API endpoint.
43     * @return UriInterface
44     */
45    public function getAuthorizationEndpoint()
46    {
47        return new Uri($this->baseApiUri . '/v2/oauth/authorize');
48    }
49
50    /**
51     * Returns the access token API endpoint.
52     * @return UriInterface
53     */
54    public function getAccessTokenEndpoint()
55    {
56        return new Uri($this->baseApiUri . '/v2/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     * @throws TokenResponseException
66     */
67    protected function parseAccessTokenResponse($responseBody)
68    {
69        $data = json_decode($responseBody, true);
70
71        if (null === $data || !is_array($data)) {
72            throw new TokenResponseException('Unable to parse response.');
73        } elseif (isset($data['error_description'])) {
74            throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
75        } elseif (isset($data['error'])) {
76            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
77        }
78
79        $token = new StdOAuth2Token();
80        $token->setAccessToken($data['access_token']);
81        $token->setLifeTime($data['expires_in']);
82
83        if (isset($data['refresh_token'])) {
84            $token->setRefreshToken($data['refresh_token']);
85            unset($data['refresh_token']);
86        }
87
88        unset($data['access_token']);
89        unset($data['expires_in']);
90
91        $token->setExtraParams($data);
92
93        return $token;
94    }
95
96    /**
97     * {@inheritdoc}
98     */
99    protected function getAuthorizationMethod()
100    {
101        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
102    }
103}
104