You are here

class BrightcoveAPIClient in Brightcove Video Connect 8.2

Same name and namespace in other branches
  1. 8 src/Entity/BrightcoveAPIClient.php \Drupal\brightcove\Entity\BrightcoveAPIClient
  2. 3.x src/Entity/BrightcoveAPIClient.php \Drupal\brightcove\Entity\BrightcoveAPIClient

Defines the Brightcove API Client entity.

Plugin annotation


@ConfigEntityType(
  id = "brightcove_api_client",
  label = @Translation("Brightcove API Client"),
  handlers = {
    "list_builder" = "Drupal\brightcove\BrightcoveAPIClientListBuilder",
    "form" = {
      "add" = "Drupal\brightcove\Form\BrightcoveAPIClientForm",
      "edit" = "Drupal\brightcove\Form\BrightcoveAPIClientForm",
      "delete" = "Drupal\brightcove\Form\BrightcoveAPIClientDeleteForm"
    },
  },
  config_prefix = "brightcove_api_client",
  admin_permission = "administer brightcove configuration",
  entity_keys = {
    "id" = "id",
    "label" = "label",
    "uuid" = "uuid"
  },
  links = {
    "add-form" = "/admin/config/media/brightcove_api_client/add",
    "edit-form" = "/admin/config/media/brightcove_api_client/{brightcove_api_client}",
    "delete-form" = "/admin/config/media/brightcove_api_client/{brightcove_api_client}/delete",
    "collection" = "/admin/config/media/brightcove_api_client"
  }
)

Hierarchy

Expanded class hierarchy of BrightcoveAPIClient

6 files declare their use of BrightcoveAPIClient
brightcove.install in ./brightcove.install
Brightcove install file.
brightcove.module in ./brightcove.module
Brightcove module.
BrightcoveAPIClientForm.php in src/Form/BrightcoveAPIClientForm.php
BrightcoveSubscriptionController.php in src/Controller/BrightcoveSubscriptionController.php
BrightcoveSubscriptionForm.php in src/Form/BrightcoveSubscriptionForm.php

... See full list

File

src/Entity/BrightcoveAPIClient.php, line 40

Namespace

Drupal\brightcove\Entity
View source
class BrightcoveAPIClient extends ConfigEntityBase implements BrightcoveAPIClientInterface {

  /**
   * The Brightcove API Client ID (Drupal-internal).
   *
   * @var string
   */
  protected $id;

  /**
   * The Brightcove API Client label.
   *
   * @var string
   */
  protected $label;

  /**
   * The Brightcove API Client account ID.
   *
   * @var string
   */
  protected $account_id;

  /**
   * The Brightcove API Client ID.
   *
   * @var string
   */
  protected $client_id;

  /**
   * The Brightcove API Client default player.
   *
   * @var string
   */
  protected $default_player;

  /**
   * The Brightcove API Client secret key.
   *
   * @var string
   */
  protected $secret_key;

  /**
   * The loaded API client.
   *
   * @var \Brightcove\API\Client
   */
  protected $client;

  /**
   * Client connection status.
   *
   * @var int
   */
  protected $client_status;

  /**
   * Client connection status message.
   *
   * If the connection status is OK, then it's an empty string.
   *
   * @var string
   */
  protected $client_status_message;

  /**
   * Indicate if there was an re-authorization attempt or not.
   *
   * @var bool
   */
  private $re_authorization_tried = FALSE;

  /**
   * Maximum number of Custom fields.
   *
   * @var array
   */
  protected $max_custom_fields;

