Passed
Push — master ( 0c6c2f...4ea690 )
by Frank
02:07
created

TestClock::tick()   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 DateTimeImmutable;
8
use DateTimeZone;
9
use EventSauce\EventSourcing\PointInTime;
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
19
    /**
20
     * @var DateTimeImmutable
21
     */
22
    private $time;
23
24
    /**
25
     * @var DateTimeZone
26
     */
27
    private $timeZone;
28
29 18
    public function __construct(DateTimeZone $timeZone = null)
30
    {
31 18
        $this->timeZone = $timeZone ?: new DateTimeZone('UTC');
32 18
        $this->tick();
33 18
    }
34
35 18
    public function tick()
36
    {
37 18
        $this->time = new DateTimeImmutable('now', $this->timeZone);
38 18
    }
39
40 2
    public function fixate(string $input)
41
    {
42 2
        $preciseTime = sprintf('%s.000000', $input);
43 2
        $dateTime = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $preciseTime, $this->timeZone);
44
45 2
        if ( ! $dateTime instanceof DateTimeImmutable) {
46 1
            throw new InvalidArgumentException("Invalid input for date/time fixation provided: {$input}");
47
        }
48
49 1
        $this->time = $dateTime;
50 1
    }
51
52 8
    public function dateTime(): DateTimeImmutable
53
    {
54 8
        return $this->time;
55
    }
56
57 5
    public function pointInTime(): PointInTime
58
    {
59 5
        return PointInTime::fromDateTime($this->dateTime());
60
    }
61
}
62