Wecounsel   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 124
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthenticatedRequest() 0 6 1
A getBaseAuthorizationUrl() 0 4 1
A getBaseAccessTokenUrl() 0 4 1
A getResourceOwnerDetailsUrl() 0 4 1
A getDefaultScopes() 0 4 1
A getHost() 0 4 1
A getScopeSeparator() 0 4 1
A checkResponse() 0 9 2
A createResourceOwner() 0 4 1
1
<?php
2
3
namespace Stevenmaguire\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Token\AccessToken;
8
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
9
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
10
use Psr\Http\Message\ResponseInterface;
11
12
class Wecounsel extends AbstractProvider
13
{
14
    use ArrayAccessorTrait,
15
        BearerAuthorizationTrait;
16
17
    /**
18
     * Wecounsel environment specific host; defaults to api
19
     *
20
     * @var string
21
     */
22
    protected $host = 'https://api.wecounsel.com';
23
24
    /**
25
     * Returns an authenticated PSR-7 request instance.
26
     *
27
     * @param  string $method
28
     * @param  string $url
29
     * @param  AccessToken|string $token
30
     * @param  array $options Any of "headers", "body", and "protocolVersion".
31
     * @return \Psr\Http\Message\RequestInterface
32
     */
33 6
    public function getAuthenticatedRequest($method, $url, $token, array $options = [])
34
    {
35 6
        $options['headers']['content-type'] = 'application/vnd.api+json';
36
37 6
        return $this->createRequest($method, $url, $token, $options);
38
    }
39
40
    /**
41
     * Get authorization url to begin OAuth flow
42
     *
43
     * @return string
44
     */
45 9
    public function getBaseAuthorizationUrl()
46
    {
47 9
        return $this->getHost() . '/oauth/authorize';
48
    }
49
50
    /**
51
     * Get access token url to retrieve token
52
     *
53
     * @return string
54
     */
55 12
    public function getBaseAccessTokenUrl(array $params)
56
    {
57 12
        return $this->getHost() . '/oauth/token';
58
    }
59
60
    /**
61
     * Get provider url to fetch user details
62
     *
63
     * @param  AccessToken $token
64
     *
65
     * @return string
66
     */
67 6
    public function getResourceOwnerDetailsUrl(AccessToken $token)
68
    {
69 6
        return $this->getHost() . '/v1/users/me';
70
    }
71
72
    /**
73
     * Get the default scopes used by this provider.
74
     *
75
     * This should not be a complete list of all scopes, but the minimum
76
     * required for the provider user interface!
77
     *
78
     * @return array
79
     */
80 6
    protected function getDefaultScopes()
81
    {
82 6
        return [];
83
    }
84
85
    /**
86
     * Returns a cleaned host.
87
     *
88
     * @return string
89
     */
90 27
    public function getHost()
91
    {
92 27
        return rtrim($this->host, '/');
93
    }
94
95
    /**
96
     * Returns the string that should be used to separate scopes when building
97
     * the URL for requesting an access token.
98
     *
99
     * @return string Scope separator, defaults to ' '
100
     */
101 9
    protected function getScopeSeparator()
102
    {
103 9
        return ' ';
104
    }
105
106
    /**
107
     * Check a provider response for errors.
108
     *
109
     * @throws IdentityProviderException
110
     * @param  ResponseInterface $response
111
     * @param  string $data Parsed response data
112
     * @return void
113
     */
114 9
    protected function checkResponse(ResponseInterface $response, $data)
115
    {
116
        // At the time of initial implementation the possible error payloads returned
117
        // by WeCounsel were not very well documented. This method will need some
118
        // improvement as the API continues to mature.
119 9
        if ($response->getStatusCode() != 200) {
120 3
            throw new IdentityProviderException('Unexpected response code', $response->getStatusCode(), $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...
121
        }
122 9
    }
123
124
    /**
125
     * Generate a user object from a successful user details request.
126
     *
127
     * @param object $response
128
     * @param AccessToken $token
129
     * @return WecounselResourceOwner
130
     */
131 3
    protected function createResourceOwner(array $response, AccessToken $token)
132
    {
133 3
        return new WecounselResourceOwner($response);
134
    }
135
}
136