Completed
Push — master ( 9c9098...7e66ea )
by Olivier
02:58
created

Calendar::addEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 4
    public function hasEvents(): bool
79
    {
80 4
        return 0 < count($this->events);
81
    }
82
83 3
    public function addEvent(Event $event): self
84
    {
85 3
        $this->events[] = $event;
86
87 3
        return $this;
88
    }
89
90 4
    public function getEvents(): array
91
    {
92 4
        return $this->events;
93
    }
94
}
95