Salesforce   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 149
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseAuthorizationUrl() 0 4 1
A getBaseAccessTokenUrl() 0 4 1
A getResourceOwnerDetailsUrl() 0 4 1
A getDefaultScopes() 0 4 1
A getDomain() 0 4 1
A getScopeSeparator() 0 4 1
A checkResponse() 0 11 3
A createResourceOwner() 0 4 1
A createAccessToken() 0 4 1
A setDomain() 0 12 2
1
<?php
2
3
namespace Stevenmaguire\OAuth2\Client\Provider;
4
5
use Exception;
6
use InvalidArgumentException;
7
use League\OAuth2\Client\Grant\AbstractGrant;
8
use League\OAuth2\Client\Provider\AbstractProvider;
9
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
10
use League\OAuth2\Client\Token\AccessToken;
11
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
12
use Psr\Http\Message\ResponseInterface;
13
14
class Salesforce extends AbstractProvider
15
{
16
    use BearerAuthorizationTrait;
17
18
    /**
19
     * @var string Key used in a token response to identify the resource owner.
20
     */
21
    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id';
22
23
    /**
24
     * Base domain used for authentication
25
     *
26
     * @var string
27
     */
28
    protected $domain = 'https://login.salesforce.com';
29
30
    /**
31
     * Get authorization url to begin OAuth flow
32
     *
33
     * @return string
34
     */
35 6
    public function getBaseAuthorizationUrl()
36
    {
37 6
        return $this->domain . '/services/oauth2/authorize';
38
    }
39
40
    /**
41
     * Get access token url to retrieve token
42
     *
43
     * @return string
44
     */
45 8
    public function getBaseAccessTokenUrl(array $params)
46
    {
47 8
        return $this->domain . '/services/oauth2/token';
48
    }
49
50
    /**
51
     * Get provider url to fetch user details
52
     *
53
     * @param  AccessToken $token
54
     *
55
     * @return string
56
     */
57 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
58
    {
59 2
        return $token->getResourceOwnerId();
60
    }
61
62
    /**
63
     * Get the default scopes used by this provider.
64
     *
65
     * This should not be a complete list of all scopes, but the minimum
66
     * required for the provider user interface!
67
     *
68
     * @return array
69
     */
70 4
    protected function getDefaultScopes()
71
    {
72 4
        return [];
73
    }
74
75
    /**
76
     * Retrives the currently configured provider domain.
77
     *
78
     * @return string
79
     */
80 4
    public function getDomain()
81
    {
82 4
        return $this->domain;
83
    }
84
85
    /**
86
     * Returns the string that should be used to separate scopes when building
87
     * the URL for requesting an access token.
88
     *
89
     * @return string Scope separator, defaults to ','
90
     */
91 6
    protected function getScopeSeparator()
92
    {
93 6
        return ' ';
94
    }
95
96
    /**
97
     * Check a provider response for errors.
98
     *
99
     * @throws IdentityProviderException
100
     * @param  ResponseInterface $response
101
     * @param  string $data Parsed response data
102
     * @return void
103
     */
104 6
    protected function checkResponse(ResponseInterface $response, $data)
105
    {
106 6
        $statusCode = $response->getStatusCode();
107 6
        if ($statusCode >= 400) {
108 2
            throw new IdentityProviderException(
109 2
                isset($data[0]['message']) ? $data[0]['message'] : $response->getReasonPhrase(),
110 1
                $statusCode,
111
                $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...
112 1
            );
113
        }
114 4
    }
115
116
    /**
117
     * Generate a user object from a successful user details request.
118
     *
119
     * @param object $response
120
     * @param AccessToken $token
121
     * @return SalesforceResourceOwner
122
     */
123 2
    protected function createResourceOwner(array $response, AccessToken $token)
124
    {
125 2
        return new SalesforceResourceOwner($response);
126
    }
127
128
    /**
129
     * Creates an access token from a response.
130
     *
131
     * The grant that was used to fetch the response can be used to provide
132
     * additional context.
133
     *
134
     * @param  array $response
135
     * @param  AbstractGrant $grant
136
     * @return AccessToken
137
     */
138 4
    protected function createAccessToken(array $response, AbstractGrant $grant)
139
    {
140 4
        return new \Stevenmaguire\OAuth2\Client\Token\AccessToken($response);
141
    }
142
143
    /**
144
     * Updates the provider domain with a given value.
145
     *
146
     * @throws  InvalidArgumentException
147
     * @param string $domain
148
     * @return  Salesforce
149
     */
150 4
    public function setDomain($domain)
151
    {
152
        try {
153 4
            $this->domain = (string) $domain;
154 3
        } catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...in is not a string'); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
155 2
            throw new InvalidArgumentException(
156 1
                'Value provided as domain is not a string'
157 1
            );
158
        }
159
160 2
        return $this;
161
    }
162
}
163