Passed
Push — master ( d6e87c...6b1b2b )
by Florian
02:41
created

Event::isAfterRegistrationPeriod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the TheAlternativeZurich/events project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Entity;
13
14
use App\Entity\Base\BaseEntity;
15
use App\Entity\Traits\EventTrait;
16
use App\Entity\Traits\IdTrait;
17
use App\Entity\Traits\TimeTrait;
18
use Doctrine\Common\Collections\ArrayCollection;
19
use Doctrine\ORM\Mapping as ORM;
20
21
/**
22
 * @ORM\Entity(repositoryClass="App\Repository\EventRepository")
23
 * @ORM\HasLifecycleCallbacks
24
 */
25
class Event extends BaseEntity
26
{
27
    use IdTrait;
28
    use TimeTrait;
29
    use EventTrait;
30
31
    /**
32
     * @var string
33
     *
34
     * @ORM\Column(type="string", length=255, unique=true)
35
     */
36
    private $identifier;
37
38
    /**
39
     * @var string
40
     *
41
     * @ORM\Column(type="string")
42
     */
43
    private $organizerSecret;
44
45
    /**
46
     * @var int|null
47
     *
48
     * @ORM\Column(type="integer", nullable=true)
49
     */
50
    private $maximumAttendeeCapacity;
51
52
    /**
53
     * when the registration starts being accessible.
54
     *
55
     * @var \DateTime|null
56
     *
57
     * @ORM\Column(type="datetime", nullable=true)
58
     */
59
    private $registrationOpen;
60
61
    /**
62
     * when the registration is no longer possible.
63
     *
64
     * @var \DateTime|null
65
     *
66
     * @ORM\Column(type="datetime", nullable=true)
67
     */
68
    private $registrationClose;
69
70
    /**
71
     * when the event was finished, hence all open participations are closed.
72
     *
73
     * @var \DateTime|null
74
     *
75
     * @ORM\Column(type="datetime", nullable=true)
76
     */
77
    private $closedDate;
78
79
    /**
80
     * @var Registration[]|ArrayCollection
81
     *
82
     * @ORM\OneToMany(targetEntity="App\Entity\Registration", mappedBy="event")
83
     * @ORM\OrderBy({"createdAt" = "ASC"})
84
     */
85
    private $registrations;
86
87
    /**
88
     * @var Participation[]|ArrayCollection
89
     *
90
     * @ORM\OneToMany(targetEntity="App\Entity\Participation", mappedBy="event")
91
     */
92
    private $participations;
93
94
    public function __construct()
95
    {
96
        $this->registrations = new ArrayCollection();
97
        $this->participations = new ArrayCollection();
98
    }
99
100
    public function setIdentifiers(string $identifier, string $organizerSecret)
101
    {
102
        $this->identifier = $identifier;
103
        $this->organizerSecret = $organizerSecret;
104
    }
105
106
    public function getIdentifier(): string
107
    {
108
        return $this->identifier;
109
    }
110
111
    public function getOrganizerSecret(): string
112
    {
113
        return $this->organizerSecret;
114
    }
115
116
    public function getMaximumAttendeeCapacity(): ?int
117
    {
118
        return $this->maximumAttendeeCapacity;
119
    }
120
121
    public function setMaximumAttendeeCapacity(?int $maximumAttendeeCapacity): void
122
    {
123
        $this->maximumAttendeeCapacity = $maximumAttendeeCapacity;
124
    }
125
126
    public function getRegistrationOpen(): ?\DateTime
127
    {
128
        return $this->registrationOpen;
129
    }
130
131
    public function setRegistrationOpen(?\DateTime $registrationOpen): void
132
    {
133
        $this->registrationOpen = $registrationOpen;
134
    }
135
136
    public function getRegistrationClose(): ?\DateTime
137
    {
138
        return $this->registrationClose;
139
    }
140
141
    public function setRegistrationClose(?\DateTime $registrationClose): void
142
    {
143
        $this->registrationClose = $registrationClose;
144
    }
145
146
    public function isRegistrationOpen(): bool
147
    {
148
        if ($this->closedDate) {
149
            return false;
150
        }
151
152
        if ($this->isBeforeRegistrationPeriod()) {
153
            return false;
154
        }
155
        if ($this->isAfterRegistrationPeriod()) {
156
            return false;
157
        }
158
159
        return true;
160
    }
161
162
    public function isBeforeRegistrationPeriod()
163
    {
164
        $now = new \DateTime();
165
166
        return null !== $this->registrationOpen && $this->registrationOpen > $now;
167
    }
168
169
    public function isAfterRegistrationPeriod()
170
    {
171
        $now = new \DateTime();
172
173
        return null !== $this->registrationClose && $this->registrationClose < $now;
174
    }
175
176
    public function isRegistrationPossible(): bool
177
    {
178
        if (null !== $this->maximumAttendeeCapacity && count($this->registrations) >= $this->maximumAttendeeCapacity) {
179
            return false;
180
        }
181
182
        return $this->isRegistrationOpen();
183
    }
184
185
    public function getClosedDate(): ?\DateTime
186
    {
187
        return $this->closedDate;
188
    }
189
190
    public function close()
191
    {
192
        $this->closedDate = new \DateTime();
193
    }
194
195
    /**
196
     * @return Registration[]|ArrayCollection
197
     */
198
    public function getRegistrations()
199
    {
200
        return $this->registrations;
201
    }
202
203
    /**
204
     * @return Registration[]
205
     */
206
    public function getOrganizerRegistrations(): array
207
    {
208
        $organizerRegistrations = [];
209
210
        foreach ($this->registrations as $registration) {
211
            if ($registration->getIsOrganizer()) {
212
                $organizerRegistrations[] = $registration;
213
            }
214
        }
215
216
        return $organizerRegistrations;
217
    }
218
219
    /**
220
     * @return Registration[]
221
     */
222
    public function getParticipantRegistrations(): array
223
    {
224
        $participantRegistrations = [];
225
226
        foreach ($this->registrations as $registration) {
227
            if (!$registration->getIsOrganizer()) {
228
                $participantRegistrations[] = $registration;
229
            }
230
        }
231
232
        return $participantRegistrations;
233
    }
234
}
235