Lines 100.00% 9 / 9
Functions and Methods 100.00% 9 / 9
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SunOffset 100.00% 9 / 9 100.00% 9 / 9 12 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 before 100.00% 1 / 1 100.00% 1 / 1 1
 after 100.00% 1 / 1 100.00% 1 / 1 1
 none 100.00% 1 / 1 100.00% 1 / 1 1
 isNone 100.00% 1 / 1 100.00% 1 / 1 1
 isBeforeEvent 100.00% 1 / 1 100.00% 1 / 1 2
 getMagnitude 100.00% 1 / 1 100.00% 1 / 1 1
 applyTo 100.00% 1 / 1 100.00% 1 / 1 2
 formatAsHaOffset 100.00% 1 / 1 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Time;
6
7final readonly class SunOffset
8{
9    private function __construct(
10        private Duration $magnitude,
11        private bool $beforeEvent,
12    ) {}
13
14    public static function before(Duration $magnitude): self
15    {
16        return new self($magnitude, true);
17    }
18
19    public static function after(Duration $magnitude): self
20    {
21        return new self($magnitude, false);
22    }
23
24    public static function none(): self
25    {
26        return new self(Duration::zero(), false);
27    }
28
29    public function isNone(): bool
30    {
31        return $this->magnitude->equals(Duration::zero());
32    }
33
34    public function isBeforeEvent(): bool
35    {
36        return $this->beforeEvent && !$this->isNone();
37    }
38
39    public function getMagnitude(): Duration
40    {
41        return $this->magnitude;
42    }
43
44    public function applyTo(Instant $eventTime): Instant
45    {
46        return $this->beforeEvent ? $eventTime->minus($this->magnitude) : $eventTime->plus($this->magnitude);
47    }
48
49    public function formatAsHaOffset(): string
50    {
51        return ($this->isBeforeEvent() ? '-' : '') . $this->magnitude->formatAsClock();
52    }
53}