Completed
Push — master ( 4c303e...0095fb )
by Дмитрий
03:38
created

Twitch::getIdentity()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 22
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 1
crap 12
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 1
    public function getBaseUri()
22
    {
23 1
        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 1
    public function getRequestTokenUri()
38
    {
39 1
        return 'https://api.twitch.tv/kraken/oauth2/token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function getName()
46
    {
47 2
        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
    public function parseToken($body)
63
    {
64
        $response = json_decode($body, true);
65
        if ($response) {
66
            return new AccessToken($response);
67
        }
68
69
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getIdentity(AccessTokenInterface $accessToken)
76
    {
77
        $response = $this->httpClient->request(
78
            $this->getBaseUri() . 'user',
79
            [
80
                'oauth_token' => $accessToken->getToken()
81
            ]
82
        );
83
84
        if (!$response->isSuccess()) {
85
            throw new InvalidResponse(
86
                'API response with error code',
87
                $response
88
            );
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