Passed
Push — master ( 0fb604...7ba039 )
by Monney
03:42
created

GitlabProvider::getUserByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mckenziearts\LaravelOAuth\Providers;
4
5
use Laravel\Socialite\Two\User;
6
use Laravel\Socialite\Two\AbstractProvider;
7
use Laravel\Socialite\Two\ProviderInterface;
8
9
class GitlabProvider extends AbstractProvider implements ProviderInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected $scopes = ['read_user'];
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $scopeSeparator = ' ';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function getAuthUrl($state)
25
    {
26
        return $this->buildAuthUrlFromBase('https://gitlab.com/oauth/authorize', $state);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function getTokenUrl()
33
    {
34
        return 'https://gitlab.com/oauth/token';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function getUserByToken($token)
41
    {
42
        $response = $this->getHttpClient()->get('https://gitlab.com/api/v4/user', [
43
            'headers' => [
44
                'Authorization' => 'Bearer '.$token,
45
            ],
46
        ]);
47
48
        return json_decode($response->getBody(), true);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function mapUserToObject(array $user)
55
    {
56
        return (new User())->setRaw($user)->map([
57
            'id'       => $user['id'],
58
            'nickname' => $user['username'],
59
            'name'     => $user['name'],
60
            'email'    => $user['email'],
61
            'avatar'   => $user['avatar_url'],
62
        ]);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function getTokenFields($code)
69
    {
70
        return array_merge(parent::getTokenFields($code), [
71
            'grant_type' => 'authorization_code',
72
        ]);
73
    }
74
}
75