Completed
Push — master ( 29bda6...e00326 )
by David
9s
created

Mailchimp   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 102
rs 10
c 1
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getAuthorizationMethod() 0 4 1
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
B parseAccessTokenResponse() 0 23 4
A request() 0 8 2
A setBaseApiUri() 0 17 1
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 Mailchimp 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);
23
24
        if (is_null($this->baseApiUri) && $storage->hasAccessToken($this->service())) {
25
            $this->setBaseApiUri($storage->retrieveAccessToken($this->service()));
0 ignored issues
show
Compatibility introduced by
$storage->retrieveAccessToken($this->service()) of type object<OAuth\Common\Token\TokenInterface> is not a sub-type of object<OAuth\OAuth2\Token\StdOAuth2Token>. It seems like you assume a concrete implementation of the interface OAuth\Common\Token\TokenInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
26
        }
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function getAuthorizationMethod()
33
    {
34
        return static::AUTHORIZATION_METHOD_QUERY_STRING_V3;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getAuthorizationEndpoint()
41
    {
42
        return new Uri('https://login.mailchimp.com/oauth2/authorize');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getAccessTokenEndpoint()
49
    {
50
        return new Uri('https://login.mailchimp.com/oauth2/token');
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function parseAccessTokenResponse($responseBody)
57
    {
58
        // Parse JSON
59
        $data = json_decode($responseBody, true);
60
61
        // Do validation.
62
        if (null === $data || !is_array($data)) {
63
            throw new TokenResponseException('Unable to parse response.');
64
        } elseif (isset($data['error'])) {
65
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
66
        }
67
68
        // Create token object.
69
        $token = new StdOAuth2Token($data['access_token']);
70
71
        // Set the right API endpoint.
72
        $this->setBaseApiUri($token);
73
74
        // Mailchimp tokens evidently never expire...
75
        $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
76
77
        return $token;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
84
    {
85
        if (is_null($this->baseApiUri)) {
86
            $this->setBaseApiUri($this->storage->retrieveAccessToken($this->service()));
0 ignored issues
show
Compatibility introduced by
$this->storage->retrieve...Token($this->service()) of type object<OAuth\Common\Token\TokenInterface> is not a sub-type of object<OAuth\OAuth2\Token\StdOAuth2Token>. It seems like you assume a concrete implementation of the interface OAuth\Common\Token\TokenInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
87
        }
88
89
        return parent::request($path, $method, $body, $extraHeaders);
90
    }
91
92
    /**
93
     * Set the right base endpoint.
94
     *
95
     * @param StdOAuth2Token $token
96
     */
97
    protected function setBaseApiUri(StdOAuth2Token $token)
98
    {
99
        // Make request uri.
100
        $endpoint = 'https://login.mailchimp.com/oauth2/metadata?oauth_token='. $token->getAccessToken();
101
102
        // Grab meta data about the token.
103
        $response = $this->httpClient->retrieveResponse(new Uri($endpoint), array(), array(), 'GET');
104
105
        // Parse JSON.
106
        $meta = json_decode($response, true);
107
108
        // Set base api uri.
109
        $this->baseApiUri = new Uri('https://'. $meta['dc'] .'.api.mailchimp.com/2.0/');
110
111
        // Allow chaining.
112
        return $this;
113
    }
114
}
115