You are here

public function BrightcoveSubscriptionController::createDefaults in Brightcove Video Connect 8.2

Same name and namespace in other branches
  1. 8 src/Controller/BrightcoveSubscriptionController.php \Drupal\brightcove\Controller\BrightcoveSubscriptionController::createDefaults()
  2. 3.x src/Controller/BrightcoveSubscriptionController.php \Drupal\brightcove\Controller\BrightcoveSubscriptionController::createDefaults()

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.

1 string reference to 'BrightcoveSubscriptionController::createDefaults'
brightcove.routing.yml in ./brightcove.routing.yml
brightcove.routing.yml

File

src/Controller/BrightcoveSubscriptionController.php, line 339

Class

BrightcoveSubscriptionController
Provides controller for subscription related callbacks.

Namespace

Drupal\brightcove\Controller

Code

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