Passed
Push — master ( a62b41...518ca5 )
by Oliver
03:36
created

TimeRange::end()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Webfactor\Laravel\OpeningHours\Entities;
4
5
use Webfactor\Laravel\OpeningHours\Exceptions\InvalidTimeRangeString;
6
7
class TimeRange
8
{
9
    /** @var \Spatie\OpeningHours\Time */
10
    protected $start;
11
12
    /** @var \Spatie\OpeningHours\Time */
13
    protected $end;
14
15 31
    protected function __construct(Time $start, Time $end)
16
    {
17 31
        $this->start = $start;
0 ignored issues
show
Documentation Bug introduced by
It seems like $start of type object<Webfactor\Laravel...ingHours\Entities\Time> is incompatible with the declared type object<Spatie\OpeningHours\Time> of property $start.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
18 31
        $this->end = $end;
0 ignored issues
show
Documentation Bug introduced by
It seems like $end of type object<Webfactor\Laravel...ingHours\Entities\Time> is incompatible with the declared type object<Spatie\OpeningHours\Time> of property $end.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
19 31
    }
20
21 32
    public static function fromString(string $string): self
22
    {
23 32
        $times = explode('-', $string);
24
25 32
        if (count($times) !== 2) {
26 1
            throw InvalidTimeRangeString::forString($string);
27
        }
28
29 31
        return new self(Time::fromString($times[0]), Time::fromString($times[1]));
30
    }
31
32 7
    public function start(): Time
33
    {
34 7
        return $this->start;
35
    }
36
37 3
    public function end(): Time
38
    {
39 3
        return $this->end;
40
    }
41
42 17
    public function spillsOverToNextDay(): bool
43
    {
44 17
        return $this->end->isBefore($this->start);
45
    }
46
47 16
    public function containsTime(Time $time): bool
48
    {
49 16
        if ($this->spillsOverToNextDay()) {
50 2
            if ($time->isAfter($this->start)) {
51 2
                return $time->isAfter($this->end);
52
            }
53
54 2
            return $time->isBefore($this->end);
55
        }
56
57 16
        return $time->isSameOrAfter($this->start) && $time->isBefore($this->end);
58
    }
59
60 10
    public function overlaps(TimeRange $timeRange): bool
61
    {
62 10
        return $this->containsTime($timeRange->start) || $this->containsTime($timeRange->end);
63
    }
64
65 9
    public function format(string $timeFormat = 'H:i', string $rangeFormat = '%s-%s'): string
66
    {
67 9
        return sprintf($rangeFormat, $this->start->format($timeFormat), $this->end->format($timeFormat));
68
    }
69
70 8
    public function __toString(): string
71
    {
72 8
        return $this->format();
73
    }
74
}
75