Completed
Push — master ( 2a5d39...9c9098 )
by Olivier
02:26
created

Calendar::hasEventBetween()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Calendar\Model;
6
7
class Calendar
8
{
9
    private $events = [];
10
11 4
    public function getFlattenedEvents(): array
12
    {
13 4
        $events = [];
14 4
        $eventsInARecurrence = [];
15 4
        $recurringEvents = [];
16 4
        foreach ($this->getEvents() as $event) {
17 3
            if ($event->isARecurrence()) {
18 2
                $eventsInARecurrence[] = $event;
19
20 2
                continue;
21
            }
22
23 3
            if ($event->isRecurring()) {
24 3
                $recurringEvents[] = $event;
25
            }
26
27 3
            $events[$event->getStartAt()->getTimestamp()] = $event;
28
        }
29
30
        // Deal with recurring events
31 4
        foreach ($recurringEvents as $event) {
32 3
            $startAt = $event->getStartAt();
33 3
            $duration = $event->getEndAt()->diff($startAt);
34 3
            $modifier = $event->getRecurrenceRule()->getModifier();
35
36 3
            $until = $event->getLastEventStartAt();
37 3
            $nextEventStartAt = $startAt->modify($modifier);
38 3
            while ($nextEventStartAt < $until) {
39 3
                $newEvent = new Event(
40 3
                    $nextEventStartAt,
41 3
                    $nextEventStartAt->add($duration)
42
                );
43 3
                $newEvent->setSummary($event->getSummary());
44 3
                $newEvent->setDescription($event->getDescription());
45
46 3
                $events[$newEvent->getStartAt()->getTimestamp()] = $newEvent;
47 3
                $nextEventStartAt = $nextEventStartAt->modify($modifier);
48
            }
49
        }
50
51
        // Replace recurring events with updated ones
52 4
        foreach ($eventsInARecurrence as $event) {
53 2
            $events[$event->getRecurrenceId()->getTimestamp()] = $event;
54
        }
55
56 4
        ksort($events);
57
58 4
        return $events;
59
    }
60
61
    public function hasEventBetween(\DateTimeImmutable $from, \DateTimeImmutable $to, bool $strict = true): bool
62
    {
63
        foreach ($this->getFlattenedEvents() as $event) {
64
            if ($event->isBetween($from, $to, $strict)) {
65
                return true;
66
            }
67
        }
68
69
        return false;
70
    }
71
72 4
    public function hasEvents(): bool
73
    {
74 4
        return 0 < count($this->events);
75
    }
76
77 3
    public function addEvent(Event $event): self
78
    {
79 3
        $this->events[] = $event;
80
81 3
        return $this;
82
    }
83
84 4
    public function getEvents(): array
85
    {
86 4
        return $this->events;
87
    }
88
}
89