You are here

public function BrightcoveAPIClient::authorizeClient in Brightcove Video Connect 3.x

Same name and namespace in other branches
  1. 8.2 src/Entity/BrightcoveAPIClient.php \Drupal\brightcove\Entity\BrightcoveAPIClient::authorizeClient()
  2. 8 src/Entity/BrightcoveAPIClient.php \Drupal\brightcove\Entity\BrightcoveAPIClient::authorizeClient()

Authorize client with Brightcove API and store client on the entity.

Return value

$this

Throws

\Brightcove\API\Exception\AuthenticationException|\Exception Re-throw any exception to be able to handle them nicely.

1 call to BrightcoveAPIClient::authorizeClient()
BrightcoveAPIClient::getClient in src/Entity/BrightcoveAPIClient.php
Returns the loaded API client.

File

src/Entity/BrightcoveAPIClient.php, line 284

Class

BrightcoveAPIClient
Defines the Brightcove API Client entity.

Namespace

Drupal\brightcove\Entity

Code

public function authorizeClient() {
  try {

    // Use the got access token while it is not expired.
    if ($this->key_value_expirable_store
      ->has($this->client_id)) {

      // Create new client.
      $this
        ->setClient(new Client($this
        ->getAccessToken()));
    }
    else {
      $client = Client::authorize($this->client_id, $this->secret_key);

      // Update access information. This will ensure that in the current
      // session we will get the correct access data.
      // Set token expire time in seconds and subtract the default php
      // max_execution_time from it.
      // We have to use the default php max_execution_time because if we
      // would get the value from ini_get('max_execution_time'), then it
      // could be larger than the Brightcove's expire date causing to always
      // get a new access token.
      $this
        ->setAccessToken($client
        ->getAccessToken(), intval($client
        ->getExpiresIn()) - 30);

      // Create new client.
      $this
        ->setClient(new Client($this
        ->getAccessToken()));
    }

    // Test account ID.
    $cms = new CMS($this->client, $this->account_id);
    $cms
      ->countVideos();

    // If client authentication was successful store it's state on the
    // entity.
    $this->client_status = self::CLIENT_OK;
  } catch (\Exception $e) {
    if ($e instanceof APIException) {

      // If we got an unauthorized error, try to re-authorize the client
      // only once.
      if ($e
        ->getCode() == 401 && !$this->re_authorization_tried) {
        $this->re_authorization_tried = TRUE;
        $this
          ->authorizeClient();
      }
    }

    // Store an error status and message on the entity if there was an
    // error.
    $this->client_status = self::CLIENT_ERROR;
    $this->client_status_message = $e
      ->getMessage();

    // If we have already tried to re-authorize the client, throw the
    // exception outside of this scope, to be able to catch this Exception
    // for better error handling.
    if ($e
      ->getCode() != 401 && !$this->re_authorization_tried || $e
      ->getCode() == 401 && $this->re_authorization_tried) {
      watchdog_exception('brightcove', $e, $e
        ->getMessage());
      throw $e;
    }
  }
  return $this;
}