Completed
Push — master ( 112fa1...258d4d )
by Shaharia
02:33
created

Zoho   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 87.18%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 3
dl 0
loc 153
ccs 34
cts 39
cp 0.8718
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseAuthorizationUrl() 0 4 1
A getBaseAccessTokenUrl() 0 4 1
A getResourceOwnerDetailsUrl() 0 4 1
A getAuthorizationHeaders() 0 4 1
A getDefaultScopes() 0 4 1
A checkResponse() 0 7 2
A createResourceOwner() 0 4 1
A __construct() 0 10 3
B getAuthorizationParameters() 0 37 7
1
<?php
2
3
4
namespace ShahariaAzam\ZohoOAuth2\Client\Provider;
5
6
use League\OAuth2\Client\Provider\AbstractProvider;
7
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
8
use League\OAuth2\Client\Token\AccessToken;
9
use Psr\Http\Message\ResponseInterface;
10
use ShahariaAzam\ZohoOAuth2\Client\Exceptions\IdentifyProviderException;
11
12
class Zoho extends AbstractProvider
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $endpoint = "https://accounts.zoho.com";
18
    protected $accessType = "online";
19
20
    /**
21
     * Zoho constructor.
22
     * @param array $options
23
     * @param array $collaborators
24
     */
25 20
    public function __construct(array $options = [], array $collaborators = [])
26
    {
27 20
        parent::__construct($options, $collaborators);
28
29 20
        foreach (['clientId', 'clientSecret', 'redirectUri'] as $key) {
30 20
            if (!isset($options[$key])) {
31 20
                throw new \InvalidArgumentException($key . " is missing");
32
            }
33
        }
34 20
    }
35
36
    /**
37
     * Returns the base URL for authorizing a client.
38
     *
39
     * Eg. https://oauth.service.com/authorize
40
     *
41
     * @return string
42
     */
43 4
    public function getBaseAuthorizationUrl()
44
    {
45 4
        return $this->endpoint . "/oauth/v2/auth";
46
    }
47
48
    /**
49
     * Returns the base URL for requesting an access token.
50
     *
51
     * Eg. https://oauth.service.com/token
52
     *
53
     * @param array $params
54
     * @return string
55
     */
56 8
    public function getBaseAccessTokenUrl(array $params)
57
    {
58 8
        return $this->endpoint . "/oauth/v2/token";
59
    }
60
61
    /**
62
     * Returns the URL for requesting the resource owner's details.
63
     *
64
     * @param AccessToken $token
65
     * @return string
66
     */
67 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
68
    {
69 2
        return $this->endpoint . "/oauth/user/info";
70
    }
71
72
    /**
73
     * @param mixed|null $token
74
     * @return array|void
75
     */
76
    protected function getAuthorizationHeaders($token = null)
77
    {
78
        return ['Authorization' => "Bearer " . $token];
79
    }
80
81
    /**
82
     * @param array $options
83
     * @return array
84
     */
85 2
    protected function getAuthorizationParameters(array $options)
86
    {
87 2
        if (empty($options['state'])) {
88 2
            $options['state'] = $this->getRandomState();
89
        }
90
91 2
        if (empty($options['scope'])) {
92 2
            $options['scope'] = $this->getDefaultScopes();
93
        }
94
95
        $options += [
96 2
            'response_type' => 'code',
97
            'approval_prompt' => 'auto'
98
        ];
99
100 2
        if (is_array($options['scope'])) {
101 2
            $separator = $this->getScopeSeparator();
102 2
            $options['scope'] = implode($separator, $options['scope']);
103
        }
104
105
        // Store the state as it may need to be accessed later on.
106 2
        $this->state = $options['state'];
107
108
        // Business code layer might set a different redirect_uri parameter
109
        // depending on the context, leave it as-is
110 2
        if (!isset($options['redirect_uri'])) {
111 2
            $options['redirect_uri'] = $this->redirectUri;
112
        }
113
114 2
        $options['client_id'] = $this->clientId;
115
116 2
        if (isset($options['access_type']) && in_array($options['access_type'], ['offline', 'online'])) {
117
            $options['access_type'] = $this->accessType;
118
        }
119
120 2
        return $options;
121
    }
122
123
    /**
124
     * Returns the default scopes used by this provider.
125
     *
126
     * This should only be the scopes that are required to request the details
127
     * of the resource owner, rather than all the available scopes.
128
     *
129
     * @return array
130
     */
131 4
    protected function getDefaultScopes()
132
    {
133 4
        return ['aaaserver.profile.READ', 'ZohoProfile.userinfo.read', 'ZohoProfile.userphoto.read'];
134
    }
135
136
    /**
137
     * Checking response to see if provider returned any error
138
     *
139
     * @param ResponseInterface $response
140
     * @param array|string $data Parsed response data
141
     * @return void
142
     * @throws IdentityProviderException
143
     */
144 4
    protected function checkResponse(ResponseInterface $response, $data)
145
    {
146 4
        $responseData = $this->parseResponse($response);
147 4
        if (array_key_exists("error", $responseData)) {
148 2
            throw new IdentifyProviderException($responseData['error'], $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...
149
        }
150 2
    }
151
152
    /**
153
     * Generates a resource owner object from a successful resource owner
154
     * details request.
155
     *
156
     * @param array $response
157
     * @param AccessToken $token
158
     * @return ZohoUser
159
     */
160
    protected function createResourceOwner(array $response, AccessToken $token)
161
    {
162
        return new ZohoUser($response);
163
    }
164
}
165