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

Bitbucket::getBaseUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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