You are here

class BrightcoveSubscriptionController in Brightcove Video Connect 3.x

Same name and namespace in other branches
  1. 8.2 src/Controller/BrightcoveSubscriptionController.php \Drupal\brightcove\Controller\BrightcoveSubscriptionController
  2. 8 src/Controller/BrightcoveSubscriptionController.php \Drupal\brightcove\Controller\BrightcoveSubscriptionController

Provides controller for subscription related callbacks.

Hierarchy

Expanded class hierarchy of BrightcoveSubscriptionController

File

src/Controller/BrightcoveSubscriptionController.php, line 25

Namespace

Drupal\brightcove\Controller
View source
class BrightcoveSubscriptionController extends ControllerBase {

  /**
   * Database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Link generator.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $linkGenerator;

  /**
   * BrightcoveVideo storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $videoStorage;

  /**
   * Lock backend.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'), $container
      ->get('link_generator'), $container
      ->get('entity_type.manager')
      ->getStorage('brightcove_video'), $container
      ->get('lock'));
  }

  /**
   * BrightcoveSubscriptionController constructor.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   Database connection.
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
   *   Link generator.
   * @param \Drupal\Core\Entity\EntityStorageInterface $video_storage
   *   Brightcove video storage.
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   Lock backend.
   */
  public function __construct(Connection $connection, LinkGeneratorInterface $link_generator, EntityStorageInterface $video_storage, LockBackendInterface $lock) {
    $this->connection = $connection;
    $this->linkGenerator = $link_generator;
    $this->videoStorage = $video_storage;
    $this->lock = $lock;
  }

  /**
   * Menu callback to handle the Brightcove notification callback.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   Request object.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   Redirection response.
   *
   * @throws \Exception
   */
  public function notificationCallback(Request $request) {
    $status = BrightcoveUtil::runWithSemaphore(function () use ($request) {
      $content = Json::decode($request
        ->getContent());
      switch ($content['event']) {
        case 'video-change':

          // Try to update an existing video or create a new one if not exist.
          try {

            // Get CMS API.
            $api_client = BrightcoveAPIClient::loadByAccountId($content['account_id']);
            if (!empty($api_client)) {
              $cms = BrightcoveUtil::getCmsApi($api_client
                ->id());
              $video = $cms
                ->getVideo($content['video']);
              BrightcoveVideo::createOrUpdate($video, $this->videoStorage, $api_client
                ->id());
            }
          } catch (APIException $e) {

            // In case of 404 we have nothing to do.
            if ($e
              ->getCode() != 404) {
              watchdog_exception('brightcove', $e, $e
                ->getMessage());
              return FALSE;
            }
          }
          break;
      }
      return TRUE;
    }, $this->lock);

    // Return 409 only if the status is FALSE, in any other case such as TRUE or
    // NULL return 200.
    return new Response('', $status === FALSE ? 409 : 200);
  }

