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

TestClock::pointInTime()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
19
    /**
20
     * @var DateTimeImmutable
21
     */
22
    private $time;
23
24
    /**
25
     * @var DateTimeZone
26
     */
27
    private $timeZone;
28
29
    public function __construct(DateTimeZone $timeZone = null)
30 27
    {
31
        $this->timeZone = $timeZone ?: new DateTimeZone('UTC');
32 27
        $this->tick();
33 27
    }
34 27
35
    public function tick(): void
36 27
    {
37
        $this->time = new DateTimeImmutable('now', $this->timeZone);
38 27
    }
39 27
40
    public function fixate(string $input): void
41 3
    {
42
        $preciseTime = sprintf('%s.000000', $input);
43 3
        $dateTime = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $preciseTime, $this->timeZone);
44 3
45
        if ( ! $dateTime instanceof DateTimeImmutable) {
0 ignored issues
show
introduced by
$dateTime is always a sub-type of DateTimeImmutable.
Loading history...
46 3
            throw new InvalidArgumentException("Invalid input for date/time fixation provided: {$input}");
47 1
        }
48
49
        $this->time = $dateTime;
50 2
    }
51 2
52
    public function moveForward(DateInterval $interval): void
53 1
    {
54
        $this->time = $this->now()->add($interval);
55 1
    }
56 1
57
    public function now(): DateTimeImmutable
58 10
    {
59
        return $this->time;
60 10
    }
61
62
    public function timeZone(): DateTimeZone
63 6
    {
64
        return $this->timeZone;
65 6
    }
66
}
67