EveOnline::checkResponse()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 2
crap 4
1
<?php
2
3
namespace Evelabs\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
8
use League\OAuth2\Client\Token\AccessToken;
9
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
10
use Psr\Http\Message\ResponseInterface;
11
12
class EveOnline extends AbstractProvider
13
{
14
    use BearerAuthorizationTrait;
15
16
17
    /**
18
     * Default scopes
19
     *
20
     * @var array
21
     */
22
    public $defaultScopes = [];
23
24
    /**
25
     * Get the string used to separate scopes.
26
     *
27
     * @return string
28
     */
29 9
    protected function getScopeSeparator()
30
    {
31 9
        return ' ';
32
    }
33
34
    /**
35
     * Returns the base URL for authorizing a client.
36
     *
37
     * Eg. https://oauth.service.com/authorize
38
     *
39
     * @return string
40
     */
41 9
    public function getBaseAuthorizationUrl()
42
    {
43 9
        return 'https://login.eveonline.com/oauth/authorize';
44
    }
45
46
    /**
47
     * Returns the base URL for requesting an access token.
48
     *
49
     * Eg. https://oauth.service.com/token
50
     *
51
     * @param array $params
52
     * @return string
53
     */
54 21
    public function getBaseAccessTokenUrl(array $params)
55
    {
56 21
        return 'https://login.eveonline.com/oauth/token';
57
    }
58
59
    /**
60
     * Returns the URL for requesting the resource owner's details.
61
     *
62
     * @param AccessToken $token
63
     * @return string
64
     */
65 3
    public function getResourceOwnerDetailsUrl(AccessToken $token)
66
    {
67 3
        return 'https://login.eveonline.com/oauth/verify';
68
    }
69
70
    /**
71
     * Returns the default scopes used by this provider.
72
     *
73
     * This should only be the scopes that are required to request the details
74
     * of the resource owner, rather than all the available scopes.
75
     *
76
     * @return array
77
     */
78 6
    protected function getDefaultScopes()
79
    {
80 6
        return $this->defaultScopes;
81
    }
82
83
    /**
84
     * Checks a provider response for errors.
85
     *
86
     * @throws IdentityProviderException
87
     * @param  ResponseInterface $response
88
     * @param  array|string $data Parsed response data
89
     * @return void
90
     */
91 21
    protected function checkResponse(ResponseInterface $response, $data)
92
    {
93
        //not throwing anything for 2xx responses
94 21
        if (intval(substr($response->getStatusCode(), 0, 1)) === 2) {
95 12
            return;
96
        }
97
98 9
        $message = $this->safeRead($data, 'error_description') || $this->safeRead($data, 'message');
99 9
        throw new IdentityProviderException(
100 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...
101 9
            $response->getStatusCode(),
102
            $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...
103 6
        );
104
    }
105
106
    /**
107
     * Generates a resource owner object from a successful resource owner
108
     * details request.
109
     *
110
     * @param  array $response
111
     * @param  AccessToken $token
112
     * @return ResourceOwnerInterface
113
     */
114 3
    protected function createResourceOwner(array $response, AccessToken $token)
115
    {
116 3
        return new EveOnlineResourceOwner($response);
117
    }
118
119
    /**
120
     * Internal helper function to safe read from an array
121
     * @param mixed $array
122
     * @param string|int $key
123
     * @return null
124
     */
125 9
    private function safeRead($array, $key)
126
    {
127 9
        return !empty($array[$key]) ? $array[$key] : null;
128
    }
129
}
130