Calendar::getNextEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.032
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 1
    public function getNextEvent(): ?Event
69
    {
70 1
        $nextEvents = $this->getUpcomingEvents();
71
72 1
        if (0 === count($nextEvents)) {
73 1
            return null;
74
        }
75
76
        return reset($nextEvents);
77
    }
78
79 1
    public function getUpcomingEvents(): array
80
    {
81 1
        return $this->getEventsAfter(new \DateTimeImmutable());
82
    }
83
84 1
    public function getEventsAfter(\DateTimeInterface $date): array
85
    {
86 1
        if (!$this->hasEvents()) {
87 1
            return [];
88
        }
89
90
        $events = [];
91
        foreach ($this->getFlattenedEvents() as $event) {
92
            if ($event->getStartAt() > $date) {
93
                $events[] = $event;
94
            }
95
        }
96
97
        return $events;
98
    }
99
100 7
    public function hasEvents(): bool
101
    {
102 7
        return 0 < count($this->events);
103
    }
104
105 7
    public function addEvent(Event $event): self
106
    {
107 7
        $this->events[] = $event;
108
109 7
        return $this;
110
    }
111
112 8
    public function getEvents(): array
113
    {
114 8
        return $this->events;
115
    }
116
}
117