Completed
Push — master ( 2b6771...5918d3 )
by Oleg
02:23
created

EveOnline::checkResponse()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 8.8571
cc 6
eloc 12
nc 4
nop 2
crap 6
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 18
    public function getBaseAccessTokenUrl(array $params)
55
    {
56 18
        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 15
    protected function checkResponse(ResponseInterface $response, $data)
92
    {
93 15
        if (isset($data['error']) || isset($data['exceptionType'])) {
94 6
            $message = $this->safeRead($data, 'error_description') || $this->safeRead($data, 'message');
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 91 can also be of type string; however, Evelabs\OAuth2\Client\Pr...r\EveOnline::safeRead() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
95 6
            throw new IdentityProviderException(
96 6
                $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...
97 6
                $response->getStatusCode(),
98
                $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...
99 4
            );
100
        }
101
102 9
        if (false === is_array($data)) {
103 3
            throw new IdentityProviderException(
104 3
                $response->getReasonPhrase(),
105 3
                $response->getStatusCode(),
106
                $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...
107 2
            );
108
        }
109 6
    }
110
111
    /**
112
     * Generates a resource owner object from a successful resource owner
113
     * details request.
114
     *
115
     * @param  array $response
116
     * @param  AccessToken $token
117
     * @return ResourceOwnerInterface
118
     */
119 3
    protected function createResourceOwner(array $response, AccessToken $token)
120
    {
121 3
        return new EveOnlineResourceOwner($response);
122
    }
123
124
    /**
125
     * Internal helper function to safe read from an array
126
     * @param array $array
127
     * @param string|int $key
128
     * @return null
129
     */
130 6
    private function safeRead(array $array, $key)
131
    {
132 6
        return !empty($array[$key]) ? $array[$key] : null;
133
    }
134
}
135