Completed
Push — master ( ec29fe...2e5711 )
by Torben
09:02 queued 07:11
created

RegistrationService::checkConfirmRegistration()   D

Complexity

Conditions 10
Paths 32

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10.0686

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 31
cts 34
cp 0.9118
rs 4.983
c 0
b 0
f 0
cc 10
eloc 31
nc 32
nop 2
crap 10.0686

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 4
        if (!$failed && $registration->getWaitlist()) {
179
            $messageKey = 'event.message.confirmation_waitlist_successful';
180
            $titleKey = 'confirmRegistrationWaitlist.title.successful';
181
        }
182
183
        return [
184 4
            $failed,
185 4
            $registration,
186 4
            $messageKey,
187
            $titleKey
188 4
        ];
189
    }
190
191
    /**
192
     * Cancels all depending registrations based on the given main registration
193
     *
194
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
195
     *
196
     * @return void
197
     */
198 1
    public function cancelDependingRegistrations($registration)
199
    {
200 1
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
201 1
        foreach ($registrations as $foundRegistration) {
202 1
            $this->registrationRepository->remove($foundRegistration);
203 1
        }
204 1
    }
205
206
    /**
207
     * Checks if the registration can be cancelled and returns an array of variables
208
     *
209
     * @param int $reguid UID of registration
210
     * @param string $hmac HMAC for parameters
211
     *
212
     * @return array
213
     */
214 4
    public function checkCancelRegistration($reguid, $hmac)
215
    {
216
        /* @var $registration Registration */
217 4
        $registration = null;
218 4
        $failed = false;
219 4
        $messageKey = 'event.message.cancel_successful';
220 4
        $titleKey = 'cancelRegistration.title.successful';
221
222 4
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
223 1
            $failed = true;
224 1
            $messageKey = 'event.message.cancel_failed_wrong_hmac';
225 1
            $titleKey = 'cancelRegistration.title.failed';
226 1
        } else {
227 3
            $registration = $this->registrationRepository->findByUid($reguid);
228
        }
229
230 4
        if (!$failed && is_null($registration)) {
231 1
            $failed = true;
232 1
            $messageKey = 'event.message.cancel_failed_registration_not_found_or_cancelled';
233 1
            $titleKey = 'cancelRegistration.title.failed';
234 1
        }
235
236 4
        if (!$failed && $registration->getEvent()->getEnableCancel() === false) {
237 1
            $failed = true;
238 1
            $messageKey = 'event.message.confirmation_failed_cancel_disabled';
239 1
            $titleKey = 'cancelRegistration.title.failed';
240 1
        }
241
242 4
        if (!$failed && $registration->getEvent()->getCancelDeadline() > 0
243 4
            && $registration->getEvent()->getCancelDeadline() < new \DateTime()
244 4
        ) {
245 1
            $failed = true;
246 1
            $messageKey = 'event.message.cancel_failed_deadline_expired';
247 1
            $titleKey = 'cancelRegistration.title.failed';
248 1
        }
249
250
        return [
251 4
            $failed,
252 4
            $registration,
253 4
            $messageKey,
254
            $titleKey
255 4
        ];
256
    }
257
258
    /**
259
     * Returns the current frontend user object if available
260
     *
261
     * @return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null
262
     */
263 5
    public function getCurrentFeUserObject()
264
    {
265 5
        if (isset($GLOBALS['TSFE']->fe_user->user['uid'])) {
266 1
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
267
        } else {
268 4
            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
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
278
     * @param RegistrationResult $result Result
279
     *
280
     * @return bool
281
     */
282 19
    public function checkRegistrationSuccess($event, $registration, &$result)
283
    {
284 19
        $success = true;
285 19
        if ($event->getEnableRegistration() === false) {
286 2
            $success = false;
287 2
            $result = RegistrationResult::REGISTRATION_NOT_ENABLED;
288 19
        } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new \DateTime()) {
289 2
            $success = false;
290 2
            $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED;
291 17
        } elseif ($event->getStartdate() < new \DateTime()) {
292 2
            $success = false;
293 2
            $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED;
294 15
        } elseif ($event->getRegistration()->count() >= $event->getMaxParticipants()
295 13
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
296 13
        ) {
297 2
            $success = false;
298 2
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS;
299 13
        } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations()
300 11
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
301 11
        ) {
302 2
            $success = false;
303 2
            $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES;
304 11
        } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) {
305 2
            $success = false;
306 2
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED;
307 9
        } elseif ($event->getUniqueEmailCheck() &&
308 2
            $this->emailNotUnique($event, $registration->getEmail())
309 7
        ) {
310 2
            $success = false;
311 2
            $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE;
312 7
        } elseif ($event->getRegistration()->count() >= $event->getMaxParticipants()
313 5
            && $event->getMaxParticipants() > 0 && $event->getEnableWaitlist()
314 5
        ) {
315 1
            $result = RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST;
316 1
        }
317 19
        return $success;
318
    }
319
320
    /**
321
     * 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 2
    protected function emailNotUnique($event, $email)
328
    {
329 2
        $registrations = $this->registrationRepository->findEventRegistrationsByEmail($event, $email);
330 2
        return $registrations->count() >= 1;
331
    }
332
333
    /**
334
     * Returns, if payment redirect for the payment method is enabled
335
     *
336
     * @param Registration $registration
337
     * @return bool
338
     */
339 2
    public function redirectPaymentEnabled($registration)
340
    {
341 2
        if ($registration->getEvent()->getEnablePayment() === false) {
342 1
            return false;
343
        }
344
345
        /** @var AbstractPayment $paymentInstance */
346 1
        $paymentInstance = $this->paymentService->getPaymentInstance($registration->getPaymentmethod());
347 1
        if ($paymentInstance !== null && $paymentInstance->isRedirectEnabled()) {
348 1
            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 7
    public function isWaitlistRegistration($event, $amountOfRegistrations)
363
    {
364 7
        if ($event->getMaxParticipants() === 0 || !$event->getEnableWaitlist()) {
365 3
            return false;
366
        }
367
368 4
        $result = false;
369 4
        if ($event->getFreePlaces() > 0 && $event->getFreePlaces() < $amountOfRegistrations) {
370 1
            $result = true;
371 4
        } elseif ($event->getFreePlaces() <= 0) {
372 2
            $result = true;
373 2
        }
374 4
        return $result;
375
    }
376
}
377