Completed
Push — master ( 046422...8eb4ac )
by ARCANEDEV
08:13 queued 14s
created

GithubProvider::getTokenUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanedev\Socialite\OAuth\Two;
2
3
use Arcanedev\Socialite\Base\OAuthTwoProvider;
4
5
/**
6
 * Class     GithubProvider
7
 *
8
 * @package  Arcanedev\Socialite\OAuth\Two
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class GithubProvider extends OAuthTwoProvider
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * The scopes being requested.
19
     *
20
     * @var array
21
     */
22
    protected $scopes = ['user:email'];
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Main Functions
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function getAuthUrl($state)
32
    {
33
        return $this->buildAuthUrlFromBase('https://github.com/login/oauth/authorize', $state);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function getTokenUrl()
40
    {
41
        return 'https://github.com/login/oauth/access_token';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function getUserByToken($token)
48
    {
49
        $userUrl  = 'https://api.github.com/user?access_token='.$token;
50
        $response = $this->getHttpClient()->get(
51
            $userUrl, $this->getRequestOptions()
52
        );
53
        $user = json_decode($response->getBody(), true);
54
55
        if (in_array('user:email', $this->scopes)) {
56
            $user['email'] = $this->getEmailByToken($token);
57
        }
58
59
        return $user;
60
    }
61
62
    /**
63
     * Get the email for the given access token.
64
     *
65
     * @param  string  $token
66
     *
67
     * @return string|null
68
     */
69
    protected function getEmailByToken($token)
70
    {
71
        $emailsUrl = 'https://api.github.com/user/emails?access_token='.$token;
72
73
        try {
74
            $response = $this->getHttpClient()->get(
75
                $emailsUrl, $this->getRequestOptions()
76
            );
77
        }
78
        catch (\Exception $e) {
79
            return null;
80
        }
81
82
        foreach (json_decode($response->getBody(), true) as $email) {
83
            if ($email['primary'] && $email['verified']) {
84
                return $email['email'];
85
            }
86
        }
87
88
        return null;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function mapUserToObject(array $user)
95
    {
96
        return (new User)->setRaw($user)->map([
97
            'id'       => $user['id'],
98
            'nickname' => $user['login'],
99
            'name'     => array_get($user, 'name'),
100
            'email'    => array_get($user, 'email'),
101
            'avatar'   => $user['avatar_url'],
102
        ]);
103
    }
104
105
    /**
106
     * Get the default options for an HTTP request.
107
     *
108
     * @return array
109
     */
110
    protected function getRequestOptions()
111
    {
112
        return [
113
            'headers' => [
114
                'Accept' => 'application/vnd.github.v3+json',
115
            ],
116
        ];
117
    }
118
}
119