Completed
Push — master ( 474cbc...4e1719 )
by Olivier
03:46
created

Calendar::getUpcomingEvents()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 9
cp 0
rs 9.7
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
use Shapin\Calendar\EventsFlattener;
8
9
class Calendar
10
{
11
    private $events = [];
12
13 5
    public function getFlattenedEvents(): array
14
    {
15 5
        $flattener = new EventsFlattener();
16
17 5
        $events = [];
18 5
        $eventsInARecurrence = [];
19 5
        $recurringEvents = [];
20 5
        foreach ($this->getEvents() as $event) {
21 4
            if ($event->isARecurrence()) {
22 2
                $eventsInARecurrence[] = $event;
23
24 2
                continue;
25
            }
26
27 4
            if ($event->isRecurring()) {
28 4
                $recurringEvents[] = $event;
29
30 4
                continue;
31
            }
32
33
            $events[$event->getStartAt()->getTimestamp()] = $event;
34
        }
35
36
        // Deal with recurring events
37 5
        foreach ($recurringEvents as $event) {
38 4
            $events += $flattener->flatten($event);
39
        }
40
41
        // Replace recurring events with updated ones
42 5
        foreach ($eventsInARecurrence as $event) {
43 2
            $events[$event->getRecurrenceId()->getTimestamp()] = $event;
44
        }
45
46 5
        ksort($events);
47
48 5
        return $events;
49
    }
50
51
    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...
52
    {
53
        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...
54
    }
55
56
    public function getEventsBetween(\DateTimeImmutable $from, \DateTimeImmutable $to, bool $strict = true)
57
    {
58
        $events = [];
59
        foreach ($this->getFlattenedEvents() as $event) {
60
            if ($event->isBetween($from, $to, $strict)) {
61
                $events[] = $event;
62
            }
63
        }
64
65
        return $events;
66
    }
67
68
    public function getNextEvent(): ?Event
69
    {
70
        if (null !== $nextEvents = $this->getUpcomingEvents()) {
71
            return reset($nextEvents);
72
        }
73
74
        return null;
75
    }
76
77
    public function getUpcomingEvents(): ?Event
78
    {
79
        if (!$this->hasEvents()) {
80
            return null;
81
        }
82
83
        $now = new \DateTimeImmutable();
84
85
        $events = [];
86
        foreach ($this->getFlattenedEvents() as $event) {
87
            if ($event->getStartAt() > $now) {
88
                $events[] = $event;
89
            }
90
        }
91
92
        return $events;
93
    }
94
95 6
    public function hasEvents(): bool
96
    {
97 6
        return 0 < count($this->events);
98
    }
99
100 7
    public function addEvent(Event $event): self
101
    {
102 7
        $this->events[] = $event;
103
104 7
        return $this;
105
    }
106
107 8
    public function getEvents(): array
108
    {
109 8
        return $this->events;
110
    }
111
}
112