You are here

class TimeMachine in Preview Link 8

Service used to simulate time.

Hierarchy

Expanded class hierarchy of TimeMachine

1 string reference to 'TimeMachine'
preview_link_test.services.yml in tests/modules/preview_link_test/preview_link_test.services.yml
tests/modules/preview_link_test/preview_link_test.services.yml
1 service uses TimeMachine
datetime.time in tests/modules/preview_link_test/preview_link_test.services.yml
Drupal\preview_link_test\TimeMachine

File

tests/modules/preview_link_test/src/TimeMachine.php, line 11

Namespace

Drupal\preview_link_test
View source
class TimeMachine implements TimeInterface {

  /**
   * State.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * TimeMachine constructor.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   State.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public function getRequestTime() {
    return $this
      ->getTime()
      ->getTimestamp();
  }

  /**
   * {@inheritdoc}
   */
  public function getRequestMicroTime() {
    return (double) $this
      ->getTime()
      ->getTimestamp();
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentTime() {
    return $this
      ->getTime()
      ->getTimestamp();
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrentMicroTime() {
    return (double) $this
      ->getTime()
      ->getTimestamp();
  }

  /**
   * Sets time.
   *
   * @param \DateTimeInterface $dateTime
   *   Sets the time.
   */
  public function setTime(\DateTimeInterface $dateTime) {
    if ($dateTime instanceof \DateTime) {
      $dateTime = \DateTimeImmutable::createFromMutable($dateTime);
    }
    $this->state
      ->set('preview_link_test_time_machine', $dateTime);
  }

  /**
   * Get the time from state.
   *
   * @returns \DateTimeImmutable
   *   The date time.
   *
   * @throws \LogicException
   *   When date time was not set.
   */
  protected function getTime() {
    $dateTime = $this->state
      ->get('preview_link_test_time_machine');
    if (!isset($dateTime)) {
      throw new \LogicException('Current date time not set.');
    }
    return $dateTime;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TimeMachine::$state protected property State.
TimeMachine::getCurrentMicroTime public function Returns the current system time with microsecond precision. Overrides TimeInterface::getCurrentMicroTime
TimeMachine::getCurrentTime public function Returns the current system time as an integer. Overrides TimeInterface::getCurrentTime
TimeMachine::getRequestMicroTime public function Returns the timestamp for the current request with microsecond precision. Overrides TimeInterface::getRequestMicroTime
TimeMachine::getRequestTime public function Returns the timestamp for the current request. Overrides TimeInterface::getRequestTime
TimeMachine::getTime protected function Get the time from state.
TimeMachine::setTime public function Sets time.
TimeMachine::__construct public function TimeMachine constructor.