Completed
Push — master ( ffda25...474cbc )
by Olivier
01:48
created

Calendar::getNextEvent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 20
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
0 ignored issues
show
Unused Code introduced by
The parameter $from is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        return 0 > count($this->getEventsBetween($form, $to, $strict));
0 ignored issues
show
Bug introduced by
The variable $form does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
64
    }
65
66
    public function getEventsBetween(\DateTimeImmutable $from, \DateTimeImmutable $to, bool $strict = true)
67
    {
68
        $events = [];
69
        foreach ($this->getFlattenedEvents() as $event) {
70
            if ($event->isBetween($from, $to, $strict)) {
71
                $events[] = $event;
72
            }
73
        }
74
75
        return $events;
76
    }
77
78
    public function getNextEvent(): ?Event
79
    {
80
        if (!$this->hasEvents()) {
81
            return null;
82
        }
83
84
        $now = new \DateTimeImmutable();
85
86
        // Flattened events are ordered by date. As soon as we found an event
87
        // starting after $now, we can return it directly.
88
        foreach ($this->getFlattenedEvents() as $event) {
89
            if ($event->getStartAt() > $now) {
90
                return $event;
91
            }
92
        }
93
94
        return null;
95
    }
96
97 5
    public function hasEvents(): bool
98
    {
99 5
        return 0 < count($this->events);
100
    }
101
102 6
    public function addEvent(Event $event): self
103
    {
104 6
        $this->events[] = $event;
105
106 6
        return $this;
107
    }
108
109 7
    public function getEvents(): array
110
    {
111 7
        return $this->events;
112
    }
113
}
114