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

GitLab   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 78.38%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 9
dl 0
loc 96
ccs 29
cts 37
cp 0.7838
rs 10
c 0
b 0
f 0

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 4 1
A parseToken() 0 13 3
B getIdentity() 0 33 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\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 GitLab extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 3
    public function getBaseUri()
22
    {
23 3
        return 'https://gitlab.com/api/v3/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://gitlab.com/oauth/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function getRequestTokenUri()
38
    {
39 2
        return 'https://gitlab.com/oauth/token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getName()
46
    {
47 3
        return 'gitlab';
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getScopeInline()
54
    {
55
        return implode('+', $this->scope);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 3
    public function parseToken($body)
62
    {
63 3
        if (empty($body)) {
64
            throw new InvalidAccessToken('Provider response with empty body');
65
        }
66
67 3
        $result = json_decode($body, true);
68 3
        if ($result) {
69 1
            return new AccessToken($result);
70
        }
71
72 2
        throw new InvalidAccessToken('Provider response with not valid JSON');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    public function getIdentity(AccessTokenInterface $accessToken)
79
    {
80 2
        $response = $this->httpClient->request(
81 2
            $this->getBaseUri() . 'user',
82
            [
83 2
                'access_token' => $accessToken->getToken()
84 2
            ]
85 2
        );
86
87 2
        if (!$response->isSuccess()) {
88 1
            throw new InvalidResponse(
89 1
                'API response with error code',
90
                $response
91 1
            );
92
        }
93
94 1
        $result = $response->json();
95 1
        if (!$result) {
96 1
            throw new InvalidResponse(
97 1
                'API response is not a valid JSON object',
98
                $response
99 1
            );
100
        }
101
102
        $hydrator = new ObjectMap(
103
            [
104
                'user_id' => 'id',
105
                'name' => 'fullname',
106
            ]
107
        );
108
109
        return $hydrator->hydrate(new User(), $result);
110
    }
111
}
112