Completed
Pull Request — master (#478)
by Luc
02:08
created

SubEvents::hasSubEventWithTimestamp()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CultuurNet\UDB3\Event\ValueObjects;
6
7
use CultuurNet\UDB3\Calendar;
8
use CultuurNet\UDB3\Timestamp;
9
10
final class SubEvents
11
{
12
    /**
13
     * @var SubEvent[]
14
     */
15
    private $subEvents;
16
17
    private function __construct(SubEvent ...$subEvents)
18
    {
19
        $this->subEvents = $subEvents;
20
    }
21
22
    public static function createEmpty(): SubEvents
23
    {
24
        return new SubEvents(...[]);
25
    }
26
27
    public static function createFromCalendar(Calendar $calendar): SubEvents
28
    {
29
        if (empty($calendar->getTimestamps())) {
30
            return self::createEmpty();
31
        }
32
33
        $subEvents = [];
34
        // TODO III-3570: Take into account a calendar with the same timestamps.
35
        foreach ($calendar->getTimestamps() as $timestamp) {
36
            $subEvents[] = new SubEvent($timestamp, Status::scheduled());
37
        }
38
39
        return new SubEvents(...$subEvents);
40
    }
41
42
    public function getSubEvents(): array
43
    {
44
        return $this->subEvents;
45
    }
46
47
    public function hasSubEvent(SubEvent $subEventToSearch): bool
48
    {
49
        foreach ($this->subEvents as $subEvent) {
50
            if ($subEvent->equals($subEventToSearch)) {
51
                return true;
52
            }
53
        }
54
55
        return false;
56
    }
57
58
    public function hasSubEventWithTimestamp(Timestamp $timestampToSearch): bool
59
    {
60
        foreach ($this->subEvents as $subEvent) {
61
            if ($subEvent->getTimestamp()->equals($timestampToSearch)) {
62
                return true;
63
            }
64
        }
65
66
        return false;
67
    }
68
69 View Code Duplication
    public function addSubEvent(SubEvent $subEvent): SubEvents
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        if ($this->hasSubEventWithTimestamp($subEvent->getTimestamp())) {
72
            return $this;
73
        }
74
75
        $this->subEvents[] = $subEvent;
76
77
        return $this;
78
    }
79
80 View Code Duplication
    public function updateSubEvent(SubEvent $subEvent): SubEvents
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        if (!$this->hasSubEventWithTimestamp($subEvent->getTimestamp())) {
83
            return $this;
84
        }
85
86
        $this->removeSubEventWithTimestamp($subEvent->getTimestamp());
87
        $this->subEvents[] = $subEvent;
88
89
        return $this;
90
    }
91
92
    private function removeSubEventWithTimestamp(Timestamp $timestamp): SubEvents
93
    {
94
        $nrOfSubEvents = count($this->subEvents);
95
        for ($index = 0; $index < $nrOfSubEvents; $index++) {
96
            if ($this->subEvents[$index]->getTimestamp()->equals($timestamp)) {
97
                unset($this->subEvents[$index]);
98
            }
99
        }
100
101
        $this->subEvents = \array_values($this->subEvents);
102
103
        return $this;
104
    }
105
}
106