Completed
Push — master ( 07b37d...b10e41 )
by Torben
02:00
created

RegistrationService::emailNotUnique()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use \TYPO3\CMS\Extbase\Reflection\ObjectAccess;
18
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
19
use DERHANSEN\SfEventMgt\Utility\RegistrationResult;
20
use DERHANSEN\SfEventMgt\Domain\Model\Event;
21
22
/**
23
 * RegistrationService
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class RegistrationService
28
{
29
30
    /**
31
     * The object manager
32
     *
33
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
34
     * @inject
35
     */
36
    protected $objectManager;
37
38
    /**
39
     * RegistrationRepository
40
     *
41
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
42
     * @inject
43
     */
44
    protected $registrationRepository;
45
46
    /**
47
     * FrontendUserRepository
48
     *
49
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
50
     * @inject
51
     */
52
    protected $frontendUserRepository;
53
54
    /**
55
     * Hash Service
56
     *
57
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
58
     * @inject
59
     */
60
    protected $hashService;
61
62
    /**
63
     * Handles expired registrations. If the $delete parameter is set, then
64
     * registrations are deleted, else just hidden
65
     *
66
     * @param bool $delete Delete
67
     *
68
     * @return void
69
     */
70 2
    public function handleExpiredRegistrations($delete = false)
71
    {
72 2
        $registrations = $this->registrationRepository->findExpiredRegistrations(new \DateTime());
73 2
        if ($registrations->count() > 0) {
74 2
            foreach ($registrations as $registration) {
75
                /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */
76 2
                if ($delete) {
77 1
                    $this->registrationRepository->remove($registration);
78 1
                } else {
79 1
                    $registration->setHidden(true);
80 1
                    $this->registrationRepository->update($registration);
81
                }
82 2
            }
83 2
        }
84 2
    }
85
86
    /**
87
     * Duplicates (all public accessable properties) the given registration the
88
     * amount of times configured in amountOfRegistrations
89
     *
90
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
91
     *
92
     * @return void
93
     */
94 1
    public function createDependingRegistrations($registration)
95
    {
96 1
        $registrations = $registration->getAmountOfRegistrations();
97 1
        for ($i = 1; $i <= $registrations - 1; $i++) {
98
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $newReg */
99 1
            $newReg = $this->objectManager->get('DERHANSEN\SfEventMgt\Domain\Model\Registration');
100 1
            $properties = ObjectAccess::getGettableProperties($registration);
101 1
            foreach ($properties as $propertyName => $propertyValue) {
102 1
                ObjectAccess::setProperty($newReg, $propertyName, $propertyValue);
103 1
            }
104 1
            $newReg->setMainRegistration($registration);
105 1
            $newReg->setAmountOfRegistrations(1);
106 1
            $newReg->setIgnoreNotifications(true);
107 1
            $this->registrationRepository->add($newReg);
108 1
        }
109 1
    }
110
111
    /**
112
     * Confirms all depending registrations based on the given main registration
113
     *
114
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
115
     *
116
     * @return void
117
     */
118 1
    public function confirmDependingRegistrations($registration)
119
    {
120 1
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
121 1
        foreach ($registrations as $foundRegistration) {
122
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $foundRegistration */
123 1
            $foundRegistration->setConfirmed(true);
124 1
            $this->registrationRepository->update($foundRegistration);
125 1
        }
126 1
    }
127
128
    /**
129
     * Checks if the registration can be confirmed and returns an array of variables
130
     *
131
     * @param int $reguid UID of registration
132
     * @param string $hmac HMAC for parameters
133
     *
134
     * @return array
135
     */
136 4
    public function checkConfirmRegistration($reguid, $hmac)
137
    {
138
        /* @var $registration Registration */
139 4
        $registration = null;
140 4
        $failed = false;
141 4
        $messageKey = 'event.message.confirmation_successful';
142 4
        $titleKey = 'confirmRegistration.title.successful';
143
144 4
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
145 1
            $failed = true;
146 1
            $messageKey = 'event.message.confirmation_failed_wrong_hmac';
147 1
            $titleKey = 'confirmRegistration.title.failed';
148 1
        } else {
149 3
            $registration = $this->registrationRepository->findByUid($reguid);
150
        }
151
152 4
        if (!$failed && is_null($registration)) {
153 1
            $failed = true;
154 1
            $messageKey = 'event.message.confirmation_failed_registration_not_found';
155 1
            $titleKey = 'confirmRegistration.title.failed';
156 1
        }
157
158 4
        if (!$failed && $registration->getConfirmationUntil() < new \DateTime()) {
159 1
            $failed = true;
160 1
            $messageKey = 'event.message.confirmation_failed_confirmation_until_expired';
161 1
            $titleKey = 'confirmRegistration.title.failed';
162 1
        }
