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

GitLab   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

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