Completed
Push — master ( dd3139...642ef0 )
by Olivier
06:37
created

Calendar   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 105
ccs 32
cts 48
cp 0.6667
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasEvents() 0 4 1
A addEvent() 0 6 1
A getEvents() 0 4 1
B getFlattenedEvents() 0 37 6
A hasEventBetween() 0 4 1
A getEventsBetween() 0 11 3
A getNextEvent() 0 10 2
A getUpcomingEvents() 0 17 4
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
        if (!$this->hasEvents()) {
82 1
            return [];
83
        }
84
85
        $now = new \DateTimeImmutable();
86
87
        $events = [];
88
        foreach ($this->getFlattenedEvents() as $event) {
89
            if ($event->getStartAt() > $now) {
90
                $events[] = $event;
91
            }
92
        }
93
94
        return $events;
95
    }
96
97 7
    public function hasEvents(): bool
98
    {
99 7
        return 0 < count($this->events);
100
    }
101
102 7
    public function addEvent(Event $event): self
103
    {
104 7
        $this->events[] = $event;
105
106 7
        return $this;
107
    }
108
109 8
    public function getEvents(): array
110
    {
111 8
        return $this->events;
112
    }
113
}
114