163
164 4
        if (!$failed && $registration->getConfirmed() === true) {
165 1
            $failed = true;
166 1
            $messageKey = 'event.message.confirmation_failed_already_confirmed';
167 1
            $titleKey = 'confirmRegistration.title.failed';
168 1
        }
169
170
        return [
171 4
            $failed,
172 4
            $registration,
173 4
            $messageKey,
174
            $titleKey
175 4
        ];
176
    }
177
178
    /**
179
     * Cancels all depending registrations based on the given main registration
180
     *
181
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
182
     *
183
     * @return void
184
     */
185 1
    public function cancelDependingRegistrations($registration)
186
    {
187 1
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
188 1
        foreach ($registrations as $foundRegistration) {
189 1
            $this->registrationRepository->remove($foundRegistration);
190 1
        }
191 1
    }
192
193
    /**
194
     * Checks if the registration can be cancelled and returns an array of variables
195
     *
196
     * @param int $reguid UID of registration
197
     * @param string $hmac HMAC for parameters
198
     *
199
     * @return array
200
     */
201 4
    public function checkCancelRegistration($reguid, $hmac)
202
    {
203
        /* @var $registration Registration */
204 4
        $registration = null;
205 4
        $failed = false;
206 4
        $messageKey = 'event.message.cancel_successful';
207 4
        $titleKey = 'cancelRegistration.title.successful';
208
209 4
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
210 1
            $failed = true;
211 1
            $messageKey = 'event.message.cancel_failed_wrong_hmac';
212 1
            $titleKey = 'cancelRegistration.title.failed';
213 1
        } else {
214 3
            $registration = $this->registrationRepository->findByUid($reguid);
215
        }
216
217 4
        if (!$failed && is_null($registration)) {
218 1
            $failed = true;
219 1
            $messageKey = 'event.message.cancel_failed_registration_not_found_or_cancelled';
220 1
            $titleKey = 'cancelRegistration.title.failed';
221 1
        }
222
223 4
        if (!$failed && $registration->getEvent()->getEnableCancel() === false) {
224 1
            $failed = true;
225 1
            $messageKey = 'event.message.confirmation_failed_cancel_disabled';
226 1
            $titleKey = 'cancelRegistration.title.failed';
227 1
        }
228
229 4
        if (!$failed && $registration->getEvent()->getCancelDeadline() > 0
230 4
            && $registration->getEvent()->getCancelDeadline() < new \DateTime()
231 4
        ) {
232 1
            $failed = true;
233 1
            $messageKey = 'event.message.cancel_failed_deadline_expired';
234 1
            $titleKey = 'cancelRegistration.title.failed';
235 1
        }
236
237
        return [
238 4
            $failed,
239 4
            $registration,
240 4
            $messageKey,
241
            $titleKey
242 4
        ];
243
    }
244
245
    /**
246
     * Returns the current frontend user object if available
247
     *
248
     * @return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null
249
     */
250 4
    public function getCurrentFeUserObject()
251
    {
252 4
        if (isset($GLOBALS['TSFE']->fe_user->user['uid'])) {
253 1
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
254
        } else {
255 3
            return null;
256
        }
257
    }
258
259
    /**
260
     * Checks, if the registration can successfully be created. Note, that
261
     * $result is passed by reference!
262
     *
263
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
264
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
265
     * @param RegistrationResult $result Result
266
     *
267
     * @return bool
268
     */
269 18
    public function checkRegistrationSuccess($event, $registration, &$result)
270
    {
271 18
        $success = true;
272 18
        if ($event->getEnableRegistration() === false) {
273 2
            $success = false;
274 2
            $result = RegistrationResult::REGISTRATION_NOT_ENABLED;
275 18
        } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new \DateTime()) {
276 2
            $success = false;
277 2
            $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED;
278 16
        } elseif ($event->getStartdate() < new \DateTime()) {
279 2
            $success = false;
280 2
            $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED;
281 14
        } elseif ($event->getRegistration()->count() >= $event->getMaxParticipants()
282 12
            && $event->getMaxParticipants() > 0
283 12
        ) {
284 2
            $success = false;
285 2
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS;
286 12
        } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations()
287 10
            && $event->getMaxParticipants() > 0
288 10
        ) {
289 2
            $success = false;
290 2
            $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES;
291 10
        } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) {
292 2
            $success = false;
293 2
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED;
294 8
        } elseif ($event->getUniqueEmailCheck() &&
295 2
            $this->emailNotUnique($event, $registration->getEmail())
296 6
        ) {
297 2
            $success = false;
298 2
            $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE;
299 2
        }
300 18
        return $success;
301
    }
302
303
    /**
304
     * Returns if the given e-mail is registered to the given event
305
     *
306
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
307
     * @param string $email
308
     * @return bool
309
     */
310 2
    protected function emailNotUnique($event, $email)
311
    {
312 2
        $registrations = $this->registrationRepository->findEventRegistrationsByEmail($event, $email);
313 2
        return $registrations->count() >= 1;
314
    }
315
}