You are here

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

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

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

Parameters

\Brightcove\Object\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 167

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;
    }

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

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

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

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

      // Remove configs if there are none set.
      $player_entity
        ->setAdjusted(NULL);
      $player_entity
        ->setHeight(NULL);
      $player_entity
        ->setWidth(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()),
    ];

    // Set player settings.
    if (!empty($studio_configuration)) {
      $player_config = $studio_configuration
        ->getPlayer();
      $values['adjusted'] = $player_config
        ->isAdjusted();
      $values['height'] = $player_config
        ->getHeight();
      $values['width'] = $player_config
        ->getWidth();
    }
    $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();
  }
}