Completed
Pull Request — master (#2)
by
unknown
06:48
created

EveOnline::parseResponse()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 1
cts 1
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 5
nop 1
crap 6
1
<?php
2
3
namespace Evelabs\OAuth2\Client\Provider;
4
5
use Psr\Http\Message\ResponseInterface;
6
use League\OAuth2\Client\Token\AccessToken;
7
use League\OAuth2\Client\Provider\AbstractProvider;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
10
use Doctrine\Instantiator\Exception\UnexpectedValueException;
11
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
12
13
class EveOnline extends AbstractProvider
14
{
15
    use BearerAuthorizationTrait;
16
17
18
    /**
19
     * Default scopes
20
     *
21
     * @var array
22
     */
23
    public $defaultScopes = [];
24
25
    /**
26
     * Get the string used to separate scopes.
27
     *
28
     * @return string
29 9
     */
30
    protected function getScopeSeparator()
31 9
    {
32
        return ' ';
33
    }
34
35
    /**
36
     * Returns the base URL for authorizing a client.
37
     *
38
     * Eg. https://oauth.service.com/authorize
39
     *
40
     * @return string
41 9
     */
42
    public function getBaseAuthorizationUrl()
43 9
    {
44
        return 'https://login.eveonline.com/oauth/authorize';
45
    }
46
47
    /**
48
     * Returns the base URL for requesting an access token.
49
     *
50
     * Eg. https://oauth.service.com/token
51
     *
52
     * @param array $params
53
     * @return string
54 21
     */
55
    public function getBaseAccessTokenUrl(array $params)
56 21
    {
57
        return 'https://login.eveonline.com/oauth/token';
58
    }
59
60
    /**
61
     * Returns the URL for requesting the resource owner's details.
62
     *
63
     * @param AccessToken $token
64
     * @return string
65 3
     */
66
    public function getResourceOwnerDetailsUrl(AccessToken $token)
67 3
    {
68
        return 'https://login.eveonline.com/oauth/verify';
69
    }
70
71
    /**
72
     * Returns the default scopes used by this provider.
73
     *
74
     * This should only be the scopes that are required to request the details
75
     * of the resource owner, rather than all the available scopes.
76
     *
77
     * @return array
78 6
     */
79
    protected function getDefaultScopes()
80 6
    {
81
        return $this->defaultScopes;
82
    }
83
84
    /**
85
     * Checks a provider response for errors.
86
     *
87
     * @throws IdentityProviderException
88
     * @param  ResponseInterface $response
89
     * @param  array|string $data Parsed response data
90
     * @return void
91 21
     */
92
    protected function checkResponse(ResponseInterface $response, $data)
93
    {
94 21
        //not throwing anything for 2xx responses
95 12
        if (intval(substr($response->getStatusCode(), 0, 1)) === 2) {
96
            return;
97
        }
98 9
99 9
        $message = $this->safeRead($data, 'error_description') || $this->safeRead($data, 'message');
100 9
        throw new IdentityProviderException(
101 9
            $message ?: $response->getReasonPhrase(),
0 ignored issues
show
Bug introduced by
It seems like $message ?: $response->getReasonPhrase() can also be of type boolean; however, League\OAuth2\Client\Pro...xception::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
102
            $response->getStatusCode(),
103 6
            $response
0 ignored issues
show
Documentation introduced by
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a array|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
        );
105
    }
106
107
    /**
108
     * Generates a resource owner object from a successful resource owner
109
     * details request.
110
     *
111
     * @param  array $response
112
     * @param  AccessToken $token
113
     * @return ResourceOwnerInterface
114 3
     */
115
    protected function createResourceOwner(array $response, AccessToken $token)
116 3
    {
117
        return new EveOnlineResourceOwner($response);
118
    }
119
120
    /**
121
     * Parses the response according to its content-type header.
122
     *
123
     * @throws UnexpectedValueException
124
     * @param  ResponseInterface $response
125 9
     * @return array
126
     */
127 9
    protected function parseResponse(ResponseInterface $response)
128
    {
129
        $content = (string) $response->getBody();
130
131
        if (empty($content) && $response->getStatusCode() === 204) {
132
            return $content;
133
        }
134
135
        $type = $this->getContentType($response);
136
137
        if (strpos($type, 'urlencoded') !== false) {
138
            parse_str($content, $parsed);
139
            return $parsed;
140
        }
141
142
        // Attempt to parse the string as JSON regardless of content type,
143
        // since some providers use non-standard content types. Only throw an
144
        // exception if the JSON could not be parsed when it was expected to.
145
        try {
146
            return $this->parseJson($content);
147
        } catch (UnexpectedValueException $e) {
148
            if (strpos($type, 'json') !== false) {
149
                throw $e;
150
            }
151
152
            return $content;
153
        }
154
    }
155
156
    /**
157
     * Internal helper function to safe read from an array
158
     * @param mixed $array
159
     * @param string|int $key
160
     * @return null
161
     */
162
    private function safeRead($array, $key)
163
    {
164
        return !empty($array[$key]) ? $array[$key] : null;
165
    }
166
}
167