You are here

public static function BrightcoveVideo::createOrUpdate in Brightcove Video Connect 8

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

Create or update an existing video from a Brightcove Video object.

Parameters

\Brightcove\Object\Video\Video $video: Brightcove Video object.

\Drupal\Core\Entity\EntityStorageInterface $storage: EntityStorage.

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

Return value

\Drupal\brightcove\Entity\BrightcoveVideo|null The saved BrightcoveVideo entity.

Throws

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

2 calls to BrightcoveVideo::createOrUpdate()
BrightcoveSubscriptionController::notificationCallback in src/Controller/BrightcoveSubscriptionController.php
Menu callback to handle the Brightcove notification callback.
BrightcoveVideoQueueWorker::processItem in src/Plugin/QueueWorker/BrightcoveVideoQueueWorker.php
Works on a single queue item.

File

src/Entity/BrightcoveVideo.php, line 1760

Class

BrightcoveVideo
Defines the Brightcove Video entity.

Namespace

Drupal\brightcove\Entity

Code

public static function createOrUpdate(Video $video, EntityStorageInterface $storage, $api_client_id = NULL) {

  // Try to get an existing video.
  $existing_video = $storage
    ->getQuery()
    ->condition('video_id', $video
    ->getId())
    ->execute();
  $needs_save = FALSE;

  // Update existing video.
  if (!empty($existing_video)) {

    // Load Brightcove Video.

    /** @var BrightcoveVideo $video_entity */
    $video_entity = self::load(reset($existing_video));

    // Update video if it is changed on Brightcove.
    if ($video_entity
      ->getChangedTime() < strtotime($video
      ->getUpdatedAt())) {
      $needs_save = TRUE;
    }
  }
  else {

    // Make sure we got an api client id when a new video is being created.
    if (is_null($api_client_id)) {
      throw new \Exception(t('To create a new BrightcoveVideo entity, the api_client_id must be given.'));
    }

    // Create new Brightcove video entity.
    $values = [
      'video_id' => $video
        ->getId(),
      'api_client' => [
        'target_id' => $api_client_id,
      ],
      'created' => strtotime($video
        ->getCreatedAt()),
    ];
    $video_entity = self::create($values);
    $needs_save = TRUE;
  }

  // Save entity only if it is being created or updated.
  if ($needs_save) {

    // Save or update changed time.
    $video_entity
      ->setChangedTime(strtotime($video
      ->getUpdatedAt()));

    // Save or update Description field if needed.
    if ($video_entity
      ->getDescription() != ($description = $video
      ->getDescription())) {
      $video_entity
        ->setDescription($description);
    }

    // Save or update duration field if needed.
    if ($video_entity
      ->getDuration() != ($duration = $video
      ->getDuration())) {
      $video_entity
        ->setDuration($duration);
    }

    // Save or update economics field if needed.
    if ($video_entity
      ->getEconomics() != ($economics = $video
      ->getEconomics())) {
      $video_entity
        ->setEconomics($economics);
    }

    // Save or update tags field if needed.
    BrightcoveUtil::saveOrUpdateTags($video_entity, $api_client_id, $video
      ->getTags());

    // Get images.
    $images = $video
      ->getImages();

    // Save or update thumbnail image if needed.
    if (!empty($images[self::IMAGE_TYPE_THUMBNAIL]) && !empty($images[self::IMAGE_TYPE_THUMBNAIL]
      ->getSrc())) {
      $video_entity
        ->saveImage(self::IMAGE_TYPE_THUMBNAIL, $images[self::IMAGE_TYPE_THUMBNAIL]);
    }
    else {

      // Delete file.
      $video_entity
        ->setThumbnail(NULL);
    }

    // Save or update poster image if needed.
    if (!empty($images[self::IMAGE_TYPE_POSTER]) && !empty($images[self::IMAGE_TYPE_POSTER]
      ->getSrc())) {
      $video_entity
        ->saveImage(self::IMAGE_TYPE_POSTER, $images[self::IMAGE_TYPE_POSTER]);
    }
    else {

      // Delete file.
      $video_entity
        ->setPoster(NULL);
    }

    // Save or update link url field if needed.
    $link = $video
      ->getLink();
    $related_link_field = $video_entity
      ->getRelatedLink() ?: NULL;
    $related_link = [];
    if (empty($related_link_field) && !empty($link)) {
      $related_link['uri'] = $link
        ->getUrl();
      $related_link['title'] = $link
        ->getText();
    }
    elseif (!empty($related_link_field) && !empty($link)) {
      if ($related_link_field['uri'] != ($url = $link
        ->getUrl())) {
        $related_link['uri'] = $url;
      }
      if ($related_link_field['title'] != ($title = $link
        ->getText())) {
        $related_link['title'] = $title;
      }
    }
    else {
      $video_entity
        ->setRelatedLink(NULL);
    }
    if (!empty($related_link)) {
      $video_entity
        ->setRelatedLink($related_link);
    }

    // Save or update long description if needed.
    if ($video_entity
      ->getLongDescription() != ($long_description = $video
      ->getLongDescription())) {
      $video_entity
        ->setLongDescription($long_description);
    }

    // Save or update Name field if needed.
    if ($video_entity
      ->getName() != ($name = $video
      ->getName())) {
      $video_entity
        ->setName($name);
    }

    // Save or update reference ID field if needed.
    if ($video_entity
      ->getReferenceId() != ($reference_id = $video
      ->getReferenceId())) {
      $video_entity
        ->setReferenceId($reference_id);
    }

    // Save or update custom field values.
    if ($video_entity
      ->getCustomFieldValues() != ($custom_fields = $video
      ->getCustomFields())) {
      $video_entity
        ->setCustomFieldValues($custom_fields);
    }

    // Save or update schedule dates if needed.
    $schedule = $video
      ->getSchedule();
    if (!is_null($schedule)) {
      if ($video_entity
        ->getScheduleStartsAt() != ($starts_at = $schedule
        ->getStartsAt())) {
        $video_entity
          ->setScheduleStartsAt(BrightcoveUtil::convertDate($starts_at));
      }
      if ($video_entity
        ->getScheduleEndsAt() != ($ends_at = $schedule
        ->getEndsAt())) {
        $video_entity
          ->setScheduleEndsAt(BrightcoveUtil::convertDate($ends_at));
      }
    }
    else {
      $video_entity
        ->setScheduleStartsAt(NULL);
      $video_entity
        ->setScheduleEndsAt(NULL);
    }

    // Save or update state.
    // We are settings the video as published only if the state is ACTIVE,
    // otherwise it is set as unpublished.
    $state = $video
      ->getState() == self::STATE_ACTIVE ? TRUE : FALSE;
    if ($video_entity
      ->isPublished() != $state) {
      $video_entity
        ->setPublished($state);
    }

    // Save video entity.
    $video_entity
      ->save();
  }
  return $video_entity;
}