Completed
Push — master ( d73944...a54609 )
by Steven
03:00
created

Zendesk::getSubdomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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