Mailchimp   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 24
dl 0
loc 98
c 2
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizationEndpoint() 0 3 1
A setBaseApiUri() 0 16 1
A getAccessTokenEndpoint() 0 3 1
A __construct() 0 11 3
A getAuthorizationMethod() 0 3 1
A parseAccessTokenResponse() 0 22 4
A request() 0 7 2
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Consumer\CredentialsInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Http\Uri\UriInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\OAuth2\Token\StdOAuth2Token;
12
13
class Mailchimp extends AbstractService
14
{
15
    public function __construct(
16
        CredentialsInterface $credentials,
17
        ClientInterface $httpClient,
18
        TokenStorageInterface $storage,
19
        $scopes = [],
20
        ?UriInterface $baseApiUri = null
21
    ) {
22
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
23
24
        if (null === $this->baseApiUri && $storage->hasAccessToken($this->service())) {
25
            $this->setBaseApiUri($storage->retrieveAccessToken($this->service()));
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 = [])
84
    {
85
        if (null === $this->baseApiUri) {
86
            $this->setBaseApiUri($this->storage->retrieveAccessToken($this->service()));
87
        }
88
89
        return parent::request($path, $method, $body, $extraHeaders);
90
    }
91
92
    /**
93
     * Set the right base endpoint.
94
     */
95
    protected function setBaseApiUri(StdOAuth2Token $token)
96
    {
97
        // Make request uri.
98
        $endpoint = 'https://login.mailchimp.com/oauth2/metadata?oauth_token=' . $token->getAccessToken();
99
100
        // Grab meta data about the token.
101
        $response = $this->httpClient->retrieveResponse(new Uri($endpoint), [], [], 'GET');
102
103
        // Parse JSON.
104
        $meta = json_decode($response, true);
105
106
        // Set base api uri.
107
        $this->baseApiUri = new Uri('https://' . $meta['dc'] . '.api.mailchimp.com/2.0/');
108
109
        // Allow chaining.
110
        return $this;
111
    }
112
}
113