Completed
Push — master ( df0c28...f24cf1 )
by Дмитрий
01:28
created

GitHub   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 79.31%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 60
ccs 23
cts 29
cp 0.7931
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 34 3
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 3
    public function getBaseUri()
17
    {
18 3
        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 2
    public function getRequestTokenUri()
27
    {
28 2
        return 'https://github.com/login/oauth/access_token';
29
    }
30
31 3
    public function getName()
32
    {
33 3
        return 'github';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function getIdentity(AccessTokenInterface $accessToken)
40
    {
41 2
        $response = $this->httpClient->request(
42 2
            $this->getBaseUri() . 'user',
43
            [
44 2
                'access_token' => $accessToken->getToken()
45 2
            ]
46 2
        );
47
48 2
        if (!$response->isSuccess()) {
49 1
            throw new InvalidResponse(
50 1
                'API response with error code',
51
                $response
52 1
            );
53
        }
54
55 1
        $result = $response->json();
56 1
        if (!$result) {
57 1
            throw new InvalidResponse(
58 1
                'API response is not a valid JSON object',
59
                $response
60 1
            );
61
        }
62
63
        $hydrator = new ObjectMap(
64
            [
65
                'id' => 'id',
66
                'login' => 'username',
67
                'email' => 'email',
68
            ]
69
        );
70
71
        return $hydrator->hydrate(new User(), $result);
72
    }
73
}
74