TimeMachine.php in Preview Link 2.x
File
tests/modules/preview_link_test_time/src/TimeMachine.php
View source
<?php
declare (strict_types=1);
namespace Drupal\preview_link_test_time;
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;
}
}