Completed
Push — master ( ad69a5...d98011 )
by Дмитрий
03:25
created

Provider::getIdentity()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 1
f 0
cc 2
eloc 14
nc 2
nop 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Twitch;
8
9
use SocialConnect\Auth\Exception\InvalidAccessToken;
10
use SocialConnect\Auth\Exception\InvalidResponse;
11
use SocialConnect\Auth\Provider\OAuth2\AccessToken;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Hydrator\ObjectMap;
14
15
class Provider extends \SocialConnect\Auth\Provider\OAuth2\AbstractProvider
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function getBaseUri()
21
    {
22
        return 'https://api.twitch.tv/kraken/';
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getAuthorizeUri()
29
    {
30
        return 'https://api.twitch.tv/kraken/oauth2/authorize';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getRequestTokenUri()
37
    {
38
        return 'https://api.twitch.tv/kraken/oauth2/token';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getName()
45
    {
46
        return 'twitch';
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getScopeInline()
53
    {
54
        // @link https://github.com/justintv/Twitch-API/blob/master/authentication.md#scopes
55
        return implode('+', $this->scope);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function parseToken($body)
62
    {
63
        $response = json_decode($body, false);
64
        if ($response) {
65
            if (isset($response->access_token)) {
66
                return new AccessToken($response->access_token);
67
            }
68
69
            throw new InvalidAccessToken('access_token field does not exists inside API JSON response');
70
        }
71
72
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getIdentity(AccessToken $accessToken)
79
    {
80
        $response = $this->service->getHttpClient()->request(
81
            $this->getBaseUri() . 'user',
82
            [
83
                'oauth_token' => $accessToken->getToken()
84
            ]
85
        );
86
87
        $result = $response->json();
88
        if (!$result) {
89
            throw new InvalidResponse(
90
                'API response is not a valid JSON object',
91
                $response->getBody()
92
            );
93
        }
94
95
        $hydrator = new ObjectMap(array(
96
            '_id' => 'id',
97
            'display_name' => 'fullname', // Custom Capitalized Users name
98
            'name' => 'username',
99
        ));
100
101
        return $hydrator->hydrate(new User(), $result);
102
    }
103
}
104