Completed
Push — master ( bda8d5...d74c6d )
by Дмитрий
03:32 queued 01:52
created

Twitch   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
wmc 10
eloc 28
dl 0
loc 94
rs 10
c 0
b 0
f 0
ccs 26
cts 31
cp 0.8387

7 Methods

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