Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
OffsetClock
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
8 / 8
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 now
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 advance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 rewind
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 advanceSeconds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 rewindSeconds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 reset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 intervalToSeconds
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace Phexium\Plugin\Clock\Adapter;
11
12use DateInterval;
13use DateTimeImmutable;
14use Override;
15use Phexium\Plugin\Clock\Port\ClockInterface;
16
17final class OffsetClock implements ClockInterface
18{
19    private int $offsetSeconds = 0;
20
21    public function __construct(
22        private readonly ClockInterface $inner,
23    ) {}
24
25    #[Override]
26    public function now(): DateTimeImmutable
27    {
28        return $this->inner->now()->modify(sprintf('%+d seconds', $this->offsetSeconds));
29    }
30
31    public function advance(DateInterval $interval): void
32    {
33        $this->advanceSeconds($this->intervalToSeconds($interval));
34    }
35
36    public function rewind(DateInterval $interval): void
37    {
38        $this->rewindSeconds($this->intervalToSeconds($interval));
39    }
40
41    public function advanceSeconds(int $seconds): void
42    {
43        $this->offsetSeconds += $seconds;
44    }
45
46    public function rewindSeconds(int $seconds): void
47    {
48        $this->offsetSeconds -= $seconds;
49    }
50
51    public function reset(): void
52    {
53        $this->offsetSeconds = 0;
54    }
55
56    private function intervalToSeconds(DateInterval $interval): int
57    {
58        $seconds = ($interval->d * 86400)
59            + ($interval->h * 3600)
60            + ($interval->i * 60)
61            + $interval->s;
62
63        return $interval->invert === 1 ? -$seconds : $seconds;
64    }
65}