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

TestClock   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 58
ccs 23
cts 23
cp 1
rs 10
c 4
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A pointInTime() 0 3 1
A __construct() 0 4 2
A fixate() 0 10 2
A tick() 0 3 1
A timeZone() 0 3 1
A dateTime() 0 3 1
A moveForward() 0 3 1
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