You are here

public static function BrightcoveSubscription::createOrUpdate in Brightcove Video Connect 8

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

Create or update a Subscription entity.

Parameters

\Brightcove\Object\Subscription $subscription: Subscription object from Brightcove.

\Drupal\brightcove\Entity\BrightcoveAPIClient|null $api_client: Loaded API client entity, or null.

Throws

\Drupal\brightcove\Entity\Exception\BrightcoveSubscriptionException

\Exception

1 call to BrightcoveSubscription::createOrUpdate()
BrightcoveSubscriptionQueueWorker::processItem in src/Plugin/QueueWorker/BrightcoveSubscriptionQueueWorker.php
Works on a single queue item.

File

src/Entity/BrightcoveSubscription.php, line 584

Class

BrightcoveSubscription
Defines the Brightcove Subscription entity.

Namespace

Drupal\brightcove\Entity

Code

public static function createOrUpdate(Subscription $subscription, BrightcoveAPIClient $api_client = NULL) {

  /** @var \Drupal\brightcove\Entity\BrightcoveSubscription $brightcove_subscription */
  $brightcove_subscription = self::loadByEndpoint($subscription
    ->getEndpoint());

  // If there is no Subscription by the endpoint, try to get one by its ID.
  if (empty($brightcove_subscription)) {

    /** @var \Drupal\brightcove\Entity\BrightcoveSubscription $subscription */
    $brightcove_subscription = self::loadByBcSid($subscription
      ->getId());
  }

  // Create new subscription if needed.
  if (empty($brightcove_subscription)) {
    $brightcove_subscription = new BrightcoveSubscription();
    $brightcove_subscription->bcsid = $subscription
      ->getId();

    /** @var \Drupal\brightcove\Entity\BrightcoveAPIClient $api_client */
    if (!empty($api_client)) {
      $brightcove_subscription
        ->setApiClient($api_client);
    }
    else {
      return;
    }
  }
  $needs_save = FALSE;

  // Update ID.
  if (($bcsid = $subscription
    ->getId()) != $brightcove_subscription
    ->getBcSid()) {
    $brightcove_subscription
      ->setBcSid($bcsid);
    $needs_save = TRUE;
  }

  // In case of an inactive default subscription set status to TRUE.
  if ($brightcove_subscription
    ->isDefault() && !$brightcove_subscription
    ->isActive()) {
    $brightcove_subscription
      ->setStatus(TRUE);
    $needs_save = TRUE;
  }

  // Update endpoint.
  if (($endpoint = $subscription
    ->getEndpoint()) != $brightcove_subscription
    ->getEndpoint()) {
    $brightcove_subscription
      ->setEndpoint($endpoint);
    $needs_save = TRUE;
  }

  // Update events.
  $events = $subscription
    ->getEvents();
  if (!is_array($events)) {
    $events = [
      $events,
    ];
  }
  if ($events != $brightcove_subscription
    ->getEvents()) {
    $brightcove_subscription
      ->setEvents($events);
    $needs_save = TRUE;
  }

  // Save the Subscription if needed.
  if ($needs_save) {
    $brightcove_subscription
      ->save(FALSE);
  }
}