TimeMachine.php in Preview Link 8
File
tests/modules/preview_link_test/src/TimeMachine.php
View source
<?php
namespace Drupal\preview_link_test;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\State\StateInterface;
class TimeMachine implements TimeInterface {
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public function getRequestTime() {
return $this
->getTime()
->getTimestamp();
}
public function getRequestMicroTime() {
return (double) $this
->getTime()
->getTimestamp();
}
public function getCurrentTime() {
return $this
->getTime()
->getTimestamp();
}
public function getCurrentMicroTime() {
return (double) $this
->getTime()
->getTimestamp();
}
public function setTime(\DateTimeInterface $dateTime) {
if ($dateTime instanceof \DateTime) {
$dateTime = \DateTimeImmutable::createFromMutable($dateTime);
}
$this->state
->set('preview_link_test_time_machine', $dateTime);
}
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;
}
}