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