Completed
Push — master ( bda8d5...d74c6d )
by Дмитрий
03:32 queued 01:52
created

GitLab::parseToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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