Completed
Push — master ( 4c303e...0095fb )
by Дмитрий
03:38
created

DigitalOcean::parseToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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