1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class MailChimp |
4
|
|
|
* |
5
|
|
|
* @link http://developer.mailchimp.com/ |
6
|
|
|
* @link http://developer.mailchimp.com/documentation/mailchimp/guides/how-to-use-oauth2/ |
7
|
|
|
* |
8
|
|
|
* @filesource MailChimp.php |
9
|
|
|
* @created 16.08.2018 |
10
|
|
|
* @package chillerlan\OAuth\Providers\MailChimp |
11
|
|
|
* @author smiley <[email protected]> |
12
|
|
|
* @copyright 2018 smiley |
13
|
|
|
* @license MIT |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace chillerlan\OAuth\Providers\MailChimp; |
17
|
|
|
|
18
|
|
|
use chillerlan\OAuth\Core\{AccessToken, CSRFToken, OAuth2Provider}; |
19
|
|
|
use chillerlan\OAuth\OAuthException; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
22
|
|
|
use function array_merge, sprintf; |
23
|
|
|
use function chillerlan\HTTP\Psr7\get_json; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @method \Psr\Http\Message\ResponseInterface createAuthorizedApp(array $body = ['client_id', 'client_secret']) |
27
|
|
|
* @method \Psr\Http\Message\ResponseInterface getAuthorizedApp(array $params = ['fields', 'exclude_fields']) |
28
|
|
|
* @method \Psr\Http\Message\ResponseInterface getAuthorizedAppList(array $params = ['fields', 'exclude_fields', 'count', 'offset']) |
29
|
|
|
* @method \Psr\Http\Message\ResponseInterface ping() |
30
|
|
|
* @method \Psr\Http\Message\ResponseInterface root(array $params = ['fields', 'exclude_fields']) |
31
|
|
|
*/ |
32
|
|
|
class MailChimp extends OAuth2Provider implements CSRFToken{ |
33
|
|
|
|
34
|
|
|
protected const API_BASE = 'https://%s.api.mailchimp.com/3.0'; |
35
|
|
|
protected const METADATA_ENDPOINT = 'https://login.mailchimp.com/oauth2/metadata'; |
36
|
|
|
|
37
|
|
|
protected string $authURL = 'https://login.mailchimp.com/oauth2/authorize'; |
38
|
|
|
protected string $accessTokenURL = 'https://login.mailchimp.com/oauth2/token'; |
39
|
|
|
protected ?string $endpointMap = MailChimpEndpoints::class; |
40
|
|
|
protected ?string $apiDocs = 'https://developer.mailchimp.com/'; |
41
|
|
|
protected ?string $applicationURL = 'https://admin.mailchimp.com/account/oauth2/'; |
42
|
|
|
protected string $authMethodHeader = 'OAuth'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \chillerlan\OAuth\Core\AccessToken|null $token |
46
|
|
|
* |
47
|
|
|
* @return \chillerlan\OAuth\Core\AccessToken |
48
|
|
|
* @throws \chillerlan\OAuth\OAuthException |
49
|
|
|
*/ |
50
|
|
|
public function getTokenMetadata(AccessToken $token = null):AccessToken{ |
51
|
|
|
|
52
|
|
|
$token ??= $this->storage->getAccessToken($this->serviceName); |
53
|
|
|
|
54
|
|
|
if(!$token instanceof AccessToken){ |
|
|
|
|
55
|
|
|
throw new OAuthException('invalid token'); // @codeCoverageIgnore |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$request = $this->requestFactory |
59
|
|
|
->createRequest('GET', $this::METADATA_ENDPOINT) |
60
|
|
|
->withHeader('Authorization', 'OAuth '.$token->accessToken) |
61
|
|
|
; |
62
|
|
|
|
63
|
|
|
$response = $this->http->sendRequest($request); |
64
|
|
|
|
65
|
|
|
if($response->getStatusCode() !== 200){ |
66
|
|
|
throw new OAuthException('metadata response error'); // @codeCoverageIgnore |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$token->extraParams = array_merge($token->extraParams, get_json($response, true)); |
70
|
|
|
|
71
|
|
|
$this->storage->storeAccessToken($this->serviceName, $token); |
72
|
|
|
|
73
|
|
|
return $token; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* prepare the API URL from the token metadata |
78
|
|
|
* |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function request( |
82
|
|
|
string $path, |
83
|
|
|
array $params = null, |
84
|
|
|
string $method = null, |
85
|
|
|
$body = null, |
86
|
|
|
array $headers = null |
87
|
|
|
):ResponseInterface{ |
88
|
|
|
$token = $this->storage->getAccessToken($this->serviceName); |
89
|
|
|
|
90
|
|
|
$this->apiURL = sprintf($this::API_BASE, $token->extraParams['dc']); |
91
|
|
|
|
92
|
|
|
return parent::request($path, $params, $method, $body, $headers); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|