Event::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
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 Attendance[]|ArrayCollection
89
     *
90
     * @ORM\OneToMany(targetEntity="Attendance", mappedBy="event")
91
     * @ORM\OrderBy({"createdAt" = "ASC"})
92
     */
93
    private $attendances;
94
95
    public function __construct()
96
    {
97
        $this->registrations = new ArrayCollection();
98
        $this->attendances = new ArrayCollection();
99
    }
100
101
    public function setIdentifiers(string $identifier, string $organizerSecret)
102
    {
103
        $this->identifier = $identifier;
104
        $this->organizerSecret = $organizerSecret;
105
    }
106
107
    public function getIdentifier(): string
108
    {
109
        return $this->identifier;
110
    }
111
112
    public function getOrganizerSecret(): string
113
    {
114
        return $this->organizerSecret;
115
    }
116
117
    public function getMaximumAttendeeCapacity(): ?int
118
    {
119
        return $this->maximumAttendeeCapacity;
120
    }
121
122
    public function setMaximumAttendeeCapacity(?int $maximumAttendeeCapacity): void
123
    {
124
        $this->maximumAttendeeCapacity = $maximumAttendeeCapacity;
125
    }
126
127
    public function getRegistrationOpen(): ?\DateTime
128
    {
129
        return $this->registrationOpen;
130
    }
131
132
    public function setRegistrationOpen(?\DateTime $registrationOpen): void
133
    {
134
        $this->registrationOpen = $registrationOpen;
135
    }
136
137
    public function getRegistrationClose(): ?\DateTime
138
    {
139
        return $this->registrationClose;
140
    }
141
142
    public function setRegistrationClose(?\DateTime $registrationClose): void
143
    {
144
        $this->registrationClose = $registrationClose;
145
    }
146
147
    public function isRegistrationOpen(): bool
148
    {
149
        if ($this->closedDate) {
150
            return false;
151
        }
152
153
        if ($this->isBeforeRegistrationPeriod()) {
154
            return false;
155
        }
156
        if ($this->isAfterRegistrationPeriod()) {
157
            return false;
158
        }
159
160
        return true;
161
    }
162
163
    public function isBeforeRegistrationPeriod()
164
    {
165
        $now = new \DateTime();
166
167
        return null !== $this->registrationOpen && $this->registrationOpen > $now;
168
    }
169
170
    public function isAfterRegistrationPeriod()
171
    {
172
        $now = new \DateTime();
173
174
        return null !== $this->registrationClose && $this->registrationClose < $now;
175
    }
176
177
    public function isRegistrationPossible(): bool
178
    {
179
        $placesLeft = $this->placesLeft();
180
181
        return (null === $placesLeft || $placesLeft > 0) && $this->isRegistrationOpen();
182
    }
183
184
    public function placesLeft(): ?int
185
    {
186
        if (null === $this->maximumAttendeeCapacity) {
187
            return null;
188
        }
189
190
        $participantRegistrationCount = 0;
191
        foreach ($this->registrations as $registration) {
192
            if (!$registration->getIsOrganizer()) {
193
                ++$participantRegistrationCount;
194
            }
195
        }
196
197
        return max($this->maximumAttendeeCapacity - $participantRegistrationCount, 0);
198
    }
199
200
    public function getClosedDate(): ?\DateTime
201
    {
202
        return $this->closedDate;
203
    }
204
205
    public function close()
206
    {
207
        $this->closedDate = new \DateTime();
208
    }
209
210
    /**
211
     * @return Registration[]|ArrayCollection
212
     */
213
    public function getRegistrations()
214
    {
215
        return $this->registrations;
216
    }
217
218
    /**
219
     * @return Registration[]
220
     */
221
    public function getOrganizerRegistrations(): array
222
    {
223
        $organizerRegistrations = [];
224
225
        foreach ($this->registrations as $registration) {
226
            if ($registration->getIsOrganizer()) {
227
                $organizerRegistrations[] = $registration;
228
            }
229
        }
230
231
        return $organizerRegistrations;
232
    }
233
234
    /**
235
     * @return Registration[]
236
     */
237
    public function getParticipantRegistrations(): array
238
    {
239
        $participantRegistrations = [];
240
241
        foreach ($this->registrations as $registration) {
242
            if (!$registration->getIsOrganizer()) {
243
                $participantRegistrations[] = $registration;
244
            }
245
        }
246
247
        return $participantRegistrations;
248
    }
249
250
    /**
251
     * @return Attendance[]|ArrayCollection
252
     */
253
    public function getAttendances()
254
    {
255
        return $this->attendances;
256
    }
257
}
258