Passed
Pull Request — master (#84)
by Frank
06:34
created

TestClock   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10
c 4
b 0
f 0
wmc 8

6 Methods

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