Lines 100.00% 3 / 3
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ShutdownTime 100.00% 3 / 3 100.00% 3 / 3 6 100.00% 1 / 1
 format 100.00% 1 / 1 100.00% 1 / 1 1
 parse 100.00% 1 / 1 100.00% 1 / 1 3
 isRequested 100.00% 1 / 1 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Tomaj\Hermes\Shutdown;
6
7use DateTimeInterface;
8
9/**
10 * Shared comparison for the built-in shutdowns.
11 *
12 * Shutdown times are stored as "seconds.microseconds" ("1700000000.123456"). A whole-second
13 * value, written by Hermes 5.x or by `touch`, counts as the start of that second.
14 *
15 * @internal
16 */
17final class ShutdownTime
18{
19    public static function format(DateTimeInterface $time): string
20    {
21        return $time->format('U.u');
22    }
23
24    public static function parse(mixed $value): ?float
25    {
26        return is_string($value) && is_numeric($value) ? (float) $value : null;
27    }
28
29    /**
30     * True when the shutdown was requested after the worker started and is not in the future.
31     */
32    public static function isRequested(float $shutdownTime, DateTimeInterface $startTime, float $now): bool
33    {
34        return $shutdownTime <= $now && $shutdownTime > $startTime->format('U.u');
35    }
36}