Passed
Push — master ( fe5827...f6519b )
by Frank
51s queued 12s
created

TestClock::moveForward()   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 1
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 EventSauce\EventSourcing\PointInTime;
11
use InvalidArgumentException;
12
13
class TestClock implements Clock
14
{
15
    /**
16
     * @private
17
     */
18
    const FORMAT_OF_TIME = 'Y-m-d H:i:s.uO';
19
20
    /**
21
     * @var DateTimeImmutable
22
     */
23
    private $time;
24
25
    /**
26
     * @var DateTimeZone
27
     */
28
    private $timeZone;
29
30 26
    public function __construct(DateTimeZone $timeZone = null)
31
    {
32 26
        $this->timeZone = $timeZone ?: new DateTimeZone('UTC');
33 26
        $this->tick();
34 26
    }
35
36 26
    public function tick(): void
37
    {
38 26
        $this->time = new DateTimeImmutable('now', $this->timeZone);
39 26
    }
40
41 3
    public function fixate(string $input): void
42
    {
43 3
        $preciseTime = sprintf('%s.000000', $input);
44 3
        $dateTime = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $preciseTime, $this->timeZone);
45
46 3
        if ( ! $dateTime instanceof DateTimeImmutable) {
47 1
            throw new InvalidArgumentException("Invalid input for date/time fixation provided: {$input}");
48
        }
49
50 2
        $this->time = $dateTime;
51 2
    }
52
53 1
    public function moveForward(DateInterval $interval): void
54
    {
55 1
        $this->time = $this->dateTime()->add($interval);
56 1
    }
57
58 9
    public function dateTime(): DateTimeImmutable
59
    {
60 9
        return $this->time;
61
    }
62
63 5
    public function pointInTime(): PointInTime
64
    {
65 5
        return PointInTime::fromDateTime($this->dateTime());
66
    }
67
68 1
    public function timeZone(): DateTimeZone
69
    {
70 1
        return $this->timeZone;
71
    }
72
}
73