Completed
Push — development ( ec29fe...c7aaa2 )
by Torben
43:55
created

RegistrationService::isWaitlistRegistration()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 4
nop 2
crap 42
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 DERHANSEN\SfEventMgt\Payment\AbstractPayment;
18
use \TYPO3\CMS\Extbase\Reflection\ObjectAccess;
19
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
20
use DERHANSEN\SfEventMgt\Utility\RegistrationResult;
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
     * Payment Service
64
     *
65
     * @var \DERHANSEN\SfEventMgt\Service\PaymentService
66
     * @inject
67
     */
68
    protected $paymentService;
69
70
    /**
71
     * Handles expired registrations. If the $delete parameter is set, then
72
     * registrations are deleted, else just hidden
73
     *
74
     * @param bool $delete Delete
75
     *
76
     * @return void
77
     */
78 2
    public function handleExpiredRegistrations($delete = false)
79
    {
80 2
        $registrations = $this->registrationRepository->findExpiredRegistrations(new \DateTime());
81 2
        if ($registrations->count() > 0) {
82 2
            foreach ($registrations as $registration) {
83
                /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */
84 2
                if ($delete) {
85 1
                    $this->registrationRepository->remove($registration);
86 1
                } else {
87 1
                    $registration->setHidden(true);
88 1
                    $this->registrationRepository->update($registration);
89
                }
90 2
            }
91 2
        }
92 2
    }
93
94
    /**
95
     * Duplicates (all public accessable properties) the given registration the
96
     * amount of times configured in amountOfRegistrations
97
     *
98
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
99
     *
100
     * @return void
101
     */
102 1
    public function createDependingRegistrations($registration)
103
    {
104 1
        $registrations = $registration->getAmountOfRegistrations();
105 1
        for ($i = 1; $i <= $registrations - 1; $i++) {
106
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $newReg */
107 1
            $newReg = $this->objectManager->get('DERHANSEN\SfEventMgt\Domain\Model\Registration');
108 1
            $properties = ObjectAccess::getGettableProperties($registration);
109 1
            foreach ($properties as $propertyName => $propertyValue) {
110 1
                ObjectAccess::setProperty($newReg, $propertyName, $propertyValue);
111 1
            }
112 1
            $newReg->setMainRegistration($registration);
113 1
            $newReg->setAmountOfRegistrations(1);
114 1
            $newReg->setIgnoreNotifications(true);
115 1
            $this->registrationRepository->add($newReg);
116 1
        }
117 1
    }
118
119
    /**
120
     * Confirms all depending registrations based on the given main registration
121
     *
122
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
123
     *
124
     * @return void
125
     */
126 1
    public function confirmDependingRegistrations($registration)
127
    {
128 1
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
129 1
        foreach ($registrations as $foundRegistration) {
130
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $foundRegistration */
131 1
            $foundRegistration->setConfirmed(true);
132 1
            $this->registrationRepository->update($foundRegistration);
133 1
        }
134 1
    }
135
136
    /**
137
     * Checks if the registration can be confirmed and returns an array of variables
138
     *
139
     * @param int $reguid UID of registration
140
     * @param string $hmac HMAC for parameters
141
     *
142
     * @return array
143
     */
144 4
    public function checkConfirmRegistration($reguid, $hmac)
145
    {
146
        /* @var $registration Registration */
147 4
        $registration = null;
148 4
        $failed = false;
149 4
        $messageKey = 'event.message.confirmation_successful';
150 4
        $titleKey = 'confirmRegistration.title.successful';
151
152 4
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
153 1
            $failed = true;
154 1
            $messageKey = 'event.message.confirmation_failed_wrong_hmac';
155 1
            $titleKey = 'confirmRegistration.title.failed';
156 1
        } else {
157 3
            $registration = $this->registrationRepository->findByUid($reguid);
158
        }
159
160 4
        if (!$failed && is_null($registration)) {
161 1
            $failed = true;
162 1
            $messageKey = 'event.message.confirmation_failed_registration_not_found';
163 1
            $titleKey = 'confirmRegistration.title.failed';
164 1
        }
165
166 4
        if (!$failed && $registration->getConfirmationUntil() < new \DateTime()) {
167 1
            $failed = true;
168 1
            $messageKey = 'event.message.confirmation_failed_confirmation_until_expired';
169 1
            $titleKey = 'confirmRegistration.title.failed';
170 1
        }
171
172 4
        if (!$failed && $registration->getConfirmed() === true) {
173 1
            $failed = true;
174 1
            $messageKey = 'event.message.confirmation_failed_already_confirmed';
175 1
            $titleKey = 'confirmRegistration.title.failed';
176 1
        }
177
178
        if (!$failed && $registration->getWaitlist()) {
179 4
            $messageKey = 'event.message.confirmation_waitlist_successful';
180 4
            $titleKey = 'confirmRegistrationWaitlist.title.successful';
181 4
        }
182
183 4
        return [
184
            $failed,
185
            $registration,
186
            $messageKey,
187
            $titleKey
188
        ];
189
    }
190
191
    /**
192
     * Cancels all depending registrations based on the given main registration
193 1
     *
194
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
195 1
     *
196 1
     * @return void
197 1
     */
198 1
    public function cancelDependingRegistrations($registration)
199 1
    {
200
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
201
        foreach ($registrations as $foundRegistration) {
202
            $this->registrationRepository->remove($foundRegistration);
203
        }
204
    }
205
206
    /**
207
     * Checks if the registration can be cancelled and returns an array of variables
208
     *
209 4
     * @param int $reguid UID of registration
210
     * @param string $hmac HMAC for parameters
211
     *
212 4
     * @return array
213 4
     */
