Passed
Push — master ( 680a43...1384c2 )
by Eric
02:09
created

SystemClock::fromUtc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\Clock.
7
 *
8
 * (c) Eric Sizemore <[email protected]>
9
 *
10
 * This source file is subject to the MIT license. For the full copyright and
11
 * license information, please view the LICENSE file that was distributed with
12
 * this source code.
13
 */
14
15
namespace Esi\Clock;
16
17
use DateInvalidTimeZoneException;
18
use DateTimeImmutable;
19
use DateTimeInterface;
20
use DateTimeZone;
21
use Throwable;
22
23
use function date_default_timezone_get;
24
use function sprintf;
25
26
/**
27
 * A clock that relies on system time.
28
 *
29
 * @immutable
30
 */
31
final readonly class SystemClock implements ClockInterface
1 ignored issue
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 31 at column 6
Loading history...
32
{
33
    private DateTimeZone $timezone;
34
35
    /**
36
     * @throws DateInvalidTimeZoneException If $timezone is passed as string and is invalid.
37
     */
38 9
    public function __construct(DateTimeZone | string | null $timezone = null)
39
    {
40 9
        if (!$timezone instanceof DateTimeZone) {
41 4
            $timezone ??= 'UTC';
42
43
            try {
44 4
                $this->timezone = new DateTimeZone($timezone === '' ? 'UTC' : $timezone);
45
            // \Exception < PHP 8.3, \DateInvalidTimeZoneException >= PHP 8.3
46 1
            } catch (Throwable $throwable) {
47 1
                throw new DateInvalidTimeZoneException($throwable->getMessage(), \intval($throwable->getCode()), $throwable);
48
            }
49
50 3
            return;
51
        }
52
53 5
        $this->timezone = $timezone;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 1
    public function __toString(): string
60
    {
61 1
        return sprintf(
62 1
            '[SystemClock("%s"): unixtime: %s; iso8601: %s;]',
63 1
            $this->timezone->getName(),
64 1
            $this->now()->format('U'),
65 1
            $this->now()->format(DateTimeInterface::ISO8601_EXPANDED)
66 1
        );
67
    }
68
69
    /**
70
     * Create a FrozenClock from the current SystemClock now.
71
     */
72 1
    public function freeze(): FrozenClock
73
    {
74 1
        return new FrozenClock($this->now());
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 8
    public function now(): DateTimeImmutable
81
    {
82 8
        return new DateTimeImmutable('now', $this->timezone);
83
    }
84
85
    /**
86
     * Returns a new SystemClock at current system time using the system's timezone.
87
     *
88
     * @throws DateInvalidTimeZoneException
89
     */
90 1
    public static function fromSystemTimezone(): SystemClock
91
    {
92 1
        return new SystemClock(new DateTimeZone(date_default_timezone_get()));
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98 1
    public static function fromUtc(): SystemClock
99
    {
100 1
        return new SystemClock(new DateTimeZone('UTC'));
101
    }
102
}
103