Completed
Push — master ( 50fde4...1991d6 )
by Torben
61:08 queued 58:12
created

RegistrationService   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 319
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 99.32%

Importance

Changes 11
Bugs 1 Features 6
Metric Value
wmc 47
c 11
b 1
f 6
lcom 3
cbo 5
dl 0
loc 319
ccs 145
cts 146
cp 0.9932
rs 8.439

10 Methods

Rating   Name   Duplication   Size   Complexity  
A handleExpiredRegistrations() 0 15 4
A createDependingRegistrations() 0 16 3
A confirmDependingRegistrations() 0 9 2
C checkConfirmRegistration() 0 41 8
A cancelDependingRegistrations() 0 7 2
D checkCancelRegistration() 0 43 9
A getCurrentFeUserObject() 0 8 2
C checkRegistrationSuccess() 0 33 12
A emailNotUnique() 0 5 1
A redirectPaymentEnabled() 0 14 4

How to fix   Complexity   

Complex Class

Complex classes like RegistrationService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RegistrationService, and based on these observations, apply Extract Interface, too.

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