You are here

abstract class IntervalBase in Commerce Recurring Framework 8

Base class for interval-based billing schedules.

Hierarchy

Expanded class hierarchy of IntervalBase

File

src/Plugin/Commerce/BillingSchedule/IntervalBase.php, line 13

Namespace

Drupal\commerce_recurring\Plugin\Commerce\BillingSchedule
View source
abstract class IntervalBase extends BillingScheduleBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'trial_interval' => [],
      'interval' => [
        'number' => 1,
        'unit' => 'month',
      ],
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['#attached']['library'][] = 'commerce_recurring/admin';
    $form['trial_interval'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'interval',
        ],
      ],
      '#open' => TRUE,
    ];
    $form['trial_interval']['allow_trials'] = [
      '#type' => 'checkbox',
      '#title' => t('Allow free trials'),
      '#default_value' => !empty($this->configuration['trial_interval']),
    ];

    // Default the trial interval to the interval for easier configuration.
    if (empty($this->configuration['trial_interval'])) {
      $this->configuration['trial_interval'] = $this->configuration['interval'];
    }
    $form['trial_interval']['number'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Trial interval'),
      '#default_value' => $this->configuration['trial_interval']['number'],
      '#min' => 1,
      '#states' => [
        'visible' => [
          ':input[name="configuration[' . $this->pluginId . '][trial_interval][allow_trials]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['trial_interval']['unit'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Unit'),
      '#title_display' => 'invisible',
      '#options' => [
        'hour' => $this
          ->t('Hour'),
        'day' => $this
          ->t('Day'),
        'week' => $this
          ->t('Week'),
        'month' => $this
          ->t('Month'),
        'year' => $this
          ->t('Year'),
      ],
      '#default_value' => $this->configuration['trial_interval']['unit'],
      '#states' => [
        'visible' => [
          ':input[name="configuration[' . $this->pluginId . '][trial_interval][allow_trials]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['interval'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'interval',
        ],
      ],
      '#open' => TRUE,
    ];
    $form['interval']['number'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Interval'),
      '#default_value' => $this->configuration['interval']['number'],
      '#min' => 1,
      '#required' => TRUE,
    ];
    $form['interval']['unit'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Unit'),
      '#title_display' => 'invisible',
      '#options' => [
        'hour' => $this
          ->t('Hour'),
        'day' => $this
          ->t('Day'),
        'week' => $this
          ->t('Week'),
        'month' => $this
          ->t('Month'),
        'year' => $this
          ->t('Year'),
      ],
      '#default_value' => $this->configuration['interval']['unit'],
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    if (!$form_state
      ->getErrors()) {
      $values = $form_state
        ->getValue($form['#parents']);
      $this->configuration = [];
      $this->configuration['trial_interval'] = [];
      if (!empty($values['trial_interval']['allow_trials'])) {
        $this->configuration['trial_interval'] = [
          'number' => $values['trial_interval']['number'],
          'unit' => $values['trial_interval']['unit'],
        ];
      }
      $this->configuration['interval'] = $values['interval'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function allowTrials() {
    return !empty($this->configuration['trial_interval']);
  }

  /**
   * {@inheritdoc}
   */
  public function generateTrialPeriod(DrupalDateTime $start_date) {

    // Trial periods are always rolling (starting from the given start date).
    $interval = new Interval($this->configuration['trial_interval']['number'], $this->configuration['trial_interval']['unit']);
    return new BillingPeriod($start_date, $interval
      ->add($start_date));
  }

  /**
   * Gets the current interval.
   *
   * @return \Drupal\commerce\Interval
   *   The interval.
   */
  protected function getInterval() {
    return new Interval($this->configuration['interval']['number'], $this->configuration['interval']['unit']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BillingScheduleBase::$entityId protected property The ID of the parent config entity.
BillingScheduleBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
BillingScheduleBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
BillingScheduleBase::getLabel public function Gets the billing schedule label. Overrides BillingScheduleInterface::getLabel
BillingScheduleBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
BillingScheduleBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
BillingScheduleBase::__construct public function Constructs a new BillingScheduleBase object. Overrides PluginBase::__construct
BillingScheduleInterface::generateFirstBillingPeriod public function Generates the first billing period. 2
BillingScheduleInterface::generateNextBillingPeriod public function Generates the next billing period. 2
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
IntervalBase::allowTrials public function Checks whether the billing schedule allows trials. Overrides BillingScheduleInterface::allowTrials
IntervalBase::buildConfigurationForm public function Form constructor. Overrides BillingScheduleBase::buildConfigurationForm 1
IntervalBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides BillingScheduleBase::defaultConfiguration 1
IntervalBase::generateTrialPeriod public function Generates the trial period. Overrides BillingScheduleInterface::generateTrialPeriod
IntervalBase::getInterval protected function Gets the current interval.
IntervalBase::submitConfigurationForm public function Form submission handler. Overrides BillingScheduleBase::submitConfigurationForm 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.