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