You are here

public function BrightcoveSubscription::save in Brightcove Video Connect 8

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

Parameters

bool $upload: Whether to upload the new subscription to Brightcove or not.

2 calls to BrightcoveSubscription::save()
BrightcoveSubscription::deleteFromBrightcove in src/Entity/BrightcoveSubscription.php
Delete the Subscription from Brightcove only.
BrightcoveSubscription::saveToBrightcove in src/Entity/BrightcoveSubscription.php
Saves the subscription entity to Brightcove.

File

src/Entity/BrightcoveSubscription.php, line 429

Class

BrightcoveSubscription
Defines the Brightcove Subscription entity.

Namespace

Drupal\brightcove\Entity

Code

public function save($upload = FALSE) {

  // Fields to insert or update.
  $fields = [
    'api_client_id' => $this
      ->getApiClient()
      ->id(),
    'endpoint' => $this
      ->getEndpoint(),
    'events' => serialize($this
      ->getEvents()),
  ];
  $fields += [
    'bcsid' => !empty($this->bcsid) ? $this
      ->getBcSid() : NULL,
  ];
  $fields += [
    'status' => $this
      ->isDefault() ? (int) $this
      ->isActive() : 1,
  ];

  // Save new entity.
  if ($this
    ->isNew()) {

    // Try to get a default subscription.
    $default_subscription = self::loadDefault($this->apiClient);
    $default_endpoint = BrightcoveUtil::getDefaultSubscriptionUrl();

    // Check whether we already have a default subscription for the API client
    // and throw an exception if one already exists.
    if ($this
      ->isDefault() && !empty($default_subscription)) {
      throw new BrightcoveSubscriptionException(strtr('Default subscription already exists for the :api_client API Client.', [
        ':api_client' => $this->apiClient
          ->getLabel(),
      ]));
    }
    elseif (empty($default_subscription) && $this
      ->getEndpoint() == $default_endpoint) {
      $this->default = TRUE;
    }

    // Create subscription on Brightcove only if the entity is new, as for now
    // it is not possible to update existing subscriptions.
    if ($upload) {
      $this
        ->saveToBrightcove();
    }

    // Insert Brightcove Subscription into the database.
    $this->connection
      ->insert('brightcove_subscription')
      ->fields($fields + [
      'is_default' => (int) $this
        ->isDefault(),
    ])
      ->execute();
  }
  elseif (!$upload) {
    $this->connection
      ->update('brightcove_subscription')
      ->fields($fields)
      ->condition('id', $this
      ->getId())
      ->execute();
  }
  else {
    throw new BrightcoveSubscriptionException('An already existing subscription cannot be updated!');
  }
}