Completed
Push — master ( 059662...eed06e )
by David
12:29 queued 09:47
created

Mondo   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 11
c 2
b 0
f 2
lcom 0
cbo 4
dl 0
loc 74
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
A getAuthorizationMethod() 0 4 1
B parseAccessTokenResponse() 0 30 6
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
use OAuth\Common\Consumer\CredentialsInterface;
9
use OAuth\Common\Http\Client\ClientInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\Common\Http\Uri\UriInterface;
12
13
class Mondo extends AbstractService
14
{
15
    public function __construct(
16
        CredentialsInterface $credentials,
17
        ClientInterface $httpClient,
18
        TokenStorageInterface $storage,
19
        $scopes = array(),
20
        UriInterface $baseApiUri = null
21
    ) {
22
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
23
24
        if (null === $baseApiUri) {
25
            $this->baseApiUri = new Uri('https://api.getmondo.co.uk');
26
        }
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getAuthorizationEndpoint()
33
    {
34
        return new Uri('https://auth.getmondo.co.uk');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getAccessTokenEndpoint()
41
    {
42
        return new Uri('https://api.getmondo.co.uk/oauth2/token');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function getAuthorizationMethod()
49
    {
50
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function parseAccessTokenResponse($responseBody)
57
    {
58
        $data = json_decode($responseBody, true);
59
60
        if (null === $data || !is_array($data)) {
61
            throw new TokenResponseException('Unable to parse response.');
62
        } elseif (isset($data['error'])) {
63
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
64
        }
65
66
67
        $token = new StdOAuth2Token();
68
        $token->setAccessToken($data['access_token']);
69
70
        if (isset($data['expires_in'])) {
71
            $token->setLifetime($data['expires_in']);
72
            unset($data['expires_in']);
73
        }
74
75
        if (isset($data['refresh_token'])) {
76
            $token->setRefreshToken($data['refresh_token']);
77
            unset($data['refresh_token']);
78
        }
79
80
        unset($data['access_token']);
81
82
        $token->setExtraParams($data);
83
84
        return $token;
85
    }
86
}
87