Completed
Push — master ( 483eda...cb59ac )
by Дмитрий
03:04
created

Twitch   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 2
cbo 9
dl 0
loc 96
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
A parseToken() 0 13 3
B getIdentity() 0 32 3
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Auth\Provider;
8
9
use SocialConnect\Auth\Provider\Exception\InvalidAccessToken;
10
use SocialConnect\Auth\Provider\Exception\InvalidResponse;
11
use SocialConnect\OAuth2\AccessToken;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Hydrator\ObjectMap;
14
15
class Twitch extends \SocialConnect\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
        if (!$response->isSuccess()) {
88
            throw new InvalidResponse(
89
                'API response with error code',
90
                $response
91
            );
92
        }
93
94
        $result = $response->json();
95
        if (!$result) {
96
            throw new InvalidResponse(
97
                'API response is not a valid JSON object',
98
                $response->getBody()
99
            );
100
        }
101
102
        $hydrator = new ObjectMap(array(
103
            '_id' => 'id',
104
            'display_name' => 'fullname', // Custom Capitalized Users name
105
            'name' => 'username',
106
        ));
107
108
        return $hydrator->hydrate(new User(), $result);
109
    }
110
}
111