Passed
Pull Request — master (#25)
by Wouter
01:37
created

DateRange::getEndDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Vanbrabantf\NpmStatFetcher\Dates;
4
5
use Cake\Chronos\Chronos;
6
use DateTimeInterface;
7
8
final class DateRange
9
{
10
    /**
11
     * @var DateTimeInterface
12
     */
13
    private $startDate;
14
15
    /**
16
     * @var DateTimeInterface
17
     */
18
    private $endDate;
19
20
    /**
21
     * @param DateTimeInterface $startDate
22
     * @param DateTimeInterface $endDate
23
     */
24
    public function __construct(
25
        DateTimeInterface $startDate,
26
        DateTimeInterface $endDate
27
    ) {
28
        $this->validateDateRange($startDate, $endDate);
29
30
        $this->startDate = $startDate;
31
        $this->endDate = $endDate;
32
    }
33
34
    /**
35
     * @return DateTimeInterface
36
     */
37
    public function getStartDate(): DateTimeInterface
38
    {
39
        return $this->startDate;
40
    }
41
42
    /**
43
     * @return DateTimeInterface
44
     */
45
    public function getEndDate(): DateTimeInterface
46
    {
47
        return $this->endDate;
48
    }
49
50
    /**
51
     * @param DateTimeInterface $start
52
     * @param DateTimeInterface $end
53
     * @throws DateException
54
     */
55
    private function validateDateRange(DateTimeInterface $start, DateTimeInterface $end)
56
    {
57
        $start = new Chronos($start);
0 ignored issues
show
Bug introduced by
$start of type DateTimeInterface is incompatible with the type null|string expected by parameter $time of Cake\Chronos\Chronos::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $start = new Chronos(/** @scrutinizer ignore-type */ $start);
Loading history...
58
        $end = new Chronos($end);
59
60
        if ($start->isFuture()) {
61
            throw new DateException("Start date \"{$start}\" must be in the past");
62
        }
63
64
        if ($end->isFuture()) {
65
            throw new DateException("End date \"{$end}\" must be in the past");
66
        }
67
68
        if ($start > $end) {
69
            throw new DateException("Start date \"{$start}\" must be smaller than end date \"{$end}\"");
70
        }
71
    }
72
}
73