Passed
Pull Request — master (#84)
by Frank
12:21 queued 02:23
created

TestClock::dateTime()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\Time;
6
7
use DateInterval;
8
use DateTimeImmutable;
9
use DateTimeZone;
10
use InvalidArgumentException;
11
12
class TestClock implements Clock
13
{
14
    /**
15
     * @private
16
     */
17
    const FORMAT_OF_TIME = 'Y-m-d H:i:s.uO';
18
    private DateTimeImmutable $time;
19
    private DateTimeZone $timeZone;
20
21
    public function __construct(DateTimeZone $timeZone = null)
22
    {
23
        $this->timeZone = $timeZone ?: new DateTimeZone('UTC');
24
        $this->tick();
25
    }
26
27
    public function tick(): void
28
    {
29
        $this->time = new DateTimeImmutable('now', $this->timeZone);
30 27
    }
31
32 27
    public function fixate(string $input): void
33 27
    {
34 27
        $preciseTime = sprintf('%s.000000', $input);
35
        $dateTime = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $preciseTime, $this->timeZone);
36 27
37
        if ( ! $dateTime instanceof DateTimeImmutable) {
0 ignored issues
show
introduced by
$dateTime is always a sub-type of DateTimeImmutable.
Loading history...
38 27
            throw new InvalidArgumentException("Invalid input for date/time fixation provided: {$input}");
39 27
        }
40
41 3
        $this->time = $dateTime;
42
    }
43 3
44 3
    public function moveForward(DateInterval $interval): void
45
    {
46 3
        $this->time = $this->now()->add($interval);
47 1
    }
48
49
    public function now(): DateTimeImmutable
50 2
    {
51 2
        return $this->time;
52
    }
53 1
54
    public function timeZone(): DateTimeZone
55 1
    {
56 1
        return $this->timeZone;
57
    }
58
}
59