  /**
   * Lists available Brightcove Subscriptions.
   *
   * @throws \Drupal\brightcove\Entity\Exception\BrightcoveSubscriptionException
   */
  public function listSubscriptions() {

    // Set headers.
    $header = [
      'endpoint' => $this
        ->t('Endpoint'),
      'api_client' => $this
        ->t('API Client'),
      'events' => $this
        ->t('Events'),
      'operations' => $this
        ->t('Operations'),
    ];

    // Get Subscriptions.
    $brightcove_subscriptions = BrightcoveSubscription::loadMultiple();

    // Whether a warning has benn shown about the missing subscriptions on
    // Brightcove or not.
    $warning_set = FALSE;

    // Assemble subscription list.
    $rows = [];
    foreach ($brightcove_subscriptions as $key => $brightcove_subscription) {
      $api_client = $brightcove_subscription
        ->getApiClient();
      $rows[$key] = [
        'endpoint' => $brightcove_subscription
          ->getEndpoint() . ($brightcove_subscription
          ->isDefault() ? " ({$this->t('default')})" : ''),
        'api_client' => !empty($api_client) ? $this->linkGenerator
          ->generate($api_client
          ->label(), Url::fromRoute('entity.brightcove_api_client.edit_form', [
          'brightcove_api_client' => $api_client
            ->id(),
        ])) : '',
        'events' => implode(', ', array_filter($brightcove_subscription
          ->getEvents(), function ($value) {
          return !empty($value);
        })),
      ];

      // Default subscriptions can be enabled or disabled only.
      if ((bool) $brightcove_subscription
        ->isDefault()) {
        $enable_link = Url::fromRoute('entity.brightcove_subscription.enable', [
          'id' => $brightcove_subscription
            ->getId(),
        ]);
        $disable_link = Url::fromRoute('entity.brightcove_subscription.disable', [
          'id' => $brightcove_subscription
            ->getId(),
        ]);
        $rows[$key]['operations'] = [
          'data' => [
            '#type' => 'operations',
            '#links' => [
              'change_status' => [
                'title' => $brightcove_subscription
                  ->isActive() ? $this
                  ->t('Disable') : $this
                  ->t('Enable'),
                'url' => $brightcove_subscription
                  ->isActive() ? $disable_link : $enable_link,
              ],
            ],
          ],
        ];
      }
      else {
        $subscriptions = BrightcoveSubscription::listFromBrightcove($api_client);
        $subscription_found = FALSE;
        foreach ($subscriptions as $subscription) {
          if ($brightcove_subscription
            ->getEndpoint() == $subscription
            ->getEndpoint()) {
            $subscription_found = TRUE;

            // If the endpoint exist but their ID is different, fix it.
            if ($brightcove_subscription
              ->getBcSid() != ($id = $subscription
              ->getId())) {
              $brightcove_subscription
                ->setBcSid($id);
              $brightcove_subscription
                ->save();
            }
            break;
          }
        }
        if (!$warning_set && !$subscription_found) {
          drupal_set_message($this
            ->t('There are subscriptions which are not available on Brightcove.<br>You can either <strong>create</strong> them on Brightcove or <strong>delete</strong> them if no longer needed.'), 'warning');
          $warning_set = TRUE;
        }

        // Add create link if the subscription is missing from Brightcove.
        $create_link = [];
        if (!$subscription_found) {
          $create_link = [
            'create' => [
              'title' => $this
                ->t('Create'),
              'url' => Url::fromRoute('entity.brightcove_subscription.create', [
                'id' => $brightcove_subscription
                  ->getId(),
              ]),
            ],
          ];
        }
        $rows[$key]['operations'] = [
          'data' => [
            '#type' => 'operations',
            '#links' => $create_link + [
              'delete' => [
                'title' => $this
                  ->t('Delete'),
                'weight' => 10,
                'url' => Url::fromRoute('entity.brightcove_subscription.delete_form', [
                  'id' => $brightcove_subscription
                    ->getId(),
                ]),
              ],
            ],
          ],
        ];
      }
    }

    // Check default subscriptions for each api client.
    $api_clients_without_default_subscription = [];

    /** @var \Drupal\brightcove\Entity\BrightcoveAPIClient $api_client */
    foreach (BrightcoveAPIClient::loadMultiple() as $api_client) {
      if (BrightcoveSubscription::loadDefault($api_client) == NULL) {
        $api_clients_without_default_subscription[] = $api_client
          ->getLabel();
      }
    }
    if (!empty($api_clients_without_default_subscription)) {
      drupal_set_message($this
        ->t('There are missing default subscription(s) for the following API Client(s): %api_clients<br><a href="@link">Create missing subscription(s)</a>.', [
        '%api_clients' => implode(', ', $api_clients_without_default_subscription),
        '@link' => Url::fromRoute('entity.brightcove_subscription.create_defaults')
          ->toString(),
      ]), 'warning');
    }
    $page['subscriptions'] = [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => $this
        ->t('There are no subscriptions yet.'),
    ];
    return $page;
  }

  /**
   * Create a subscription on Brightcove from an already existing entity.
   *
   * @param int $id
   *   BrightcoveSubscription entity ID.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Redirect response to redirect user after creating a Drupal only
   *   subscription.
   */
  public function createSubscription($id) {
    try {
      $brightcove_subscription = BrightcoveSubscription::load($id);
      $brightcove_subscription
        ->saveToBrightcove();
    } catch (BrightcoveSubscriptionException $e) {
      drupal_set_message($this
        ->t('Failed to create Subscription on Brightcove: @error', [
        '@error' => $e
          ->getMessage(),
      ]), 'error');
    }
    return $this
      ->redirect('entity.brightcove_subscription.list');
  }

  /**
   * Enables and creates the default Subscription from Brightcove.
   *
   * @param string $id
   *   The ID of the Brightcove Subscription.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Redirect response to redirect user after enabling the default
   *   subscription.
   */
  public function enable($id) {
    try {
      $subscription = BrightcoveSubscription::load($id);
      $subscription
        ->saveToBrightcove();
      drupal_set_message($this
        ->t('Default subscription for the "@api_client" API client has been successfully enabled.', [
        '@api_client' => $subscription
          ->getApiClient()
          ->label(),
      ]));
    } catch (\Exception $e) {
      drupal_set_message($this
        ->t('Failed to enable the default subscription: @error', [
        '@error' => $e
          ->getMessage(),
      ]), 'error');
    }
    return $this
      ->redirect('entity.brightcove_subscription.list');
  }

