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

GitHub   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 30.76%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 55
ccs 8
cts 26
cp 0.3076
rs 10
c 0
b 0
f 0

5 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
B getIdentity() 0 29 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\InvalidResponse;
11
use SocialConnect\Common\Entity\User;
12
use SocialConnect\Common\Hydrator\ObjectMap;
13
14
class GitHub extends \SocialConnect\OAuth2\AbstractProvider
15
{
16 1
    public function getBaseUri()
17
    {
18 1
        return 'https://api.github.com/';
19
    }
20
21 2
    public function getAuthorizeUri()
22
    {
23 2
        return 'https://github.com/login/oauth/authorize';
24
    }
25
26 1
    public function getRequestTokenUri()
27
    {
28 1
        return 'https://github.com/login/oauth/access_token';
29
    }
30
31 2
    public function getName()
32
    {
33 2
        return 'github';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getIdentity(AccessTokenInterface $accessToken)
40
    {
41
        $response = $this->httpClient->request(
42
            $this->getBaseUri() . 'user',
43
            [
44
                'access_token' => $accessToken->getToken()
45
            ]
46
        );
47
48
        if (!$response->isSuccess()) {
49
            throw new InvalidResponse(
50
                'API response with error code',
51
                $response
52
            );
53
        }
54
55
        $body = $response->getBody();
56
        $result = json_decode($body);
57
58
        $hydrator = new ObjectMap(
59
            [
60
                'id' => 'id',
61
                'login' => 'username',
62
                'email' => 'email',
63
            ]
64
        );
65
66
        return $hydrator->hydrate(new User(), $result);
67
    }
68
}
69