Completed
Push — master ( a71c48...fb068a )
by Дмитрий
04:47
created

Discord::getScopeInline()   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
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2\Provider;
8
9
use SocialConnect\Common\Http\Client\Client;
10
use SocialConnect\Provider\AccessTokenInterface;
11
use SocialConnect\Provider\Exception\InvalidAccessToken;
12
use SocialConnect\Provider\Exception\InvalidResponse;
13
use SocialConnect\OAuth2\AccessToken;
14
use SocialConnect\Common\Entity\User;
15
use SocialConnect\Common\Hydrator\ObjectMap;
16
17
class Discord extends \SocialConnect\OAuth2\AbstractProvider
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function getBaseUri()
23
    {
24
        return 'https://discordapp.com/api/';
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getAuthorizeUri()
31
    {
32
        return 'https://discordapp.com/api/oauth2/authorize';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getRequestTokenUri()
39
    {
40
        return 'https://discordapp.com/api/oauth2/token';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getName()
47
    {
48
        return 'discord';
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getScopeInline()
55
    {
56
        return implode(' ', $this->scope);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function parseToken($body)
63
    {
64
        if (empty($body)) {
65
            throw new InvalidAccessToken('Provider response with empty body');
66
        }
67
68
        $result = json_decode($body, true);
69
        if ($result) {
70
            return new AccessToken($result);
71
        }
72
73
        throw new InvalidAccessToken('Provider response with not valid JSON');
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getIdentity(AccessTokenInterface $accessToken)
80
    {
81
        $response = $this->httpClient->request(
82
            $this->getBaseUri() . 'users/@me',
83
            [],
84
            Client::GET,
85
            [
86
                'Authorization' => 'Bearer ' . $accessToken->getToken()
87
            ]
88
        );
89
90
        if (!$response->isSuccess()) {
91
            throw new InvalidResponse(
92
                'API response with error code',
93
                $response
94
            );
95
        }
96
97
        $result = $response->json();
98
        if (!$result) {
99
            throw new InvalidResponse(
100
                'API response is not a valid JSON object',
101
                $response->getBody()
102
            );
103
        }
104
105
        $hydrator = new ObjectMap(
106
            [
107
                'verified' => 'emailVerified'
108
            ]
109
        );
110
111
        return $hydrator->hydrate(new User(), $result);
112
    }
113
}
114