Completed
Push — master ( c695e2...2f421e )
by Chris
03:14 queued 01:27
created

Drupal::checkResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 2
nop 2
crap 3
1
<?php
2
3
namespace ChrisHemmings\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 Drupal extends AbstractProvider
13
{
14
    use BearerAuthorizationTrait;
15
16
    protected $baseUrl;
17
18 21
    public function getBaseUrl()
19
    {
20 21
        return $this->baseUrl;
21
    }
22
    
23
    /**
24
     * Get provider url to run authorization
25
     *
26
     * @return string
27
     */
28 6
    public function getBaseAuthorizationUrl()
29
    {
30 6
        return $this->getBaseUrl() . '/oauth2/authorize';
31
    }
32
33
    /**
34
     * Get provider url to fetch token
35
     *
36
     * @param AccessToken $token
0 ignored issues
show
Bug introduced by
There is no parameter named $token. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
37
     *
38
     * @return string
39
     */
40 12
    public function getBaseAccessTokenUrl(array $params)
41
    {
42 12
        return $this->getBaseUrl() . '/oauth2/token';
43
    }
44
45
    /**
46
     * Get provider url to fetch user details
47
     *
48
     * @param AccessToken $token
49
     *
50
     * @return string
51
     */
52 3
    public function getResourceOwnerDetailsUrl(AccessToken $token)
53
    {
54 3
        return $this->getBaseUrl() . '/oauth2/userInfo';
55
    }
56
57
    /**
58
     * @Override
59
     * Requests resource owner details.
60
     *
61
     * @param  AccessToken $token
62
     * @return mixed
63
     */
64 3
    protected function fetchResourceOwnerDetails(AccessToken $token)
65
    {
66 3
        $url = $this->getResourceOwnerDetailsUrl($token);
67 3
        $request = $this->getAuthenticatedRequest(self::METHOD_POST, $url, $token);
68
69 3
        return $this->getResponse($request);
70
    }
71
72
    /**
73
     * Get the default scopes used by this provider.
74
     *
75
     * @return array
76
     */
77 6
    protected function getDefaultScopes()
78
    {
79 6
        return ['openid', 'email', 'profile'];
80
    }
81
82
    /**
83
     * Returns the string that should be used to separate scopes when building
84
     * the URL for requesting an access token.
85
     *
86
     * @return string Scope separator, defaults to ' '
87
     */
88 6
    protected function getScopeSeparator()
89
    {
90 6
        return ' ';
91
    }
92
93
    /**
94
     * Check a provider response for errors.
95
     *
96
     * @param ResponseInterface $response
97
     * @param array|string $data
98
     *
99
     * @throws IdentityProviderException
100
     */
101 9
    protected function checkResponse(ResponseInterface $response, $data)
102
    {
103 9
        if ($response->getStatusCode() >= 400) {
104 3
            throw new IdentityProviderException(
105 3
                $data['error'] ?: $response->getReasonPhrase(),
106 3
                $response->getStatusCode(),
107
                $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...
108 2
            );
109
        }
110 6
    }
111
112
    /**
113
     * Generate a user object from a successful user details request.
114
     *
115
     * @param array $response
116
     * @param AccessToken $token
117
     *
118
     * @return League\OAuth2\Client\Provider\ResourceOwnerInterface
119
     */
120 3
    protected function createResourceOwner(array $response, AccessToken $token)
121
    {
122 3
        return new DrupalResourceOwner($response);
123
    }
124
}
125