Passed
Pull Request — master (#190)
by AD
05:46 queued 03:08
created

Discord   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 75
ccs 19
cts 20
cp 0.95
rs 10
c 3
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizeUri() 0 3 1
A getName() 0 3 1
A getScopeInline() 0 3 1
A getBaseUri() 0 3 1
A getRequestTokenUri() 0 3 1
A prepareRequest() 0 4 2
A getIdentity() 0 18 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\OAuth2\Provider;
9
10
use SocialConnect\Common\ArrayHydrator;
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Common\Entity\User;
13
14
class Discord extends \SocialConnect\OAuth2\AbstractProvider
15
{
16
    const NAME = 'discord';
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 4
    public function getBaseUri()
22
    {
23 4
        return 'https://discordapp.com/api/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://discordapp.com/api/oauth2/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function getRequestTokenUri()
38
    {
39 2
        return 'https://discordapp.com/api/oauth2/token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getName()
46
    {
47 3
        return self::NAME;
48
    }
49
50
    /**
51
     * @return string
52
     */
53 1
    public function getScopeInline()
54
    {
55 1
        return implode(' ', $this->scope);
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 3
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, ?AccessTokenInterface $accessToken = null): void
62
    {
63 3
        if ($accessToken) {
64 3
            $headers['Authorization'] = "Bearer {$accessToken->getToken()}";
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 3
    public function getIdentity(AccessTokenInterface $accessToken)
72
    {
73 3
        $response = $this->request('GET', 'users/@me', [], $accessToken);
74
75 1
        $hydrator = new ArrayHydrator([
76
            'id' => 'id',
77
            'username' => 'username',
78
            'avatar' => 'pictureURL',
79
            'email' => 'email',
80
            'verified' => 'emailVerified'
81
        ]);
82
83 1
        $user = $hydrator->hydrate(new User(), $response);
84 1
        if ($user->pictureURL) {
85
            $user->pictureURL = "https://cdn.discordapp.com/avatars/{$user->id}/{$user->pictureURL}.png";
86
        }
87
88 1
        return $user;
89
    }
90
}
91