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

Provider::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Bitbucket;
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\Hydrator\ObjectMap;
14
15
class Provider extends \SocialConnect\Auth\Provider\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
        $result = $response->json();
77
        if (!$result) {
78
            throw new InvalidResponse(
79
                'API response is not a valid JSON object',
80
                $response->getBody()
81
            );
82
        }
83
84
        $hydrator = new ObjectMap(array(
85
            'uuid' => 'id',
86
            'display_name' => 'fullname',
87
        ));
88
89
        return $hydrator->hydrate(new User(), $result);
90
    }
91
}
92