You are here

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

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

Create or update an existing player from a Brightcove Player object.

Parameters

\Brightcove\Item\Player\Player $player: Brightcove Player object.

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

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.

1 call to BrightcovePlayer::createOrUpdate()
BrightcovePlayerQueueWorker::processItem in src/Plugin/QueueWorker/BrightcovePlayerQueueWorker.php
Works on a single queue item.

File

src/Entity/BrightcovePlayer.php, line 248

Class

BrightcovePlayer
Defines the Brightcove Player.

Namespace

Drupal\brightcove\Entity

Code

public static function createOrUpdate(Player $player, EntityStorageInterface $storage, $api_client_id = NULL) {

  // Try to get an existing player.
  $existing_player = $storage
    ->getQuery()
    ->condition('player_id', $player
    ->getId())
    ->execute();
  $needs_save = FALSE;
  $branches = $player
    ->getBranches();
  $master = $branches
    ->getMaster();
  $configuration = $master
    ->getConfiguration();
  $studio_configuration = $configuration
    ->getStudioConfiguration();

  // Update existing player.
  if (!empty($existing_player)) {

    // Load Brightcove Player.

    /** @var BrightcovePlayer $player_entity */
    $player_entity = self::load(reset($existing_player));

    // Update player if it is changed on Brightcove.
    if ($player_entity
      ->getChangedTime() < strtotime($master
      ->getUpdatedAt())) {
      $needs_save = TRUE;

      // Save or update playlist if needed.
      $is_playlist = $configuration
        ->isPlaylist();
      if ($player_entity
        ->isPlaylist() != $is_playlist) {
        $player_entity
          ->setPlaylist($is_playlist);
      }

      // Save or update version if needed.
      $version = $configuration
        ->getPlayer()
        ->getTemplate()
        ->getVersion();
      if ($player_entity
        ->getVersion() != $version) {
        $player_entity
          ->setVersion($version);
      }

      // Set player studio configs if they are set.
      if (!empty($studio_configuration)) {
        $player_config = $studio_configuration
          ->getPlayer();

        // Save or update adjusted if needed.
        $adjusted = $player_config
          ->isAdjusted();
        if ($player_entity
          ->isAdjusted() != $adjusted) {
          $player_entity
            ->setAdjusted($adjusted);
        }

        // Save or update height if needed.
        $height = $player_config
          ->getHeight();
        if ($player_entity
          ->getHeight() != $height) {
          $player_entity
            ->setHeight($height);
        }

        // Save or update width if needed.
        $width = $player_config
          ->getWidth();
        if ($player_entity
          ->getWidth() != $width) {
          $player_entity
            ->setWidth($width);
        }

        // Save or update units if needed.
        $units = $player_config
          ->getUnits();
        if ($player_entity
          ->getUnits() != $units) {
          $player_entity
            ->setUnits($units);
        }

        // Save or update responsive if needed.
        $responsive = $player_config
          ->isResponsive();
        if ($player_entity
          ->isResponsive() != $responsive) {
          $player_entity
            ->setResponsive($responsive);
        }
      }
      else {

        // Remove studio configs if there is none.
        $player_entity
          ->setAdjusted(NULL);
        $player_entity
          ->setHeight(NULL);
        $player_entity
          ->setWidth(NULL);
        $player_entity
          ->setUnits(NULL);
        $player_entity
          ->setResponsive(NULL);
      }
    }
  }
  else {

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

    // Create new Brightcove player entity.
    $values = [
      'player_id' => $player
        ->getId(),
      'api_client' => [
        'target_id' => $api_client_id,
      ],
      'created' => strtotime($player
        ->getCreatedAt()),
      'playlist' => $configuration
        ->isPlaylist(),
      'version' => $configuration
        ->getPlayer()
        ->getTemplate()
        ->getVersion(),
    ];

    // Set player settings.
    if (!empty($studio_configuration)) {
      $player_config = $studio_configuration
        ->getPlayer();
      if ($player_config) {
        $values['adjusted'] = $player_config
          ->isAdjusted();
        $values['height'] = $player_config
          ->getHeight();
        $values['width'] = $player_config
          ->getWidth();
        $values['units'] = $player_config
          ->getUnits();
        $values['responsive'] = $player_config
          ->isResponsive();
      }
    }
    $player_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.
    $player_entity
      ->setChangedTime(strtotime($master
      ->getUpdatedAt()));

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