You are here

protected function License::calculateExpirationTime in Commerce License 8.2

Calculate the expiration time for this license from a start time.

Parameters

int $start: The timestamp to calculate the duration from.

Return value

int The expiry timestamp, or the value \Drupal\recurring_period\Plugin\RecurringPeriod\RecurringPeriodInterface::UNLIMITED if the license does not expire.

1 call to License::calculateExpirationTime()
License::preSave in src/Entity/License.php
Acts on an entity before the presave hook is invoked.

File

src/Entity/License.php, line 246

Class

License
Defines the License entity.

Namespace

Drupal\commerce_license\Entity

Code

protected function calculateExpirationTime($start) {

  /** @var \Drupal\recurring_period\Plugin\RecurringPeriod\RecurringPeriodInterface $expiration_type_plugin */
  $expiration_type_plugin = $this
    ->getExpirationPlugin();

  // The recurring period plugin needs DateTimeImmutable objects in order
  // to handle timezones properly. So we convert the timestamp to a datetime
  // using an appropriate timezone for the user, and then convert the
  // expiration back into a UTC timestamp.
  $start_date = (new \DateTimeImmutable('@' . $start))
    ->setTimezone(new \DateTimeZone(commerce_license_get_user_timezone($this
    ->getOwner())));
  $expiration_date = $expiration_type_plugin
    ->calculateDate($start_date);

  // The returned date is either \DateTimeImmutable or
  // \Drupal\recurring_period\Plugin\RecurringPeriod\RecurringPeriodInterface::UNLIMITED.
  if (is_object($expiration_date)) {
    return $expiration_date
      ->format('U');
  }
  else {
    return $expiration_date;
  }
}