Passed
Push — master ( 334365...ef9772 )
by Eric
14:05 queued 02:14
created

FrozenClock::fromUtc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
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 6
    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 2
    public function adjustTo(string $withModifier): void
47
    {
48 2
        $this->now = $this->now->modify($withModifier);
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 5
    public function now(): DateTimeImmutable
55
    {
56 5
        return $this->now;
57
    }
58
59
    /**
60
     * Sets the FrozenClock to a specific time.
61
     */
62 1
    public function setTo(DateTimeImmutable $now): void
63
    {
64 1
        $this->now = $now;
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70 3
    public static function fromUtc(): FrozenClock
71
    {
72 3
        return new FrozenClock(
73 3
            new DateTimeImmutable('now', new DateTimeZone('UTC'))
74 3
        );
75
    }
76
}
77