You are here

class BrightcovePlaylistEntityController in Brightcove Video Connect 7.7

Same name and namespace in other branches
  1. 7.6 brightcove.playlist.inc \BrightcovePlaylistEntityController

Entity controller class for Brightcove client.

Hierarchy

Expanded class hierarchy of BrightcovePlaylistEntityController

1 string reference to 'BrightcovePlaylistEntityController'
brightcove_entity_info in ./brightcove.module
Implements hook_entity_info().

File

./brightcove.playlist.inc, line 10
Brightcove playlist related functions.

View source
class BrightcovePlaylistEntityController extends EntityAPIController {

  /**
   * Overwrites EntityAPIController::create().
   */
  public function create(array $values = []) {

    // Add is_new property if it is not set.
    $values += [
      'is_new' => TRUE,
    ];
    brightcove_load_lib();
    $playlist_entity = new Entity($values, $this->entityType);
    if (empty($playlist_entity->client) || !$playlist_entity->client instanceof Entity) {
      $playlist_entity->client = entity_create('brightcove_client', []);
    }
    if (empty($playlist_entity->playlist) || !$playlist_entity->playlist instanceof \Brightcove\Object\Playlist) {
      $playlist_entity->playlist = new \Brightcove\Object\Playlist();
    }
    return $playlist_entity;
  }

  /**
   * Overwrites EntityAPIController::save().
   */
  public function save($playlist_entity, DatabaseTransaction $transaction = NULL) {
    if (!isset($playlist_entity->client)) {
      watchdog('brightcove', 'Saving playlist failed due to missing client property.', [], WATCHDOG_ERROR);
      drupal_set_message(t('Saving playlist failed due to missing client property.'), 'error');
      return;
    }
    $bcid = $playlist_entity->client->bcid;
    brightcove_try(function () use ($playlist_entity, $bcid) {

      /** @var \Brightcove\API\CMS $cms */
      list($cms, ) = brightcove_create_classes($playlist_entity->client);
      if (!empty($playlist_entity->bpid)) {
        field_attach_update('brightcove_playlist', $playlist_entity);
        $cms
          ->updatePlaylist($playlist_entity->playlist);
        brightcove_invalidate_cache("brightcove:playlist:{$playlist_entity->playlist->getId()}:{$bcid}");
      }
      else {
        field_attach_insert('brightcove_playlist', $playlist_entity);
        $cms
          ->createPlaylist($playlist_entity->playlist);
      }
      $name = $playlist_entity->playlist
        ->getName();
      watchdog('brightcove', 'Saving brightcove playlist "@name" was successful.', [
        '@name' => $name,
      ], WATCHDOG_INFO);
      drupal_set_message(t('Saving brightcove playlist "@name" was successful.', [
        '@name' => $name,
      ]), 'status');
    }, function (\Brightcove\API\Exception\APIException $ex) {
      throw $ex;
    });
    brightcove_invalidate_cache('brightcove:playlist:list', TRUE);
  }

  /**
   * Overwrites DrupalDefaultEntityController::load().
   *
   * @param array $ids
   *  Array of ids. The format for an id is "1234-5" where 1234 is
   *  a Playlist->getId() and 5 is a Client->bcid.
   * @param $conditions
   *
   * @return \Brightcove\Object\Playlist[] An array of Playlist objects, keyed by the playlist id.
   */
  public function load($ids = [], $conditions = []) {
    $playlists = [];
    $entity_info = entity_get_info('brightcove_playlist');
    foreach ($ids as $id) {
      $playlist_ids = explode('-', $id);
      if (count($playlist_ids) != 2) {
        continue;
      }
      $playlist_id = $playlist_ids[0];
      $bcid = $playlist_ids[1];
      $playlist = brightcove_load_playlist($playlist_id, $bcid);
      if ($playlist) {
        $values = [
          $entity_info['entity keys']['id'] => $id,
          'playlist_id' => $playlist_id,
          $entity_info['entity keys']['label'] => $playlist
            ->getName(),
          $entity_info['entity keys']['bundle'] => $playlist
            ->getType() == 'EXPLICIT' ? BRIGHTCOVE_PLAYLIST_TYPE_MANUAL : BRIGHTCOVE_PLAYLIST_TYPE_SMART,
          'playlist' => $playlist,
          'client' => brightcove_client_load($bcid),
        ];
        $playlist_entity = entity_create('brightcove_playlist', $values);
        $playlists[$id] = $playlist_entity;
      }
    }
    field_attach_load('brightcove_playlist', $playlists);
    return $playlists;
  }

  /**
   * Overwrites DrupalDefaultEntityController::delete().
   */
  public function delete($ids, DatabaseTransaction $transaction = NULL) {
    $entities = $ids ? $this
      ->load($ids) : FALSE;
    if (!$entities) {

      // Do nothing in case invalid or no ids have been passed.
      return;
    }
    foreach ($entities as $playlist_entity) {

      /** @var \Brightcove\Object\Playlist $playlist */
      $playlist = $playlist_entity->playlist;
      brightcove_try(function () use ($playlist, $playlist_entity) {

        /** @var \Brightcove\API\CMS $cms */
        list($cms, ) = brightcove_create_classes($playlist_entity->client);
        $cms
          ->deletePlaylist($playlist
          ->getId());

        // deletePlaylist() fails silently in case the client has no permission to
        // delete the playlist so we check here if it has been really removed or
        // not.
        try {
          $cms
            ->getPlaylist($playlist
            ->getId());
          watchdog('brightcove', 'Deleting the playlist @playlist has failed. Maybe the brightcove client has not enough permissions.', [
            '@playlist' => $playlist
              ->getId(),
          ], WATCHDOG_ERROR);
          drupal_set_message(t('Deleting the playlist @playlist has failed. Maybe the brightcove client has not enough permissions.', [
            '@playlist' => $playlist
              ->getId(),
          ]), 'error');
        } catch (Exception $e) {

          // Invalidate playlist cache.
          brightcove_invalidate_cache('brightcove:playlist', TRUE);
        }
      });
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BrightcovePlaylistEntityController::create public function Overwrites EntityAPIController::create(). Overrides EntityAPIController::create
BrightcovePlaylistEntityController::delete public function Overwrites DrupalDefaultEntityController::delete(). Overrides EntityAPIController::delete
BrightcovePlaylistEntityController::load public function Overwrites DrupalDefaultEntityController::load(). Overrides EntityAPIController::load
BrightcovePlaylistEntityController::save public function Overwrites EntityAPIController::save(). Overrides EntityAPIController::save
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildContent public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::buildContent
EntityAPIController::buildQuery protected function Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController::buildQuery 1
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::export public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::export 1
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::invoke public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::invoke 1
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController::resetCache 1
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIController::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::view 1
EntityAPIController::__construct public function Overridden. Overrides DrupalDefaultEntityController::__construct 1