Completed
Push — master ( 51b8e0...0f94ca )
by ARCANEDEV
06:17
created

GoogleProvider::getAccessToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php namespace Arcanedev\Socialite\OAuth\Two;
2
3
use Illuminate\Support\Arr;
4
5
/**
6
 * Class     GoogleProvider
7
 *
8
 * @package  Arcanedev\Socialite\OAuth\Two
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class GoogleProvider extends AbstractProvider
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * The separating character for the requested scopes.
19
     *
20
     * @var string
21
     */
22
    protected $scopeSeparator = ' ';
23
24
    /**
25
     * The scopes being requested.
26
     *
27
     * @var array
28
     */
29
    protected $scopes = ['openid', 'profile', 'email'];
30
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Getters & Setters
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function getAuthUrl($state)
39
    {
40
        return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/auth', $state);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function getTokenUrl()
47
    {
48
        return 'https://accounts.google.com/o/oauth2/token';
49
    }
50
51
    /**
52
     * Get the POST fields for the token request.
53
     *
54
     * @param  string  $code
55
     * @return array
56
     */
57
    protected function getTokenFields($code)
58
    {
59
        return Arr::add(
60
            parent::getTokenFields($code), 'grant_type', 'authorization_code'
61
        );
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function getUserByToken($token)
68
    {
69
        $response = $this->getHttpClient()->get('https://www.googleapis.com/plus/v1/people/me?', [
70
            'query' => [
71
                'prettyPrint' => 'false',
72
            ],
73
            'headers' => [
74
                'Accept' => 'application/json',
75
                'Authorization' => 'Bearer '.$token,
76
            ],
77
        ]);
78
79
        return json_decode($response->getBody(), true);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function mapUserToObject(array $user)
86
    {
87
        return (new User)->setRaw($user)->map([
88
            'id'              => $user['id'],
89
            'nickname'        => Arr::get($user, 'nickname'),
90
            'name'            => $user['displayName'],
91
            'email'           => $user['emails'][0]['value'],
92
            'avatar'          => Arr::get($user, 'image')['url'],
93
            'avatar_original' => preg_replace('/\?sz=([0-9]+)/', '', Arr::get($user, 'image')['url']),
94
        ]);
95
    }
96
}
97