214 4
    public function checkCancelRegistration($reguid, $hmac)
215 4
    {
216
        /* @var $registration Registration */
217 4
        $registration = null;
218 1
        $failed = false;
219 1
        $messageKey = 'event.message.cancel_successful';
220 1
        $titleKey = 'cancelRegistration.title.successful';
221 1
222 3
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
223
            $failed = true;
224
            $messageKey = 'event.message.cancel_failed_wrong_hmac';
225 4
            $titleKey = 'cancelRegistration.title.failed';
226 1
        } else {
227 1
            $registration = $this->registrationRepository->findByUid($reguid);
228 1
        }
229 1
230
        if (!$failed && is_null($registration)) {
231 4
            $failed = true;
232 1
            $messageKey = 'event.message.cancel_failed_registration_not_found_or_cancelled';
233 1
            $titleKey = 'cancelRegistration.title.failed';
234 1
        }
235 1
236
        if (!$failed && $registration->getEvent()->getEnableCancel() === false) {
237 4
            $failed = true;
238 4
            $messageKey = 'event.message.confirmation_failed_cancel_disabled';
239 4
            $titleKey = 'cancelRegistration.title.failed';
240 1
        }
241 1
242 1
        if (!$failed && $registration->getEvent()->getCancelDeadline() > 0
243 1
            && $registration->getEvent()->getCancelDeadline() < new \DateTime()
244
        ) {
245
            $failed = true;
246 4
            $messageKey = 'event.message.cancel_failed_deadline_expired';
247 4
            $titleKey = 'cancelRegistration.title.failed';
248 4
        }
249
250 4
        return [
251
            $failed,
252
            $registration,
253
            $messageKey,
254
            $titleKey
255
        ];
256
    }
257
258 4
    /**
259
     * Returns the current frontend user object if available
260 4
     *
261 1
     * @return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null
262
     */
263 3
    public function getCurrentFeUserObject()
264
    {
265
        if (isset($GLOBALS['TSFE']->fe_user->user['uid'])) {
266
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
267
        } else {
268
            return null;
269
        }
270
    }
271
272
    /**
273
     * Checks, if the registration can successfully be created. Note, that
274
     * $result is passed by reference!
275
     *
276
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
277 18
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
278
     * @param RegistrationResult $result Result
279 18
     *
280 18
     * @return bool
281 2
     */
282 2
    public function checkRegistrationSuccess($event, $registration, &$result)
283 18
    {
284 2
        $success = true;
285 2
        if ($event->getEnableRegistration() === false) {
286 16
            $success = false;
287 2
            $result = RegistrationResult::REGISTRATION_NOT_ENABLED;
288 2
        } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new \DateTime()) {
289 14
            $success = false;
290 12
            $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED;
291 12
        } elseif ($event->getStartdate() < new \DateTime()) {
292 2
            $success = false;
293 2
            $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED;
294 12
        } elseif ($event->getRegistration()->count() >= $event->getMaxParticipants()
295 10
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
296 10
        ) {
297 2
            $success = false;
298 2
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS;
299 10
        } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations()
300 2
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
301 2
        ) {
302 8
            $success = false;
303 2
            $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES;
304 6
        } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) {
305 2
            $success = false;
306 2
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED;
307 2
        } elseif ($event->getUniqueEmailCheck() &&
308 18
            $this->emailNotUnique($event, $registration->getEmail())
309
        ) {
310
            $success = false;
311
            $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE;
312
        } elseif ($event->getRegistration()->count() >= $event->getMaxParticipants()
313
            && $event->getMaxParticipants() > 0 && $event->getEnableWaitlist()
314
        ) {
315
            $result = RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST;
316
        }
317
        return $success;
318 2
    }
319
320 2
    /**
321 2
     * Returns if the given e-mail is registered to the given event
322
     *
323
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
324
     * @param string $email
325
     * @return bool
326
     */
327
    protected function emailNotUnique($event, $email)
328
    {
329
        $registrations = $this->registrationRepository->findEventRegistrationsByEmail($event, $email);
330 2
        return $registrations->count() >= 1;
331
    }
332 2
333 1
    /**
334
     * Returns, if payment redirect for the payment method is enabled
335
     *
336
     * @param Registration $registration
337 1
     * @return bool
338 1
     */
339 1
    public function redirectPaymentEnabled($registration)
340
    {
341
        if ($registration->getEvent()->getEnablePayment() === false) {
342
            return false;
343
        }
344
345
        /** @var AbstractPayment $paymentInstance */
346
        $paymentInstance = $this->paymentService->getPaymentInstance($registration->getPaymentmethod());
347
        if ($paymentInstance !== null && $paymentInstance->isRedirectEnabled()) {
348
            return true;
349
        } else {
350
            return false;
351
        }
352
    }
353
354
    /**
355
     * Returns if the given amount of registrations for the event will be registrations for the waitlist
356
     * (depending on the total amount of registrations and free places)
357
     *
358
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
359
     * @param int $amountOfRegistrations
360
     * @return bool
361
     */
362
    public function isWaitlistRegistration($event, $amountOfRegistrations)
363
    {
364
        if ($event->getMaxParticipants() === 0 || !$event->getEnableWaitlist()) {
365
            return false;
366
        }
367
368
        $result = false;
369
        if ($event->getFreePlaces() > 0 && $event->getFreePlaces() < $amountOfRegistrations) {
370
            $result = true;
371
        } elseif ($event->getFreePlaces() <= 0) {
372
            $result = true;
373
        }
374
        return $result;
375
    }
376
}
377