Completed
Push — master ( 6f4e0b...12f334 )
by Florian
19s queued 16s
created

Event::getAttendances()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
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
        return $this->placesLeft() > 0 && $this->isRegistrationOpen();
180
    }
181
182
    public function placesLeft(): int
183
    {
184
        $participantRegistrationCount = 0;
185
        foreach ($this->registrations as $registration) {
186
            if (!$registration->getIsOrganizer()) {
187
                ++$participantRegistrationCount;
188
            }
189
        }
190
191
        return max($this->maximumAttendeeCapacity - $participantRegistrationCount, 0);
192
    }
193
194
    public function getClosedDate(): ?\DateTime
195
    {
196
        return $this->closedDate;
197
    }
198
199
    public function close()
200
    {
201
        $this->closedDate = new \DateTime();
202
    }
203
204
    /**
205
     * @return Registration[]|ArrayCollection
206
     */
207
    public function getRegistrations()
208
    {
209
        return $this->registrations;
210
    }
211
212
    /**
213
     * @return Registration[]
214
     */
215
    public function getOrganizerRegistrations(): array
216
    {
217
        $organizerRegistrations = [];
218
219
        foreach ($this->registrations as $registration) {
220
            if ($registration->getIsOrganizer()) {
221
                $organizerRegistrations[] = $registration;
222
            }
223
        }
224
225
        return $organizerRegistrations;
226
    }
227
228
    /**
229
     * @return Registration[]
230
     */
231
    public function getParticipantRegistrations(): array
232
    {
233
        $participantRegistrations = [];
234
235
        foreach ($this->registrations as $registration) {
236
            if (!$registration->getIsOrganizer()) {
237
                $participantRegistrations[] = $registration;
238
            }
239
        }
240
241
        return $participantRegistrations;
242
    }
243
244
    /**
245
     * @return Attendance[]|ArrayCollection
246
     */
247
    public function getAttendances()
248
    {
249
        return $this->attendances;
250
    }
251
}
252