You are here

public function Subscription::addScheduledChange in Commerce Recurring Framework 8

Adds a scheduled change for the given field.

Parameters

string $field_name: The field_name.

mixed $value: The value.

Return value

$this

Overrides SubscriptionInterface::addScheduledChange

1 call to Subscription::addScheduledChange()
Subscription::cancel in src/Entity/Subscription.php
Cancel the subscription.

File

src/Entity/Subscription.php, line 610

Class

Subscription
Defines the subscription entity.

Namespace

Drupal\commerce_recurring\Entity

Code

public function addScheduledChange($field_name, $value) {
  if (!$this
    ->hasField($field_name)) {
    throw new \InvalidArgumentException(sprintf('Invalid field_name "%s" specified for the given scheduled change.', $field_name));
  }
  if ($field_name === 'purchased_entity') {
    throw new \InvalidArgumentException('Scheduling a plan change is not yet supported.');
  }

  // Other scheduled changes are made irrelevant by a state change.
  if ($field_name === 'state') {
    $this
      ->removeScheduledChanges();
  }
  else {

    // There can only be a single scheduled change for a given field.
    $this
      ->removeScheduledChanges($field_name);
  }
  $scheduled_change = new ScheduledChange($field_name, $value, \Drupal::time()
    ->getRequestTime());
  $this
    ->get('scheduled_changes')
    ->appendItem($scheduled_change);
  return $this;
}