BrightcovePlayer.php in Brightcove Video Connect 8
File
src/Entity/BrightcovePlayer.php
View source
<?php
namespace Drupal\brightcove\Entity;
use Brightcove\Object\Player\Player;
use Drupal\brightcove\BrightcovePlayerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
class BrightcovePlayer extends BrightcoveCmsEntity implements BrightcovePlayerInterface {
public function getPlayerId() {
return $this
->get('player_id')->value;
}
public function setPlayerId($player_id) {
$this
->set('player_id', $player_id);
return $this;
}
public function isAdjusted() {
return $this
->get('adjusted')->value;
}
public function setAdjusted($adjusted) {
return $this
->set('adjusted', $adjusted);
}
public function getHeight() {
return $this
->get('height')->value;
}
public function setHeight($height) {
return $this
->set('height', $height);
}
public function getWidth() {
return $this
->get('width')->value;
}
public function setWidth($width) {
return $this
->set('width', $width);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['bcpid'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The Drupal entity ID of the Brightcove Player.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The Brightcove Player UUID.'))
->setReadOnly(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Player name'))
->setDescription(t('The name of the Brightcove Player.'));
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code for the Brightcove Player.'));
$fields['api_client'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('API Client'))
->setDescription(t('API Client to use for the Player.'))
->setRequired(TRUE)
->setSetting('target_type', 'brightcove_api_client');
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The username of the Brightcove Playlist author.'))
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\\brightcove\\Entity\\BrightcovePlayer::getCurrentUserId')
->setTranslatable(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the Brightcove Player was created.'))
->setTranslatable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the Brightcove Player was last edited.'))
->setTranslatable(TRUE);
$fields['player_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Player ID'))
->setDescription(t('Unique Player ID assigned by Brightcove.'))
->setReadOnly(TRUE);
$fields['adjusted'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Adjusted'))
->setDescription(t('Indicates if player dimensions should be adjusted for playlist.'));
$fields['height'] = BaseFieldDefinition::create('float')
->setLabel(t('Adjusted'))
->setDescription(t('The height of the player.'));
$fields['width'] = BaseFieldDefinition::create('float')
->setLabel(t('Adjusted'))
->setDescription(t('The width of the player.'));
return $fields;
}
public static function createOrUpdate(Player $player, EntityStorageInterface $storage, $api_client_id = NULL) {
$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();
if (!empty($existing_player)) {
$player_entity = self::load(reset($existing_player));
if ($player_entity
->getChangedTime() < strtotime($master
->getUpdatedAt())) {
$needs_save = TRUE;
}
if (!empty($studio_configuration)) {
$player_config = $studio_configuration
->getPlayer();
if ($player_entity
->isAdjusted() != ($adjusted = $player_config
->isAdjusted())) {
$player_entity
->setAdjusted($adjusted);
}
if ($player_entity
->getHeight() != ($height = $player_config
->getHeight())) {
$player_entity
->setHeight($height);
}
if ($player_entity
->getWidth() != ($width = $player_config
->getWidth())) {
$player_entity
->setWidth($width);
}
}
else {
$player_entity
->setAdjusted(NULL);
$player_entity
->setHeight(NULL);
$player_entity
->setWidth(NULL);
}
}
else {
if (is_null($api_client_id)) {
throw new \Exception(t('To create a new BrightcovePlayer entity, the api_client_id must be given.'));
}
$values = [
'player_id' => $player
->getId(),
'api_client' => [
'target_id' => $api_client_id,
],
'created' => strtotime($player
->getCreatedAt()),
];
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;
}
if ($needs_save) {
$player_entity
->setChangedTime(strtotime($master
->getUpdatedAt()));
if ($player_entity
->getName() != ($name = $player
->getName())) {
$player_entity
->setName($name);
}
$player_entity
->save();
}
}
public static function loadByPlayerId($player_id) {
$eq = \Drupal::entityQuery('brightcove_player');
$player = $eq
->condition('player_id', $player_id)
->execute();
return self::load(reset($player));
}
public static function getList($api_client, $use_entity_id = FALSE) {
if ($use_entity_id) {
$players = [];
}
else {
$players = [
BrightcoveAPIClient::DEFAULT_PLAYER => t('Brightcove Default Player'),
];
}
if (!$api_client) {
return $players;
}
$eq = \Drupal::entityQuery('brightcove_player');
$eq
->condition('api_client', $api_client);
$player_ids = $eq
->execute();
foreach (self::loadMultiple($player_ids) as $player) {
$players[$use_entity_id ? $player
->id() : $player
->getPlayerId()] = $player
->getName();
}
return $players;
}
}