Completed
Push — master ( 48f3ce...a71c48 )
by Дмитрий
04:26
created

Discord   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 83
ccs 0
cts 51
cp 0
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUri() 0 4 1
A getAuthorizeUri() 0 4 1
A getRequestTokenUri() 0 4 1
A getName() 0 4 1
A parseToken() 0 13 3
B getIdentity() 0 28 3
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\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidAccessToken;
11
use SocialConnect\Provider\Exception\InvalidResponse;
12
use SocialConnect\OAuth2\AccessToken;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
class Discord extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getBaseUri()
22
    {
23
        return 'https://disqus.com/api/3.0/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getAuthorizeUri()
30
    {
31
        return 'https://disqus.com/api/oauth/2.0/authorize/';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getRequestTokenUri()
38
    {
39
        return 'https://disqus.com/api/oauth/2.0/access_token/';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getName()
46
    {
47
        return 'discord';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function parseToken($body)
54
    {
55
        if (empty($body)) {
56
            throw new InvalidAccessToken('Provider response with empty body');
57
        }
58
59
        $result = json_decode($body, true);
60
        if ($result) {
61
            return new AccessToken($result);
62
        }
63
64
        throw new InvalidAccessToken('Provider response with not valid JSON');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getIdentity(AccessTokenInterface $accessToken)
71
    {
72
        $response = $this->httpClient->request(
73
            $this->getBaseUri() . 'users/details',
74
            [
75
                'access_token' => $accessToken->getToken()
76
            ]
77
        );
78
79
        if (!$response->isSuccess()) {
80
            throw new InvalidResponse(
81
                'API response with error code',
82
                $response
83
            );
84
        }
85
86
        $result = $response->json();
87
        if (!$result) {
88
            throw new InvalidResponse(
89
                'API response is not a valid JSON object',
90
                $response->getBody()
91
            );
92
        }
93
94
        $hydrator = new ObjectMap([]);
95
96
        return $hydrator->hydrate(new User(), $result);
97
    }
98
}
99