You are here

public static function BrightcovePlaylist::createOrUpdate in Brightcove Video Connect 3.x

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

Create or update an existing playlist from a Brightcove Playlist object.

Parameters

\Brightcove\Item\Playlist $playlist: Brightcove Playlist object.

\Drupal\Core\Entity\EntityStorageInterface $playlist_storage: Playlist EntityStorage.

\Drupal\Core\Entity\EntityStorageInterface $video_storage: Video EntityStorage.

int|null $api_client_id: The ID of the BrightcoveAPIClient entity.

Throws

\Exception If BrightcoveAPIClient ID is missing when a new entity is being created.

2 calls to BrightcovePlaylist::createOrUpdate()
BrightcovePlaylistController::update in src/Controller/BrightcovePlaylistController.php
Menu callback to update the existing Playlist with the latest version.
BrightcovePlaylistQueueWorker::processItem in src/Plugin/QueueWorker/BrightcovePlaylistQueueWorker.php
Works on a single queue item.

File

src/Entity/BrightcovePlaylist.php, line 721

Class

BrightcovePlaylist
Defines the Brightcove Playlist.

Namespace

Drupal\brightcove\Entity

Code

public static function createOrUpdate(Playlist $playlist, EntityStorageInterface $playlist_storage, EntityStorageInterface $video_storage, $api_client_id = NULL) {

  // May throw an \Exception if any of the videos is not found.
  $videos = self::extractVideosArray($playlist, $video_storage);

  // Only save the brightcove_playlist entity if it's really updated or
  // created now.
  $needs_save = FALSE;

  // Try to get an existing playlist.
  $existing_playlist = $playlist_storage
    ->getQuery()
    ->condition('playlist_id', $playlist
    ->getId())
    ->execute();

  // Update existing playlist if needed.
  if (!empty($existing_playlist)) {

    // Load Brightcove Playlist.
    $playlist_entity_id = reset($existing_playlist);

    /** @var BrightcovePlaylist $playlist_entity */
    $playlist_entity = BrightcovePlaylist::load($playlist_entity_id);

    // Update playlist if it is changed on Brightcove.
    if ($playlist_entity
      ->getChangedTime() < ($updated_at = strtotime($playlist
      ->getUpdatedAt()))) {
      $needs_save = TRUE;

      // Update changed time.
      $playlist_entity
        ->setChangedTime($updated_at);
    }
  }
  else {
    $needs_save = TRUE;

    // Create new Brightcove Playlist entity.

    /** @var BrightcovePlaylist $playlist_entity */
    $playlist_entity = BrightcovePlaylist::create([
      'api_client' => [
        'target_id' => $api_client_id,
      ],
      'playlist_id' => $playlist
        ->getId(),
      'created' => strtotime($playlist
        ->getCreatedAt()),
      'changed' => strtotime($playlist
        ->getUpdatedAt()),
    ]);
  }
  if ($needs_save) {

    // Update type field if needed.
    if ($playlist_entity
      ->getType() != ($type = $playlist
      ->getType())) {
      $playlist_entity
        ->setType($type);
    }

    // Update name field if needed.
    if ($playlist_entity
      ->getName() != ($name = $playlist
      ->getName())) {
      $playlist_entity
        ->setName($name);
    }

    // Update favorite field if needed.
    if ($playlist_entity
      ->isFavorite() != ($favorite = $playlist
      ->isFavorite())) {

      // This is a non-modifiable field so it does not have a specific
      // setter.
      $playlist_entity
        ->set('favorite', $favorite);
    }

    // Update reference ID field if needed.
    if ($playlist_entity
      ->getReferenceId() != ($reference_id = $playlist
      ->getReferenceId())) {
      $playlist_entity
        ->setReferenceId($reference_id);
    }

    // Update description field if needed.
    if ($playlist_entity
      ->getDescription() != ($description = $playlist
      ->getDescription())) {
      $playlist_entity
        ->setDescription($description);
    }

    // Update tags field if needed.
    $playlist_search = $playlist
      ->getSearch();
    preg_match('/^(\\+?)([^\\+].*):(?:,?"(.*?[^"])")$/i', $playlist_search, $matches);
    if (count($matches) == 4 && $matches[2] == 'tags') {
      $playlist_tags = explode('","', $matches[3]);

      // Save or update tag search condition if needed.
      $playlist_search_condition = $matches[1] == '+' ? self::TAG_SEARCH_CONTAINS_ALL : self::TAG_SEARCH_CONTAINS_ONE_OR_MORE;
      if ($playlist_entity
        ->getTagsSearchCondition() != $playlist_search_condition) {
        $playlist_entity
          ->setTagsSearchCondition($playlist_search_condition);
      }
      BrightcoveUtil::saveOrUpdateTags($playlist_entity, $api_client_id, $playlist_tags);
    }

    // Update videos field if needed.
    if ($playlist_entity
      ->getVideos() != $videos) {
      $playlist_entity
        ->setVideos($videos);
    }

    // @TODO: State/published.
    $playlist_entity
      ->save();
  }
}