View source
<?php
namespace Drupal\brightcove\Plugin\QueueWorker;
use Drupal\brightcove\BrightcoveUtil;
use Drupal\brightcove\Entity\BrightcoveCustomField;
use Drupal\brightcove\Entity\BrightcovePlayer;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BrightcoveClientQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $videoPageQueue;
protected $playlistPageQueue;
protected $playerQueue;
protected $playerDeleteQueue;
protected $customFieldQueue;
protected $customFieldDeleteQueue;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, QueueInterface $video_page_queue, QueueInterface $playlist_page_queue, QueueInterface $player_queue, QueueInterface $player_delete_queue, QueueInterface $custom_field_queue, QueueInterface $custom_field_delete_queue) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->videoPageQueue = $video_page_queue;
$this->playlistPageQueue = $playlist_page_queue;
$this->playerQueue = $player_queue;
$this->playerDeleteQueue = $player_delete_queue;
$this->customFieldQueue = $custom_field_queue;
$this->customFieldDeleteQueue = $custom_field_delete_queue;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('queue')
->get('brightcove_video_page_queue_worker'), $container
->get('queue')
->get('brightcove_playlist_page_queue_worker'), $container
->get('queue')
->get('brightcove_player_queue_worker'), $container
->get('queue')
->get('brightcove_player_delete_queue_worker'), $container
->get('queue')
->get('brightcove_custom_field_queue_worker'), $container
->get('queue')
->get('brightcove_custom_field_delete_queue_worker'));
}
public function processItem($data) {
$cms = BrightcoveUtil::getCmsApi($data);
$items_per_page = 100;
$pm = BrightcoveUtil::getPmApi($data);
$player_list = $pm
->listPlayers();
$players = [];
if (!empty($player_list)) {
$players = $player_list
->getItems() ?: [];
}
$player_entities = BrightcovePlayer::getList($data);
foreach ($players as $player) {
unset($player_entities[$player
->getId()]);
$this->playerQueue
->createItem([
'api_client_id' => $data,
'player' => $player,
]);
}
foreach (array_keys($player_entities) as $player_id) {
$this->playerDeleteQueue
->createItem([
'player_id' => $player_id,
]);
}
$video_fields = $cms
->getVideoFields();
$custom_fields = [];
foreach ($video_fields
->getCustomFields() as $custom_field) {
$custom_fields[] = $custom_field
->getId();
$this->customFieldQueue
->createItem([
'api_client_id' => $data,
'custom_field' => $custom_field,
]);
}
$custom_field_entities = BrightcoveCustomField::loadMultipleByApiClient($data);
foreach ($custom_field_entities as $custom_field_entity) {
if (!in_array($custom_field_entity
->getCustomFieldId(), $custom_fields)) {
$this->customFieldDeleteQueue
->createItem($custom_field_entity);
}
}
$video_count = $cms
->countVideos();
$page = 0;
while ($page * $items_per_page < $video_count) {
$this->videoPageQueue
->createItem([
'api_client_id' => $data,
'page' => $page,
'items_per_page' => $items_per_page,
]);
$page++;
}
$playlist_count = $cms
->countPlaylists();
$page = 0;
while ($page * $items_per_page < $playlist_count) {
$this->playlistPageQueue
->createItem([
'api_client_id' => $data,
'page' => $page,
'items_per_page' => $items_per_page,
]);
$page++;
}
}
}