You are here

public function BrightcoveSubscription::saveToBrightcove in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 src/Entity/BrightcoveSubscription.php \Drupal\brightcove\Entity\BrightcoveSubscription::saveToBrightcove()
  2. 3.x src/Entity/BrightcoveSubscription.php \Drupal\brightcove\Entity\BrightcoveSubscription::saveToBrightcove()

Saves the subscription entity to Brightcove.

Throws

\Drupal\brightcove\Entity\Exception\BrightcoveSubscriptionException If the Subscription wasn't saved to Brightcove successfully.

1 call to BrightcoveSubscription::saveToBrightcove()
BrightcoveSubscription::save in src/Entity/BrightcoveSubscription.php

File

src/Entity/BrightcoveSubscription.php, line 488

Class

BrightcoveSubscription
Defines the Brightcove Subscription entity.

Namespace

Drupal\brightcove\Entity

Code

public function saveToBrightcove() {
  try {

    // Get CMS API.
    $cms = BrightcoveUtil::getCmsApi($this->apiClient
      ->id());
    if ($is_default = $this
      ->isDefault()) {

      // Make sure that when the default is enabled, always use the correct
      // URL.
      $default_endpoint = BrightcoveUtil::getDefaultSubscriptionUrl();
      if ($this->endpoint != $default_endpoint) {
        $this
          ->setEndpoint($default_endpoint);
      }
    }

    // Create subscription.
    $subscription_request = new SubscriptionRequest();
    $subscription_request
      ->setEndpoint($this
      ->getEndpoint());
    $subscription_request
      ->setEvents($this
      ->getEvents());
    $new_subscription = $cms
      ->createSubscription($subscription_request);
    $this
      ->setBcSid($new_subscription
      ->getId());

    // If it's a default subscription update the local entity to enable it.
    if ($is_default) {
      $this
        ->setStatus(TRUE);
      $this
        ->save();
    }
  } catch (\Exception $e) {
    watchdog_exception('brightcove', $e, $e
      ->getMessage());
    throw new BrightcoveSubscriptionException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}