AbstractDateRangeIterator::current()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Adlogix\EventScheduler\DateRange;
6
7
use DateInterval;
8
use DateTimeImmutable;
9
use Iterator;
10
11
/**
12
 * @author Toni Van de Voorde <[email protected]>
13
 */
14
abstract class AbstractDateRangeIterator implements Iterator
15
{
16
    /**
17
     * @var DateRange
18
     */
19
    protected $dateRange;
20
21
    /**
22
     * @var DateInterval
23
     */
24
    protected $interval;
25
26
    /**
27
     * @var int
28
     */
29
    protected $key = 0;
30
31
    /**
32
     * @var DateTimeImmutable
33
     */
34
    protected $current;
35
36
    /**
37
     * @param DateRange         $dateRange
38
     * @param DateInterval|null $interval
39
     */
40 8
    public function __construct(DateRange $dateRange, DateInterval $interval = null)
41
    {
42 8
        $this->dateRange = $dateRange;
43 8
        $this->interval = $interval ?: new DateInterval('P1D');
44 8
    }
45
46
    /**
47
     * @return DateTimeImmutable
48
     */
49 8
    public function current(): DateTimeImmutable
50
    {
51 8
        return $this->current;
52
    }
53
54
    /**
55
     * @return int
56
     */
57 2
    public function key(): int
58
    {
59 2
        return $this->key;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65 8
    public function valid(): bool
66
    {
67 8
        return $this->current >= $this->dateRange->startDate()
68 8
            && $this->current <= $this->dateRange->endDate();
69
    }
70
}
71