Completed
Push — master ( ad69a5...d98011 )
by Дмитрий
03:25
created

Provider::getIdentity()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Gitlab;
8
9
use SocialConnect\Auth\Exception\InvalidAccessToken;
10
use SocialConnect\Auth\Exception\InvalidResponse;
11
use SocialConnect\Auth\Provider\OAuth2\AccessToken;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Http\Client\Client;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
class Provider extends \SocialConnect\Auth\Provider\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getBaseUri()
22
    {
23
        return 'https://gitlab.com/api/v3/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getAuthorizeUri()
30
    {
31
        return 'https://gitlab.com/oauth/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getRequestTokenUri()
38
    {
39
        return 'https://gitlab.com/oauth/token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getName()
46
    {
47
        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
    public function parseToken($body)
62
    {
63
        if (empty($body)) {
64
            throw new InvalidAccessToken('Provider response with empty body');
65
        }
66
67
        $result = json_decode($body);
68
        if ($result) {
69
            if (isset($result->access_token)) {
70
                return new AccessToken($result->access_token);
71
            }
72
73
            throw new InvalidAccessToken('Provider API returned without access_token field inside JSON');
74
        }
75
76
        throw new InvalidAccessToken('Provider response with not valid JSON');
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getIdentity(AccessToken $accessToken)
83
    {
84
        $response = $this->service->getHttpClient()->request(
85
            $this->getBaseUri() . 'user',
86
            [
87
                'access_token' => $accessToken->getToken()
88
            ]
89
        );
90
91
        $result = $response->json();
92
        if (!$result) {
93
            throw new InvalidResponse(
94
                'API response is not a valid JSON object',
95
                $response->getBody()
96
            );
97
        }
98
99
        $hydrator = new ObjectMap(array(
100
            'user_id' => 'id',
101
            'name' => 'fullname',
102
        ));
103
104
        return $hydrator->hydrate(new User(), $result);
105
    }
106
}
107