[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/lusitanian/oauth/src/OAuth/OAuth2/Service/ -> Mailchimp.php (source)

   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  use OAuth\Common\Consumer\CredentialsInterface;
   9  use OAuth\Common\Http\Client\ClientInterface;
  10  use OAuth\Common\Storage\TokenStorageInterface;
  11  use OAuth\Common\Http\Uri\UriInterface;
  12  
  13  class Mailchimp extends AbstractService
  14  {
  15      public function __construct(
  16          CredentialsInterface $credentials,
  17          ClientInterface $httpClient,
  18          TokenStorageInterface $storage,
  19          $scopes = array(),
  20          UriInterface $baseApiUri = null
  21      ) {
  22          parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  23  
  24          if (is_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 = array())
  84      {
  85          if (is_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       * @param StdOAuth2Token $token
  96       */
  97      protected function setBaseApiUri(StdOAuth2Token $token)
  98      {
  99          // Make request uri.
 100          $endpoint = 'https://login.mailchimp.com/oauth2/metadata?oauth_token='. $token->getAccessToken();
 101  
 102          // Grab meta data about the token.
 103          $response = $this->httpClient->retrieveResponse(new Uri($endpoint), array(), array(), 'GET');
 104  
 105          // Parse JSON.
 106          $meta = json_decode($response, true);
 107  
 108          // Set base api uri.
 109          $this->baseApiUri = new Uri('https://'. $meta['dc'] .'.api.mailchimp.com/2.0/');
 110  
 111          // Allow chaining.
 112          return $this;
 113      }
 114  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1