  /**
   * Expirable key/value store for the client.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   */
  protected $key_value_expirable_store;

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->label;
  }

  /**
   * {@inheritdoc}
   */
  public function getAccountId() {
    return $this->account_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getClientId() {
    return $this->client_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultPlayer() {
    return $this->default_player;
  }

  /**
   * {@inheritdoc}
   */
  public function getSecretKey() {
    return $this->secret_key;
  }

  /**
   * {@inheritdoc}
   */
  public function getClient() {
    $this
      ->authorizeClient();
    return $this->client;
  }

  /**
   * {@inheritdoc}
   */
  public function getClientStatus() {
    return $this->client_status;
  }

  /**
   * {@inheritdoc}
   */
  public function getClientStatusMessage() {
    return $this->client_status_message;
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessToken() {
    return $this->key_value_expirable_store
      ->get($this->client_id, NULL);
  }

  /**
   * {@inheritdoc}
   */
  public function getMaxCustomFields() {
    return $this->max_custom_fields;
  }

  /**
   * {@inheritdoc}
   */
  public function setLabel($label) {
    $this->label = $label;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setAccountId($account_id) {
    $this->account_id = $account_id;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setClientId($client_id) {
    $this->client_id = $client_id;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setDefaultPlayer($default_player) {
    $this->default_player = $default_player;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setSecretKey($secret_key) {
    $this->secret_key = $secret_key;
    return $this;
  }

  /**
   * Set Brightcove API client.
   *
   * @param \Brightcove\API\Client $client
   *   The initialized Brightcove API Client.
   *
   * @return $this
   */
  public function setClient(Client $client) {
    $this->client = $client;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setAccessToken($access_token, $expire) {
    $this->key_value_expirable_store
      ->setWithExpire($this->client_id, $access_token, $expire);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setMaxCustomFields($max_custom_fields) {
    $this->max_custom_fields = $max_custom_fields;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function __construct(array $values, $entity_type) {
    parent::__construct($values, $entity_type);
    $this->key_value_expirable_store = \Drupal::keyValueExpirable('brightcove_access_token');
  }

  /**
   * Authorize client with Brightcove API and store client on the entity.
   *
   * @return $this
   *
   * @throws \Brightcove\API\Exception\AuthenticationException|\Exception
   *   Re-throw any exception to be able to handle them nicely.
   */
  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;
  }

  /**
   * Loads API client by the Brightcove account ID.
   *
   * @param string $account_id
   *   Brightcove account ID.
   *
   * @return \Drupal\brightcove\Entity\BrightcoveAPIClient|null
   *   Loaded BrightcoveAPIClient entity or NULL if cannot be found.
   */
  public static function loadByAccountId($account_id) {
    $api_client_ids = \Drupal::entityQuery('brightcove_api_client')
      ->condition('account_id', $account_id)
      ->execute();
    if (!empty($api_client_ids)) {
      return self::load(reset($api_client_ids));
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BrightcoveAPIClient::$account_id protected property The Brightcove API Client account ID.
BrightcoveAPIClient::$client protected property The loaded API client.
BrightcoveAPIClient::$client_id protected property The Brightcove API Client ID.
BrightcoveAPIClient::$client_status protected property Client connection status.
BrightcoveAPIClient::$client_status_message protected property Client connection status message.
BrightcoveAPIClient::$default_player protected property The Brightcove API Client default player.
BrightcoveAPIClient::$id protected property The Brightcove API Client ID (Drupal-internal).
BrightcoveAPIClient::$key_value_expirable_store protected property Expirable key/value store for the client.
BrightcoveAPIClient::$label protected property The Brightcove API Client label.
BrightcoveAPIClient::$max_custom_fields protected property Maximum number of Custom fields.
BrightcoveAPIClient::$re_authorization_tried private property Indicate if there was an re-authorization attempt or not.
BrightcoveAPIClient::$secret_key protected property The Brightcove API Client secret key.
BrightcoveAPIClient::authorizeClient public function Authorize client with Brightcove API and store client on the entity.
BrightcoveAPIClient::getAccessToken public function Returns access token. Overrides BrightcoveAPIClientInterface::getAccessToken
BrightcoveAPIClient::getAccountId public function Returns the API Client account ID. Overrides BrightcoveAPIClientInterface::getAccountId
BrightcoveAPIClient::getClient public function Returns the loaded API client. Overrides BrightcoveAPIClientInterface::getClient
BrightcoveAPIClient::getClientId public function Returns the API Client ID. Overrides BrightcoveAPIClientInterface::getClientId
BrightcoveAPIClient::getClientStatus public function Returns the connection status. Overrides BrightcoveAPIClientInterface::getClientStatus
BrightcoveAPIClient::getClientStatusMessage public function Returns the connection status message. Overrides BrightcoveAPIClientInterface::getClientStatusMessage
BrightcoveAPIClient::getDefaultPlayer public function Returns the API Client default player. Overrides BrightcoveAPIClientInterface::getDefaultPlayer
BrightcoveAPIClient::getLabel public function Returns the API Client label. Overrides BrightcoveAPIClientInterface::getLabel
BrightcoveAPIClient::getMaxCustomFields public function Returns the maximum number of addable custom fields. Overrides BrightcoveAPIClientInterface::getMaxCustomFields
BrightcoveAPIClient::getSecretKey public function Returns the API Client secret key. Overrides BrightcoveAPIClientInterface::getSecretKey
BrightcoveAPIClient::loadByAccountId public static function Loads API client by the Brightcove account ID.
BrightcoveAPIClient::setAccessToken public function Sets access token. Overrides BrightcoveAPIClientInterface::setAccessToken
BrightcoveAPIClient::setAccountId public function Sets the API Client account ID. Overrides BrightcoveAPIClientInterface::setAccountId
BrightcoveAPIClient::setClient public function Set Brightcove API client.
BrightcoveAPIClient::setClientId public function Sets the API Client ID. Overrides BrightcoveAPIClientInterface::setClientId
BrightcoveAPIClient::setDefaultPlayer public function Sets the API Client default player. Overrides BrightcoveAPIClientInterface::setDefaultPlayer
BrightcoveAPIClient::setLabel public function Sets the API Client label. Overrides BrightcoveAPIClientInterface::setLabel
BrightcoveAPIClient::setMaxCustomFields public function Sets the maximum addable custom fields number. Overrides BrightcoveAPIClientInterface::setMaxCustomFields
BrightcoveAPIClient::setSecretKey public function Sets the API Client secret key. Overrides BrightcoveAPIClientInterface::setSecretKey
BrightcoveAPIClient::__construct public function Constructs an Entity object. Overrides ConfigEntityBase::__construct
BrightcoveAPIClientInterface::CLIENT_ERROR constant Indicates that the connection to the API was not successful.
BrightcoveAPIClientInterface::CLIENT_OK constant Indicates that the connection to the API was successful.
BrightcoveAPIClientInterface::DEFAULT_PLAYER constant Indicates the default player for any API Client.
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
ConfigEntityBase::$isUninstalling private property Whether the config is being deleted by the uninstall process.
ConfigEntityBase::$langcode protected property The language code of the entity's default language.
ConfigEntityBase::$originalId protected property The original ID of the configuration entity.
ConfigEntityBase::$status protected property The enabled/disabled status of the configuration entity. 4
ConfigEntityBase::$third_party_settings protected property Third party entity settings.
ConfigEntityBase::$trustedData protected property Trust supplied data and not use configuration schema on save.
ConfigEntityBase::$uuid protected property The UUID for this entity.
ConfigEntityBase::$_core protected property Information maintained by Drupal core about configuration.
ConfigEntityBase::addDependency protected function Overrides \Drupal\Core\Entity\DependencyTrait:addDependency().
ConfigEntityBase::calculateDependencies public function Calculates dependencies and stores them in the dependency property. Overrides ConfigEntityInterface::calculateDependencies 13
ConfigEntityBase::createDuplicate public function Creates a duplicate of the entity. Overrides EntityBase::createDuplicate 1
ConfigEntityBase::disable public function Disables the configuration entity. Overrides ConfigEntityInterface::disable 1
ConfigEntityBase::enable public function Enables the configuration entity. Overrides ConfigEntityInterface::enable
ConfigEntityBase::get public function Returns the value of a property. Overrides ConfigEntityInterface::get
ConfigEntityBase::getCacheTagsToInvalidate public function Returns the cache tags that should be used to invalidate caches. Overrides EntityBase::getCacheTagsToInvalidate 1
ConfigEntityBase::getConfigDependencyName public function Gets the configuration dependency name. Overrides EntityBase::getConfigDependencyName
ConfigEntityBase::getConfigManager protected static function Gets the configuration manager.
ConfigEntityBase::getConfigTarget public function Gets the configuration target identifier for the entity. Overrides EntityBase::getConfigTarget
ConfigEntityBase::getDependencies public function Gets the configuration dependencies. Overrides ConfigEntityInterface::getDependencies
ConfigEntityBase::getOriginalId public function Gets the original ID. Overrides EntityBase::getOriginalId
ConfigEntityBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
ConfigEntityBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
ConfigEntityBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
ConfigEntityBase::getTypedConfig protected function Gets the typed config manager.
ConfigEntityBase::hasTrustedData public function Gets whether on not the data is trusted. Overrides ConfigEntityInterface::hasTrustedData
ConfigEntityBase::invalidateTagsOnDelete protected static function Override to never invalidate the individual entities' cache tags; the config system already invalidates them. Overrides EntityBase::invalidateTagsOnDelete
ConfigEntityBase::invalidateTagsOnSave protected function Override to never invalidate the entity's cache tag; the config system already invalidates it. Overrides EntityBase::invalidateTagsOnSave
ConfigEntityBase::isInstallable public function Checks whether this entity is installable. Overrides ConfigEntityInterface::isInstallable 2
ConfigEntityBase::isNew public function Overrides Entity::isNew(). Overrides EntityBase::isNew
ConfigEntityBase::isUninstalling public function Returns whether this entity is being changed during the uninstall process. Overrides ConfigEntityInterface::isUninstalling
ConfigEntityBase::link public function Deprecated way of generating a link to the entity. See toLink(). Overrides EntityBase::link
ConfigEntityBase::onDependencyRemoval public function Informs the entity that entities it depends on will be deleted. Overrides ConfigEntityInterface::onDependencyRemoval 7
ConfigEntityBase::preDelete public static function Acts on entities before they are deleted and before hooks are invoked. Overrides EntityBase::preDelete 8
ConfigEntityBase::preSave public function Acts on an entity before the presave hook is invoked. Overrides EntityBase::preSave 13
ConfigEntityBase::save public function Saves an entity permanently. Overrides EntityBase::save 1
ConfigEntityBase::set public function Sets the value of a property. Overrides ConfigEntityInterface::set
ConfigEntityBase::setOriginalId public function Sets the original ID. Overrides EntityBase::setOriginalId
ConfigEntityBase::setStatus public function Sets the status of the configuration entity. Overrides ConfigEntityInterface::setStatus
ConfigEntityBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
ConfigEntityBase::setUninstalling public function
ConfigEntityBase::sort public static function Helper callback for uasort() to sort configuration entities by weight and label. 6
ConfigEntityBase::status public function Returns whether the configuration entity is enabled. Overrides ConfigEntityInterface::status 4
ConfigEntityBase::toArray public function Gets an array of all property values. Overrides EntityBase::toArray 2
ConfigEntityBase::toUrl public function Gets the URL object for the entity. Overrides EntityBase::toUrl
ConfigEntityBase::trustData public function Sets that the data should be trusted. Overrides ConfigEntityInterface::trustData
ConfigEntityBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
ConfigEntityBase::url public function Gets the public URL for this entity. Overrides EntityBase::url
ConfigEntityBase::urlInfo public function Gets the URL object for the entity. Overrides EntityBase::urlInfo
ConfigEntityBase::__sleep public function Overrides EntityBase::__sleep 4
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Aliased as: traitSleep 1
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency. Aliased as: addDependencyTrait
EntityBase::$enforceIsNew protected property Boolean indicating whether the entity should be forced to be new.
EntityBase::$entityTypeId protected property The entity type.
EntityBase::$typedData protected property A typed data object wrapping this entity.
EntityBase::access public function Checks data value access. Overrides AccessibleInterface::access 1
EntityBase::bundle public function Gets the bundle of the entity. Overrides EntityInterface::bundle 1
EntityBase::create public static function Constructs a new entity object, without permanently saving it. Overrides EntityInterface::create
EntityBase::delete public function Deletes an entity permanently. Overrides EntityInterface::delete 2
EntityBase::enforceIsNew public function Enforces an entity to be new. Overrides EntityInterface::enforceIsNew
EntityBase::entityManager Deprecated protected function Gets the entity manager.
EntityBase::entityTypeBundleInfo protected function Gets the entity type bundle info service.
EntityBase::entityTypeManager protected function Gets the entity type manager.
EntityBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyTrait::getCacheContexts
EntityBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyTrait::getCacheMaxAge
EntityBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyTrait::getCacheTags
EntityBase::getConfigDependencyKey public function Gets the key that is used to store configuration dependencies. Overrides EntityInterface::getConfigDependencyKey
EntityBase::getEntityType public function Gets the entity type definition. Overrides EntityInterface::getEntityType
EntityBase::getEntityTypeId public function Gets the ID of the type of the entity. Overrides EntityInterface::getEntityTypeId
EntityBase::getListCacheTagsToInvalidate protected function The list cache tags to invalidate for this entity.
EntityBase::getTypedData public function Gets a typed data object for this entity object. Overrides EntityInterface::getTypedData
EntityBase::hasLinkTemplate public function Indicates if a link template exists for a given key. Overrides EntityInterface::hasLinkTemplate
EntityBase::id public function Gets the identifier. Overrides EntityInterface::id 11
EntityBase::label public function Gets the label of the entity. Overrides EntityInterface::label 6
EntityBase::language public function Gets the language of the entity. Overrides EntityInterface::language 1
EntityBase::languageManager protected function Gets the language manager.
EntityBase::linkTemplates protected function Gets an array link templates. 1
EntityBase::load public static function Loads an entity. Overrides EntityInterface::load
EntityBase::loadMultiple public static function Loads one or more entities. Overrides EntityInterface::loadMultiple
EntityBase::postCreate public function Acts on a created entity before hooks are invoked. Overrides EntityInterface::postCreate 4
EntityBase::postDelete public static function Acts on deleted entities before the delete hook is invoked. Overrides EntityInterface::postDelete 16
EntityBase::postLoad public static function Acts on loaded entities. Overrides EntityInterface::postLoad 2
EntityBase::postSave public function Acts on a saved entity before the insert or update hook is invoked. Overrides EntityInterface::postSave 14
EntityBase::preCreate public static function Changes the values of an entity before it is created. Overrides EntityInterface::preCreate 5
EntityBase::referencedEntities public function Gets a list of entities referenced by this entity. Overrides EntityInterface::referencedEntities 1
EntityBase::toLink public function Generates the HTML for a link to this entity. Overrides EntityInterface::toLink
EntityBase::uriRelationships public function Gets a list of URI relationships supported by this entity. Overrides EntityInterface::uriRelationships
EntityBase::urlRouteParameters protected function Gets an array of placeholders for this entity. 2
EntityBase::uuid public function Gets the entity UUID (Universally Unique Identifier). Overrides EntityInterface::uuid 1
EntityBase::uuidGenerator protected function Gets the UUID generator.
PluginDependencyTrait::calculatePluginDependencies protected function Calculates and adds dependencies of a specific plugin instance. 1
PluginDependencyTrait::getPluginDependencies protected function Calculates and returns dependencies of a specific plugin instance.
PluginDependencyTrait::moduleHandler protected function Wraps the module handler. 1
PluginDependencyTrait::themeHandler protected function Wraps the theme handler. 1
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
SynchronizableEntityTrait::$isSyncing protected property Whether this entity is being created, updated or deleted through a synchronization process.
SynchronizableEntityTrait::isSyncing public function
SynchronizableEntityTrait::setSyncing public function