  /**
   * Disabled and removed the default Subscription from Brightcove.
   *
   * @param string $id
   *   The ID of the Brightcove Subscription.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Redirect response to redirect user after enabling the default
   *   subscription.
   */
  public function disable($id) {
    try {
      $subscription = BrightcoveSubscription::load($id);
      $subscription
        ->deleteFromBrightcove();
      drupal_set_message($this
        ->t('Default subscription for the "@api_client" API client has been successfully disabled.', [
        '@api_client' => $subscription
          ->getApiClient()
          ->label(),
      ]));
    } catch (\Exception $e) {
      drupal_set_message($this
        ->t('Failed to disable the default subscription: @error', [
        '@error' => $e
          ->getMessage(),
      ]), 'error');
    }
    return $this
      ->redirect('entity.brightcove_subscription.list');
  }

  /**
   * Creates default subscriptions.
   *
   * This method must be called through the site's URL, otherwise the default
   * subscriptions won't be possible to create, because of the missing site URL.
   */
  public function createDefaults() {
    try {

      // Get all available API clients.
      $api_clients = BrightcoveAPIClient::loadMultiple();
      foreach ($api_clients as $api_client) {
        $brightcove_subscription = BrightcoveSubscription::loadDefault($api_client);

        // Try to grab an existing subscription by the site's endpoint URL if
        // the default doesn't exist for the current API client.
        $default_endpoint = BrightcoveUtil::getDefaultSubscriptionUrl();
        if (empty($brightcove_subscription)) {
          $brightcove_subscription = BrightcoveSubscription::loadByEndpoint($default_endpoint);
        }

        // If there is an existing subscription with an endpoint, make it
        // default.
        if (!empty($brightcove_subscription)) {
          $this->connection
            ->update('brightcove_subscription')
            ->fields([
            'is_default' => 1,
          ])
            ->condition('id', $brightcove_subscription
            ->getId())
            ->execute();
        }
        else {

          // Check Brightcove whether if it has a subscription for the default
          // one.
          $subscriptions = BrightcoveSubscription::listFromBrightcove($api_client);
          $subscription_with_default_endpoint = NULL;
          foreach ($subscriptions as $subscription) {
            if ($subscription
              ->getEndpoint() == $default_endpoint) {
              $subscription_with_default_endpoint = $subscription;
              break;
            }
          }

          // Create a new default subscription for the API client.
          $brightcove_subscription = new BrightcoveSubscription(TRUE);
          $brightcove_subscription
            ->setEvents([
            'video-change',
          ]);
          $brightcove_subscription
            ->setEndpoint($default_endpoint);
          $brightcove_subscription
            ->setApiClient($api_client);
          if ($subscription_with_default_endpoint !== NULL) {
            $brightcove_subscription
              ->setBcSid($subscription_with_default_endpoint
              ->getId());
            $brightcove_subscription
              ->setStatus(TRUE);
          }
          else {
            $brightcove_subscription
              ->setStatus(FALSE);
          }
          $brightcove_subscription
            ->save();
        }
      }
      drupal_set_message($this
        ->t('Default subscriptions has been successfully created.'));
    } catch (\Exception $e) {
      drupal_set_message($this
        ->t('Failed to create default subscription(s), @error', [
        '@error' => $e
          ->getMessage(),
      ]), 'error');
      watchdog_exception('brightcove', $e, 'Failed to create default subscription(s), @error', [
        '@error' => $e
          ->getMessage(),
      ]);
    }
    return $this
      ->redirect('entity.brightcove_subscription.list');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BrightcoveSubscriptionController::$connection protected property Database connection.
BrightcoveSubscriptionController::$linkGenerator protected property Link generator.
BrightcoveSubscriptionController::$lock protected property Lock backend.
BrightcoveSubscriptionController::$videoStorage protected property BrightcoveVideo storage.
BrightcoveSubscriptionController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
BrightcoveSubscriptionController::createDefaults public function Creates default subscriptions.
BrightcoveSubscriptionController::createSubscription public function Create a subscription on Brightcove from an already existing entity.
BrightcoveSubscriptionController::disable public function Disabled and removed the default Subscription from Brightcove.
BrightcoveSubscriptionController::enable public function Enables and creates the default Subscription from Brightcove.
BrightcoveSubscriptionController::listSubscriptions public function Lists available Brightcove Subscriptions.
BrightcoveSubscriptionController::notificationCallback public function Menu callback to handle the Brightcove notification callback.
BrightcoveSubscriptionController::__construct public function BrightcoveSubscriptionController constructor.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.