Completed
Pull Request — master (#506)
by Dragonqos
02:52
created

ParrotFlowerPower   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 0
loc 104
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A getAuthorizationEndpoint() 0 5 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
B parseAccessTokenResponse() 0 28 5
B refreshAccessToken() 0 26 2
1
<?php
2
/**
3
 * ParrotFlowerPower service.
4
 *
5
 * @author  Pedro Amorim <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 * @link    https://flowerpowerdev.parrot.com/projects/flower-power-web-service-api/wiki
8
 */
9
10
namespace OAuth\OAuth2\Service;
11
12
use OAuth\OAuth2\Token\StdOAuth2Token;
13
use OAuth\Common\Http\Exception\TokenResponseException;
14
use OAuth\Common\Http\Uri\Uri;
15
use OAuth\OAuth2\Service\Exception\MissingRefreshTokenException;
16
use OAuth\Common\Token\TokenInterface;
17
18
/**
19
 * ParrotFlowerPower service.
20
 *
21
 * @author  Pedro Amorim <[email protected]>
22
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
23
 * @link    https://flowerpowerdev.parrot.com/projects/flower-power-web-service-api/wiki
24
 */
25
class ParrotFlowerPower extends AbstractService
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function init()
31
    {
32
        $this->stateParameterInAuthUrl = true;
33
34
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
35
            $this->baseApiUri = new Uri('https://apiflowerpower.parrot.com/');
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getAuthorizationEndpoint()
43
    {
44
        return new Uri($this->baseApiUri.'oauth2/v1/authorize');
45
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getAccessTokenEndpoint()
52
    {
53
        return new Uri($this->baseApiUri.'user/v1/authenticate');
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function getAuthorizationMethod()
60
    {
61
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function parseAccessTokenResponse($responseBody)
68
    {
69
        $data = json_decode($responseBody, true);
70
71
        if (null === $data || !is_array($data)) {
72
            throw new TokenResponseException('Unable to parse response.');
73
        } elseif (isset($data['error'])) {
74
            throw new TokenResponseException(
75
                'Error in retrieving token: "' . $data['error'] . '"'
76
            );
77
        }
78
79
        $token = new StdOAuth2Token();
80
        $token->setAccessToken($data['access_token']);
81
        $token->setLifetime($data['expires_in']);
82
83
        if (isset($data['refresh_token'])) {
84
            $token->setRefreshToken($data['refresh_token']);
85
            unset($data['refresh_token']);
86
        }
87
88
        unset($data['access_token']);
89
        unset($data['expires_in']);
90
91
        $token->setExtraParams($data);
92
93
        return $token;
94
    }
95
96
97
    /**
98
     * Parrot use a different endpoint for refresh a token
99
     *
100
     * {@inheritdoc}
101
     */
102
    public function refreshAccessToken(TokenInterface $token)
103
    {
104
        $refreshToken = $token->getRefreshToken();
105
106
        if (empty($refreshToken)) {
107
            throw new MissingRefreshTokenException();
108
        }
109
110
        $parameters = array(
111
            'grant_type'    => 'refresh_token',
112
            'type'          => 'web_server',
113
            'client_id'     => $this->credentials->getConsumerId(),
114
            'client_secret' => $this->credentials->getConsumerSecret(),
115
            'refresh_token' => $refreshToken,
116
        );
117
118
        $responseBody = $this->httpClient->retrieveResponse(
119
            new Uri($this->baseApiUri.'user/v1/refresh'),
120
            $parameters,
121
            $this->getExtraOAuthHeaders()
122
        );
123
        $token = $this->parseAccessTokenResponse($responseBody);
124
        $this->storage->storeAccessToken($this->service(), $token, $this->account());
125
126
        return $token;
127
    }
128
}
129