Passed
Push — master ( 67c528...ea8c3a )
by Hirofumi
02:42
created

BaseProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 94
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

7 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 checkResponse() 0 10 3
A createResourceOwner() 0 4 1
A getScopeSeparator() 0 4 1
1
<?php
2
3
namespace Shippinno\Base\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
8
use League\OAuth2\Client\Token\AccessToken;
9
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
10
use Psr\Http\Message\ResponseInterface;
11
12
class BaseProvider extends AbstractProvider
13
{
14
    use BearerAuthorizationTrait;
15
16
    /**
17
     * Returns the base URL for authorizing a client.
18
     *
19
     * @return string
20
     */
21 1
    public function getBaseAuthorizationUrl()
22
    {
23 1
        return 'https://api.thebase.in/1/oauth/authorize';
24
    }
25
26
    /**
27
     * Returns the base URL for requesting an access token.
28
     *
29
     * Eg. https://oauth.service.com/token
30
     *
31
     * @param array $params
32
     * @return string
33
     */
34 3
    public function getBaseAccessTokenUrl(array $params)
35
    {
36 3
        return 'https://api.thebase.in/1/oauth/token';
37
    }
38
39
    /**
40
     * Returns the URL for requesting the resource owner's details.
41
     *
42
     * @param AccessToken $token
43
     * @return string
44
     */
45 2
    public function getResourceOwnerDetailsUrl(AccessToken $token)
46
    {
47 2
        return 'https://api.thebase.in/1/users/me';
48
    }
49
50
    /**
51
     * Returns the default scopes used by this provider.
52
     *
53
     * This should only be the scopes that are required to request the details
54
     * of the resource owner, rather than all the available scopes.
55
     *
56
     * @return string[]
57
     */
58 1
    protected function getDefaultScopes()
59
    {
60 1
        return ['read_users'];
61
    }
62
63
    /**
64
     * Checks a provider response for errors.
65
     *
66
     * @throws IdentityProviderException
67
     * @param  ResponseInterface $response
68
     * @param  array|string $data Parsed response data
69
     * @return void
70
     */
71 3
    protected function checkResponse(ResponseInterface $response, $data)
72
    {
73 3
        if (isset($data['error'])) {
74 1
            throw new IdentityProviderException(
75 1
                $data['error'] ?: $response->getReasonPhrase(),
76 1
                $response->getStatusCode(),
77 1
                $response->getBody()
78 1
            );
79
        }
80 2
    }
81
82
    /**
83
     * Generates a resource owner object from a successful resource owner
84
     * details request.
85
     *
86
     * @param  array $response
87
     * @param  AccessToken $token
88
     * @return ResourceOwnerInterface
89
     */
90 1
    protected function createResourceOwner(array $response, AccessToken $token)
91
    {
92 1
        return new BaseResourceOwner($response);
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
100
     */
101 1
    protected function getScopeSeparator()
102
    {
103 1
        return ' ';
104
    }
105
}
106