Interval   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 13
eloc 26
dl 0
loc 80
ccs 28
cts 28
cp 1
rs 10
c 3
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getStop() 0 3 1
A getStart() 0 3 1
A getInterval() 0 3 1
B __construct() 0 37 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Interval;
5
6
use DateTime;
7
use DateTimeInterface;
8
use InvalidArgumentException;
9
10
class Interval implements IntervalInterface
11
{
12
    protected DateTimeInterface $start;
13
14
    protected DateTimeInterface $stop;
15
16
    /**
17
     * Interval constructor.
18
     *
19
     * @param \DateTimeInterface|int|string      $start DateTime object, unix timestamp or string accepted by
20
     *                                                  DateTime::__construct
21
     * @param \DateTimeInterface|int|string|null $stop  DateTime object, unix timestamp or string accepted by
22
     *                                                  DateTime::__construct
23
     *
24
     * @throws \Exception
25
     */
26 134
    public function __construct(DateTimeInterface|int|string $start, DateTimeInterface|int|string|null $stop = null)
27
    {
28
        // Check if we received a "raw" interval string, like 2019-04-15T08:00:00.000Z/2019-04-15T09:00:00.000Z
29 134
        if (is_string($start) && $stop === null) {
30 57
            if (str_contains($start, '/')) {
31 56
                [$start, $stop] = explode('/', $start);
32
            } else {
33 1
                throw new InvalidArgumentException(
34 1
                    'Invalid interval given: ' . $start . '. ' .
35 1
                    'You should supply a valid interval (start and stop date) which is split by a forward slash (/).'
36 1
                );
37
            }
38
        }
39
40
        // Check if some gecko forgot the stop date.
41 133
        if ($stop === null) {
42 1
            throw new InvalidArgumentException(
43 1
                'Invalid parameters given for the interval() method. ' .
44 1
                'You should supply a valid start and stop value. This can be in string form ("start/stop"), or specify ' .
45 1
                'the start and stop parameters individually'
46 1
            );
47
        }
48
49 132
        if (!$start instanceof DateTimeInterface) {
50 124
            $start = new DateTime(is_numeric($start) ? "@$start" : $start);
51
        }
52
53 131
        if (!$stop instanceof DateTimeInterface) {
54 123
            $stop = new DateTime(is_numeric($stop) ? "@$stop" : $stop);
55
        }
56
57 131
        if ($stop < $start) {
58 1
            throw new InvalidArgumentException('The end date must be greater than the start date');
59
        }
60
61 130
        $this->start = $start;
62 130
        $this->stop  = $stop;
63
    }
64
65
    /**
66
     * Return the interval in ISO-8601 format.
67
     * For example: "2012-01-01T00:00:00.000/2012-01-03T00:00:00.000"
68
     *
69
     * @return string
70
     */
71 49
    public function getInterval(): string
72
    {
73 49
        return $this->start->format('Y-m-d\TH:i:s.000\Z') . '/' . $this->stop->format('Y-m-d\TH:i:s.000\Z');
74
    }
75
76
    /**
77
     * @return \DateTimeInterface
78
     */
79 18
    public function getStart(): DateTimeInterface
80
    {
81 18
        return $this->start;
82
    }
83
84
    /**
85
     * @return \DateTimeInterface
86
     */
87 18
    public function getStop(): DateTimeInterface
88
    {
89 18
        return $this->stop;
90
    }
91
}