View source
<?php
namespace Drupal\brightcove\Form;
use Brightcove\API\Client;
use Brightcove\API\CMS;
use Brightcove\API\Exception\APIException;
use Brightcove\API\Exception\AuthenticationException;
use Brightcove\API\PM;
use Drupal\brightcove\BrightcoveUtil;
use Drupal\brightcove\Entity\BrightcoveAPIClient;
use Drupal\brightcove\Entity\BrightcovePlayer;
use Drupal\brightcove\Entity\BrightcoveSubscription;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Queue\QueueInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BrightcoveAPIClientForm extends EntityForm {
protected $config;
protected $playerStorage;
protected $playerQueue;
protected $customFieldQueue;
protected $subscriptionsQueue;
protected $queryFactory;
protected $keyValueExpirableStore;
public function __construct(Config $config, EntityStorageInterface $player_storage, QueueInterface $player_queue, QueueInterface $custom_field_queue, QueueInterface $subscriptions_queue, QueryFactory $query_factory, KeyValueStoreExpirableInterface $key_value_expirable_store) {
$this->config = $config;
$this->playerStorage = $player_storage;
$this->playerQueue = $player_queue;
$this->customFieldQueue = $custom_field_queue;
$this->subscriptionsQueue = $subscriptions_queue;
$this->queryFactory = $query_factory;
$this->keyValueExpirableStore = $key_value_expirable_store;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory')
->getEditable('brightcove.settings'), $container
->get('entity_type.manager')
->getStorage('brightcove_player'), $container
->get('queue')
->get('brightcove_player_queue_worker'), $container
->get('queue')
->get('brightcove_custom_field_queue_worker'), $container
->get('queue')
->get('brightcove_subscriptions_queue_worker'), $container
->get('entity.query'), $container
->get('keyvalue.expirable')
->get('brightcove_access_token'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$brightcove_api_client = $this->entity;
if (!$brightcove_api_client
->isNew()) {
try {
$brightcove_api_client
->authorizeClient();
$status = $brightcove_api_client
->getClientStatus() ? $this
->t('OK') : $this
->t('Error');
} catch (\Exception $e) {
$status = $this
->t('Error');
}
$form['status'] = [
'#type' => 'item',
'#title' => t('Status'),
'#markup' => $status,
];
if ($brightcove_api_client
->getClientStatus() == 0) {
$form['status_message'] = [
'#type' => 'item',
'#title' => t('Error message'),
'#markup' => $brightcove_api_client
->getClientStatusMessage(),
];
}
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $brightcove_api_client
->label(),
'#description' => $this
->t('A label to identify the API Client (authentication credentials).'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $brightcove_api_client
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\brightcove\\Entity\\BrightcoveAPIClient::load',
],
'#disabled' => !$brightcove_api_client
->isNew(),
];
$form['client_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Brightcove API Client ID'),
'#description' => $this
->t('The Client ID of the Brightcove API Authentication credentials, available <a href=":link" target="_blank">here</a>.', [
':link' => 'https://studio.brightcove.com/products/videocloud/admin/oauthsettings',
]),
'#maxlength' => 255,
'#default_value' => $brightcove_api_client
->getClientId(),
'#required' => TRUE,
];
$form['secret_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Brightcove API Secret Key'),
'#description' => $this
->t('The Secret Key associated with the Client ID above, only visible once when Registering New Application.'),
'#maxlength' => 255,
'#default_value' => $brightcove_api_client
->getSecretKey(),
'#required' => TRUE,
];
$form['account_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Brightcove Account ID'),
'#description' => $this
->t('The number of one of the Brightcove accounts "selected for authorization" with the API Client above.'),
'#maxlength' => 255,
'#default_value' => $brightcove_api_client
->getAccountId(),
'#required' => TRUE,
];
$form['default_player'] = [
'#type' => 'select',
'#title' => $this
->t('Default player'),
'#options' => BrightcovePlayer::getList($brightcove_api_client
->id()),
'#default_value' => $brightcove_api_client
->getDefaultPlayer() ? $brightcove_api_client
->getDefaultPlayer() : BrightcoveAPIClient::DEFAULT_PLAYER,
'#required' => TRUE,
];
if ($brightcove_api_client
->isNew()) {
$form['default_player']['#description'] = t('The rest of the players will be available after saving.');
}
$api_clients_number = $this->queryFactory
->get('brightcove_api_client')
->count()
->execute();
$form['default_client'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Default API Client'),
'#description' => $this
->t('Enable to make this the default API Client.'),
'#default_value' => $api_clients_number == 0 || $this->config
->get('defaultAPIClient') == $brightcove_api_client
->id(),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
try {
$client = Client::authorize($form_state
->getValue('client_id'), $form_state
->getValue('secret_key'));
$cms = new CMS($client, $form_state
->getValue('account_id'));
$cms
->countVideos();
$this->keyValueExpirableStore
->setWithExpire($form_state
->getValue('id'), $client
->getAccessToken(), intval($client
->getExpiresIn()) - 30);
} catch (AuthenticationException $e) {
$form_state
->setErrorByName('client_id', $e
->getMessage());
$form_state
->setErrorByName('secret_key');
} catch (APIException $e) {
$form_state
->setErrorByName('account_id', $e
->getMessage());
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $this->entity;
$client = new Client($this->keyValueExpirableStore
->get($form_state
->getValue('id')));
$cms = new CMS($client, $form_state
->getValue('account_id'));
$video_fields = $cms
->getVideoFields();
$entity
->setMaxCustomFields($video_fields
->getMaxCustomFields());
foreach ($video_fields
->getCustomFields() as $custom_field) {
$this->customFieldQueue
->createItem([
'api_client_id' => $this->entity
->id(),
'custom_field' => $custom_field,
]);
}
parent::submitForm($form, $form_state);
if ($entity
->isNew()) {
$pm = new PM($client, $form_state
->getValue('account_id'));
$player_list = $pm
->listPlayers();
$players = [];
if (!empty($player_list) && !empty($player_list
->getItems())) {
$players = $player_list
->getItems();
}
foreach ($players as $player) {
$this->playerQueue
->createItem([
'api_client_id' => $entity
->id(),
'player' => $player,
]);
}
$video_fields = $cms
->getVideoFields();
foreach ($video_fields
->getCustomFields() as $custom_field) {
$this->customFieldQueue
->createItem([
'api_client_id' => $entity
->id(),
'custom_field' => $custom_field,
]);
}
$subscription = new BrightcoveSubscription(TRUE);
$subscription
->setStatus(FALSE)
->setApiClient($entity)
->setEndpoint(BrightcoveUtil::getDefaultSubscriptionUrl())
->setEvents([
'video-change',
])
->save();
$this->subscriptionsQueue
->createItem($entity
->id());
}
}
public function save(array $form, FormStateInterface $form_state) {
$brightcove_api_client = $this->entity;
$status = $brightcove_api_client
->save();
switch ($status) {
case SAVED_NEW:
drupal_set_message($this
->t('Created the %label Brightcove API Client.', [
'%label' => $brightcove_api_client
->label(),
]));
batch_set([
'operations' => [
[
[
BrightcoveUtil::class,
'runQueue',
],
[
'brightcove_player_queue_worker',
],
],
[
[
BrightcoveUtil::class,
'runQueue',
],
[
'brightcove_custom_field_queue_worker',
],
],
[
[
BrightcoveUtil::class,
'runQueue',
],
[
'brightcove_subscriptions_queue_worker',
],
],
[
[
BrightcoveUtil::class,
'runQueue',
],
[
'brightcove_subscription_queue_worker',
],
],
],
]);
break;
default:
drupal_set_message($this
->t('Saved the %label Brightcove API Client.', [
'%label' => $brightcove_api_client
->label(),
]));
}
if ($form_state
->getValue('default_client')) {
$this->config
->set('defaultAPIClient', $brightcove_api_client
->id())
->save();
}
$form_state
->setRedirectUrl($brightcove_api_client
->urlInfo('collection'));
}
}