FrozenClock::now()   A
last analyzed

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 DateTimeImmutable;
18
use DateTimeInterface;
19
use DateTimeZone;
20
21
/**
22
 * A clock frozen in time.
23
 */
24
final class FrozenClock implements ClockInterface
25
{
26 5
    public function __construct(private DateTimeImmutable $now) {}
27
28
    /**
29
     * @inheritDoc
30
     */
31 1
    public function __toString(): string
32
    {
33 1
        return \sprintf(
34 1
            '[FrozenClock(): unixtime: %s; iso8601: %s;]',
35 1
            $this->now()->format('U'),
36 1
            $this->now()->format(DateTimeInterface::ISO8601_EXPANDED)
37 1
        );
38
    }
39
40
    /**
41
     * Adjusts the FrozenClock time based on a given modifier.
42
     *
43
     * @see https://www.php.net/manual/en/datetime.formats.php
44
     * @see https://www.php.net/manual/en/class.datemalformedstringexception.php
45
     *
46
     * @psalm-suppress PossiblyFalsePropertyAssignmentValue
47
     */
48 1
    public function adjustTo(string $withModifier): void
49
    {
50 1
        $this->now = $this->now->modify($withModifier);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 5
    public function now(): DateTimeImmutable
57
    {
58 5
        return $this->now;
59
    }
60
61
    /**
62
     * Sets the FrozenClock to a specific time.
63
     */
64 1
    public function setTo(DateTimeImmutable $now): void
65
    {
66 1
        $this->now = $now;
67
    }
68
69
    /**
70
     * @inheritDoc
71
     *
72
     * @throws \DateMalformedStringException
73
     */
74 2
    public static function fromUtc(): FrozenClock
75
    {
76 2
        return new FrozenClock(
77 2
            new DateTimeImmutable('now', new DateTimeZone('UTC'))
78 2
        );
79
    }
80
}
81