Passed
Push — master ( ece3c1...a0e7b2 )
by Florian
02:27
created

Event::isRegistrationPossible()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 3
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
     */
84
    private $registrations;
85
86
    /**
87
     * @var Participation[]|ArrayCollection
88
     *
89
     * @ORM\OneToMany(targetEntity="App\Entity\Participation", mappedBy="event")
90
     */
91
    private $participations;
92
93
    public function __construct()
94
    {
95
        $this->registrations = new ArrayCollection();
96
        $this->participations = new ArrayCollection();
97
    }
98
99
    public function setIdentifiers(string $identifier, string $organizerSecret)
100
    {
101
        $this->identifier = $identifier;
102
        $this->organizerSecret = $organizerSecret;
103
    }
104
105
    public function getIdentifier(): string
106
    {
107
        return $this->identifier;
108
    }
109
110
    public function getOrganizerSecret(): string
111
    {
112
        return $this->organizerSecret;
113
    }
114
115
    public function getMaximumAttendeeCapacity(): ?int
116
    {
117
        return $this->maximumAttendeeCapacity;
118
    }
119
120
    public function setMaximumAttendeeCapacity(?int $maximumAttendeeCapacity): void
121
    {
122
        $this->maximumAttendeeCapacity = $maximumAttendeeCapacity;
123
    }
124
125
    public function getRegistrationOpen(): ?\DateTime
126
    {
127
        return $this->registrationOpen;
128
    }
129
130
    public function setRegistrationOpen(?\DateTime $registrationOpen): void
131
    {
132
        $this->registrationOpen = $registrationOpen;
133
    }
134
135
    public function getRegistrationClose(): ?\DateTime
136
    {
137
        return $this->registrationClose;
138
    }
139
140
    public function setRegistrationClose(?\DateTime $registrationClose): void
141
    {
142
        $this->registrationClose = $registrationClose;
143
    }
144
145
    public function isRegistrationOpen(): bool
146
    {
147
        if ($this->closedDate) {
148
            return false;
149
        }
150
151
        $now = new \DateTime();
152
        if (null !== $this->registrationOpen && $this->registrationOpen > $now) {
153
            return false;
154
        }
155
156
        if (null !== $this->registrationClose && $this->registrationClose < $now) {
157
            return false;
158
        }
159
160
        return true;
161
    }
162
163
    public function isRegistrationPossible(): bool
164
    {
165
        if (null !== $this->maximumAttendeeCapacity && count($this->registrations) >= $this->maximumAttendeeCapacity) {
166
            return false;
167
        }
168
169
        return $this->isRegistrationOpen();
170
    }
171
172
    public function getClosedDate(): ?\DateTime
173
    {
174
        return $this->closedDate;
175
    }
176
177
    public function close()
178
    {
179
        $this->closedDate = new \DateTime();
180
    }
181
182
    /**
183
     * @return Registration[]|ArrayCollection
184
     */
185
    public function getRegistrations()
186
    {
187
        return $this->registrations;
188
    }
189
}
190