Completed
Push — master ( ea6bff...86302a )
by Дмитрий
08:05 queued 44s
created

Bitbucket::parseToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 7
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 Bitbucket extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 2
    public function getBaseUri()
22
    {
23 2
        return 'https://api.bitbucket.org/2.0/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://bitbucket.org/site/oauth2/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function getRequestTokenUri()
38
    {
39 2
        return 'https://bitbucket.org/site/oauth2/access_token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getName()
46
    {
47 3
        return 'bitbucket';
48
    }
49
50 3
    public function parseToken($body)
51
    {
52 3
        if (empty($body)) {
53
            throw new InvalidAccessToken('Provider response with empty body');
54
        }
55
56 3
        $result = json_decode($body, true);
57 3
        if ($result) {
58 1
            return new AccessToken($result);
59
        }
60
61 2
        throw new InvalidAccessToken('Server response with not valid/empty JSON');
62
    }
63
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function getIdentity(AccessTokenInterface $accessToken)
69
    {
70 1
        $response = $this->httpClient->request(
71 1
            $this->getBaseUri() . 'user',
72
            [
73 1
                'access_token' => $accessToken->getToken()
74 1
            ]
75 1
        );
76
77 1
        if (!$response->isSuccess()) {
78 1
            throw new InvalidResponse(
79 1
                'API response with error code',
80
                $response
81 1
            );
82
        }
83
84
        $result = $response->json();
85
        if (!$result) {
86
            throw new InvalidResponse(
87
                'API response is not a valid JSON object',
88
                $response->getBody()
89
            );
90
        }
91
92
        $hydrator = new ObjectMap(
93
            [
94
                'uuid' => 'id',
95
                'display_name' => 'fullname',
96
            ]
97
        );
98
99
        return $hydrator->hydrate(new User(), $result);
100
    }
101
}
102