Github::checkResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
3
namespace League\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\Exception\GithubIdentityProviderException;
6
use League\OAuth2\Client\Token\AccessToken;
7
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
8
use Psr\Http\Message\ResponseInterface;
9
10
class Github extends AbstractProvider
11
{
12
    use BearerAuthorizationTrait;
13
14
    /**
15
     * Domain
16
     *
17
     * @var string
18
     */
19
    public $domain = 'https://github.com';
20
21
    /**
22
     * Api domain
23
     *
24
     * @var string
25
     */
26
    public $apiDomain = 'https://api.github.com';
27
28
    /**
29
     * Get authorization url to begin OAuth flow
30
     *
31
     * @return string
32
     */
33 12
    public function getBaseAuthorizationUrl()
34
    {
35 12
        return $this->domain.'/login/oauth/authorize';
36
    }
37
38
    /**
39
     * Get access token url to retrieve token
40
     *
41
     * @param  array $params
42
     *
43
     * @return string
44
     */
45 18
    public function getBaseAccessTokenUrl(array $params)
46
    {
47 18
        return $this->domain.'/login/oauth/access_token';
48
    }
49
50
    /**
51
     * Get provider url to fetch user details
52
     *
53
     * @param  AccessToken $token
54
     *
55
     * @return string
56
     */
57 6
    public function getResourceOwnerDetailsUrl(AccessToken $token)
58
    {
59 6
        if ($this->domain === 'https://github.com') {
60 3
            return $this->apiDomain.'/user';
61
        }
62 3
        return $this->domain.'/api/v3/user';
63
    }
64
65
    /**
66
     * Get the default scopes used by this provider.
67
     *
68
     * This should not be a complete list of all scopes, but the minimum
69
     * required for the provider user interface!
70
     *
71
     * @return array
72
     */
73 6
    protected function getDefaultScopes()
74
    {
75 6
        return [];
76
    }
77
78
    /**
79
     * Check a provider response for errors.
80
     *
81
     * @link   https://developer.github.com/v3/#client-errors
82
     * @link   https://developer.github.com/v3/oauth/#common-errors-for-the-access-token-request
83
     * @throws IdentityProviderException
84
     * @param  ResponseInterface $response
85
     * @param  array $data Parsed response data
86
     * @return void
87
     */
88 15
    protected function checkResponse(ResponseInterface $response, $data)
89
    {
90 15
        if ($response->getStatusCode() >= 400) {
91 3
            throw GithubIdentityProviderException::clientException($response, $data);
92 12
        } elseif (isset($data['error'])) {
93 3
            throw GithubIdentityProviderException::oauthException($response, $data);
94
        }
95 9
    }
96
97
    /**
98
     * Generate a user object from a successful user details request.
99
     *
100
     * @param array $response
101
     * @param AccessToken $token
102
     * @return \League\OAuth2\Client\Provider\ResourceOwnerInterface
103
     */
104 3
    protected function createResourceOwner(array $response, AccessToken $token)
105
    {
106 3
        $user = new GithubResourceOwner($response);
107
108 3
        return $user->setDomain($this->domain);
109
    }
110
}
111