Lines
100.00%
9 / 9
Functions and Methods
100.00%
6 / 6
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| HistoryQuery | 100.00% | 9 / 9 | 100.00% | 6 / 6 | 8 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| lastFor | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| between | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| withAttributes | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| withAttributeChanges | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| resolveWindowAt | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Contracts\History; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\HistoryException; | |
| 8 | use Stewart\Contracts\Exception\TimeException; | |
| 9 | use Stewart\Contracts\Time\Duration; | |
| 10 | use Stewart\Contracts\Time\Instant; | |
| 11 | ||
| 12 | final readonly class HistoryQuery | |
| 13 | { | |
| 14 | private function __construct( | |
| 15 | private ?Duration $lookback, | |
| 16 | private ?HistoryWindow $fixedWindow, | |
| 17 | public HistoryDetail $detail, | |
| 18 | ) {} | |
| 19 | ||
| 20 | /** @throws TimeException */ | |
| 21 | public static function lastFor(Duration $lookback): self | |
| 22 | { | |
| 23 | return new self($lookback->requireAtLeastOneMillisecond('a history lookback'), null, HistoryDetail::StateChanges); | |
| 24 | } | |
| 25 | ||
| 26 | /** @throws HistoryException */ | |
| 27 | public static function between(Instant $startsAt, Instant $endsAt): self | |
| 28 | { | |
| 29 | return new self(null, new HistoryWindow($startsAt, $endsAt), HistoryDetail::StateChanges); | |
| 30 | } | |
| 31 | ||
| 32 | public function withAttributes(): self | |
| 33 | { | |
| 34 | return $this->detail === HistoryDetail::StateChanges ? new self($this->lookback, $this->fixedWindow, HistoryDetail::StateChangesWithAttributes) : $this; | |
| 35 | } | |
| 36 | ||
| 37 | public function withAttributeChanges(): self | |
| 38 | { | |
| 39 | return new self($this->lookback, $this->fixedWindow, HistoryDetail::AllChanges); | |
| 40 | } | |
| 41 | ||
| 42 | public function resolveWindowAt(Instant $now): HistoryWindow | |
| 43 | { | |
| 44 | if ($this->fixedWindow !== null) { | |
| 45 | return $this->fixedWindow; | |
| 46 | } | |
| 47 | ||
| 48 | \assert($this->lookback !== null); | |
| 49 | ||
| 50 | return new HistoryWindow($now->minus($this->lookback), $now); | |
| 51 | } | |
| 52 | } |