Completed
Push — master ( 19eae0...af0e0d )
by Florian
13s queued 11s
created

Event::placesLeft()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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