Code Duplication    Length = 95-103 lines in 2 locations

src/States/InfiniteEndRange.php 1 location

@@ 12-106 (lines=95) @@
9
/**
10
 * Class InfiniteEndRange.
11
 */
12
final class InfiniteEndRange extends UndefinedRange
13
{
14
    /**
15
     * @var DateTimeInterface
16
     */
17
    private $startTime;
18
19
    /**
20
     * @param DateTimeInterface $startTime
21
     */
22
    public function __construct(DateTimeInterface $startTime)
23
    {
24
        $this->startTime = $startTime;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __toString(): string
31
    {
32
        return sprintf('%s/-', $this->startTime->format('c'));
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function jsonSerialize(): array
39
    {
40
        return ['startTime' => $this->startTime->format('c'), 'endTime' => null];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function hasStartTime(): bool
47
    {
48
        return true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function hasEndTime(): bool
55
    {
56
        return false;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getStartTime(): DateTimeInterface
63
    {
64
        return $this->startTime;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getEndTime(): DateTimeInterface
71
    {
72
        throw new DateRangeException('Date range is undefined');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function setStartTime(DateTimeInterface $time): RangeState
79
    {
80
        return new InfiniteEndRange($time);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function setEndTime(DateTimeInterface $time): RangeState
87
    {
88
        return new FiniteRange($this->startTime, $time);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function isStartAt(DateTimeInterface $time): bool
95
    {
96
        return $this->startTime->getTimestamp() === $time->getTimestamp();
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function isStarted(): bool
103
    {
104
        return $this->startTime <= new DateTimeImmutable();
105
    }
106
}
107

src/States/InfiniteStartRange.php 1 location

@@ 12-114 (lines=103) @@
9
/**
10
 * Class InfiniteStartRange.
11
 */
12
final class InfiniteStartRange extends UndefinedRange
13
{
14
    /**
15
     * @var DateTimeInterface
16
     */
17
    private $endTime;
18
19
    /**
20
     * @param DateTimeInterface $endTime
21
     */
22
    public function __construct(DateTimeInterface $endTime)
23
    {
24
        $this->endTime = $endTime;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __toString(): string
31
    {
32
        return sprintf('-/%s', $this->endTime->format('c'));
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function jsonSerialize(): array
39
    {
40
        return ['startTime' => null, 'endTime' => $this->endTime->format('c')];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function hasStartTime(): bool
47
    {
48
        return false;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function hasEndTime(): bool
55
    {
56
        return true;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getStartTime(): DateTimeInterface
63
    {
64
        throw new DateRangeException('Date range is undefined');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getEndTime(): DateTimeInterface
71
    {
72
        return $this->endTime;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function setStartTime(DateTimeInterface $time): RangeState
79
    {
80
        return new FiniteRange($time, $this->endTime);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function setEndTime(DateTimeInterface $time): RangeState
87
    {
88
        return new InfiniteStartRange($time);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function isEndAt(DateTimeInterface $time): bool
95
    {
96
        return $this->endTime->getTimestamp() === $time->getTimestamp();
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function isStarted(): bool
103
    {
104
        return true;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function isEnded(): bool
111
    {
112
        return $this->endTime <= new DateTimeImmutable();
113
    }
114
}
115