Completed
Pull Request — master (#3)
by
unknown
02:25
created

Zendesk::checkResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php namespace Stevenmaguire\OAuth2\Client\Provider;
2
3
use League\OAuth2\Client\Provider\AbstractProvider;
4
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
5
use League\OAuth2\Client\Token\AccessToken;
6
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
7
use Psr\Http\Message\ResponseInterface;
8
9
class Zendesk extends AbstractProvider
10
{
11
    use BearerAuthorizationTrait;
12
13
    protected $subdomain;
14
15
    protected $scopes = [];
16
17
    /**
18
     * @var string Key used in a token response to identify the resource owner.
19
     */
20
    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id';
21
22
    /**
23
     * Constructs an OAuth 2.0 service provider.
24
     *
25
     * @param array $options An array of options to set on this provider.
26
     *     Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
27
     *     Individual providers may introduce more options, as needed.
28
     * @param array $collaborators An array of collaborators that may be used to
29
     *     override this provider's default behavior. Collaborators include
30
     *     `grantFactory`, `requestFactory`, `httpClient`, and `randomFactory`.
31
     *     Individual providers may introduce more collaborators, as needed.
32
     */
33 18
    public function __construct(array $options = [], array $collaborators = [])
34
    {
35 18
        parent::__construct($options, $collaborators);
36
37 18
        if (empty($this->subdomain)) {
38 2
            throw new Exception\ProviderConfigurationException(
39 1
                'No subdomain has been configured for this Zendesk provider; it has to have a subdomain.'
40 1
            );
41
        }
42 18
    }
43
44
    /**
45
     * Get authorization url to begin OAuth flow
46
     *
47
     * @return string
48
     */
49 6
    public function getBaseAuthorizationUrl()
50
    {
51 6
        return 'https://'.$this->subdomain.'.zendesk.com/oauth/authorizations/new';
52
    }
53
54
    /**
55
     * Get access token url to retrieve token
56
     *
57
     * @return string
58
     */
59 8
    public function getBaseAccessTokenUrl(array $params)
60
    {
61 8
        return 'https://'.$this->subdomain.'.zendesk.com/oauth/tokens';
62
    }
63
64
    /**
65
     * Get provider url to fetch user details
66
     *
67
     * @param  AccessToken $token
68
     *
69
     * @return string
70
     */
71 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
72
    {
73 2
        return 'https://'.$this->subdomain.'.zendesk.com/api/v2/users/me.json';
74
    }
75
76
    /**
77
     * Get the default scopes used by this provider.
78
     *
79
     * This should not be a complete list of all scopes, but the minimum
80
     * required for the provider user interface!
81
     *
82
     * @return array
83
     */
84 4
    protected function getDefaultScopes()
85
    {
86 4
        return $this->scopes;
87
    }
88
89
    /**
90
     * Set the default scopes used by this provider.
91
     *
92
     * This should not be a complete list of all scopes, but the minimum
93
     * required for the provider user interface!
94
     *
95
     * @return Zendesk
96
     */
97
    public function setDefaultScopes(Array $scopes)
98
    {
99
        if (!empty($scopes)) {
100
            $this->scopes = $scopes;
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * Returns the string that should be used to separate scopes when building
108
     * the URL for requesting an access token.
109
     *
110
     * @return string Scope separator, defaults to ','
111
     */
112 6
    protected function getScopeSeparator()
113
    {
114 6
        return ' ';
115
    }
116
117
    /**
118
     * Check a provider response for errors.
119
     *
120
     * @throws IdentityProviderException
121
     * @param  ResponseInterface $response
122
     * @param  string $data Parsed response data
123
     * @return void
124
     */
125 6
    protected function checkResponse(ResponseInterface $response, $data)
126
    {
127 6
        $statusCode = $response->getStatusCode();
128 6
        if ($statusCode >= 400) {
129 2
            throw new IdentityProviderException(
130 2
                isset($data['description']) ? $data['description'] : $response->getReasonPhrase(),
131 3
                $statusCode,
132 1
                $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...
133 1
            );
134
        }
135 4
    }
136
137
    /**
138
     * Generate a user object from a successful user details request.
139
     *
140
     * @param object $response
141
     * @param AccessToken $token
142
     * @return ZendeskResourceOwner
143
     */
144 2
    protected function createResourceOwner(array $response, AccessToken $token)
145
    {
146 2
        return new ZendeskResourceOwner($response);
147
    }
148
149
    /**
150
     * Retrieves currently configured subdomain.
151
     *
152
     * @return string
153
     */
154 2
    public function getSubdomain()
155
    {
156 2
        return $this->subdomain;
157
    }
158
159
    /**
160
     * Updates currently configured subdomain.
161
     *
162
     * @param string $subdomain
163
     *
164
     * @return Zendesk
165
     */
166 2
    public function setSubdomain($subdomain)
167
    {
168 2
        if (!empty($subdomain)) {
169 2
            $this->subdomain = $subdomain;
170 1
        }
171
172 2
        return $this;
173
    }
174
}
175