Completed
Push — master ( ea6bff...86302a )
by Дмитрий
08:05 queued 44s
created

Twitch   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 62.16%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 2
cbo 9
dl 0
loc 94
ccs 23
cts 37
cp 0.6216
rs 10

7 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 getScopeInline() 0 5 1
B getIdentity() 0 34 3
A parseToken() 0 9 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\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 Twitch extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 2
    public function getBaseUri()
22
    {
23 2
        return 'https://api.twitch.tv/kraken/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://api.twitch.tv/kraken/oauth2/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function getRequestTokenUri()
38
    {
39 2
        return 'https://api.twitch.tv/kraken/oauth2/token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getName()
46
    {
47 3
        return 'twitch';
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getScopeInline()
54
    {
55
        // @link https://github.com/justintv/Twitch-API/blob/master/authentication.md#scopes
56
        return implode('+', $this->scope);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 3
    public function parseToken($body)
63
    {
64 3
        $response = json_decode($body, true);
65 3
        if ($response) {
66 1
            return new AccessToken($response);
67
        }
68
69 2
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function getIdentity(AccessTokenInterface $accessToken)
76
    {
77 1
        $response = $this->httpClient->request(
78 1
            $this->getBaseUri() . 'user',
79
            [
80 1
                'oauth_token' => $accessToken->getToken()
81 1
            ]
82 1
        );
83
84 1
        if (!$response->isSuccess()) {
85 1
            throw new InvalidResponse(
86 1
                'API response with error code',
87
                $response
88 1
            );
89
        }
90
91
        $result = $response->json();
92
        if (!$result) {
93
            throw new InvalidResponse(
94
                'API response is not a valid JSON object',
95
                $response->getBody()
96
            );
97
        }
98
99
        $hydrator = new ObjectMap(
100
            [
101
                '_id' => 'id',
102
                'display_name' => 'fullname', // Custom Capitalized Users name
103
                'name' => 'username',
104
            ]
105
        );
106
107
        return $hydrator->hydrate(new User(), $result);
108
    }
109
}
110