|
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
|
|
|
|
|
9
|
|
|
class Mailchimp extends AbstractService |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritdoc} |
|
13
|
|
|
*/ |
|
14
|
|
|
protected function init() |
|
15
|
|
|
{ |
|
16
|
|
|
if (is_null($this->baseApiUri) && $this->getStorage()->hasAccessToken($this->service(), $this->account())) { |
|
17
|
|
|
$this->setBaseApiUri($this->getStorage()->retrieveAccessToken($this->service(), $this->account())); |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function getAuthorizationMethod() |
|
25
|
|
|
{ |
|
26
|
|
|
return static::AUTHORIZATION_METHOD_QUERY_STRING_V3; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getAuthorizationEndpoint() |
|
33
|
|
|
{ |
|
34
|
|
|
return new Uri('https://login.mailchimp.com/oauth2/authorize'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getAccessTokenEndpoint() |
|
41
|
|
|
{ |
|
42
|
|
|
return new Uri('https://login.mailchimp.com/oauth2/token'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function parseAccessTokenResponse($responseBody) |
|
49
|
|
|
{ |
|
50
|
|
|
// Parse JSON |
|
51
|
|
|
$data = json_decode($responseBody, true); |
|
52
|
|
|
|
|
53
|
|
|
// Do validation. |
|
54
|
|
|
if (null === $data || !is_array($data)) { |
|
55
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
|
56
|
|
|
} elseif (isset($data['error'])) { |
|
57
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Create token object. |
|
61
|
|
|
$token = new StdOAuth2Token($data['access_token']); |
|
62
|
|
|
|
|
63
|
|
|
// Set the right API endpoint. |
|
64
|
|
|
$this->setBaseApiUri($token); |
|
65
|
|
|
|
|
66
|
|
|
// Mailchimp tokens evidently never expire... |
|
67
|
|
|
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); |
|
68
|
|
|
|
|
69
|
|
|
return $token; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function request($path, $method = 'GET', $body = null, array $extraHeaders = array()) |
|
76
|
|
|
{ |
|
77
|
|
|
if (is_null($this->baseApiUri)) { |
|
78
|
|
|
$this->setBaseApiUri($this->storage->retrieveAccessToken($this->service(), $this->account())); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return parent::request($path, $method, $body, $extraHeaders); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set the right base endpoint. |
|
86
|
|
|
* |
|
87
|
|
|
* @param StdOAuth2Token $token |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function setBaseApiUri(StdOAuth2Token $token) |
|
90
|
|
|
{ |
|
91
|
|
|
// Make request uri. |
|
92
|
|
|
$endpoint = 'https://login.mailchimp.com/oauth2/metadata?oauth_token='. $token->getAccessToken(); |
|
93
|
|
|
|
|
94
|
|
|
// Grab meta data about the token. |
|
95
|
|
|
$response = $this->httpClient->retrieveResponse(new Uri($endpoint), array(), array(), 'GET'); |
|
96
|
|
|
|
|
97
|
|
|
// Parse JSON. |
|
98
|
|
|
$meta = json_decode($response, true); |
|
99
|
|
|
|
|
100
|
|
|
// Set base api uri. |
|
101
|
|
|
$this->baseApiUri = new Uri('https://'. $meta['dc'] .'.api.mailchimp.com/2.0/'); |
|
102
|
|
|
|
|
103
|
|
|
// Allow chaining. |
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
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.