Completed
Push — development ( 6b83c1...4f1043 )
by Torben
11:42
created

confirmDependingRegistrations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
12
use DERHANSEN\SfEventMgt\Payment\AbstractPayment;
13
use DERHANSEN\SfEventMgt\Utility\RegistrationResult;
14
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
15
16
/**
17
 * RegistrationService
18
 *
19
 * @author Torben Hansen <[email protected]>
20
 */
21
class RegistrationService
22
{
23
    /**
24
     * The object manager
25
     *
26
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
27
     * */
28
    protected $objectManager;
29
30
    /**
31
     * RegistrationRepository
32
     *
33
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
34
     * */
35
    protected $registrationRepository;
36
37
    /**
38
     * FrontendUserRepository
39
     *
40
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
41
     * */
42
    protected $frontendUserRepository;
43
44
    /**
45
     * Hash Service
46
     *
47
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
48
     * */
49
    protected $hashService;
50
51
    /**
52
     * Payment Service
53
     *
54
     * @var \DERHANSEN\SfEventMgt\Service\PaymentService
55
     * */
56
    protected $paymentService;
57
58
    /**
59
     * DI for $frontendUserRepository
60
     *
61
     * @param \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository
62
     */
63
    public function injectFrontendUserRepository(
64
        \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository
65
    ) {
66
        $this->frontendUserRepository = $frontendUserRepository;
67
    }
68
69
    /**
70
     * DI for $hashService
71
     *
72
     * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
73
     */
74
    public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
75
    {
76
        $this->hashService = $hashService;
77
    }
78 2
79
    /**
80 2
     * DI for $objectManager
81 2
     *
82 2
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
83
     */
84 2
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
85 1
    {
86 1
        $this->objectManager = $objectManager;
87 1
    }
88 1
89
    /**
90 2
     * DI for $paymentService
91 2
     *
92 2
     * @param \DERHANSEN\SfEventMgt\Service\PaymentService $paymentService
93
     */
94
    public function injectPaymentService(\DERHANSEN\SfEventMgt\Service\PaymentService $paymentService)
95
    {
96
        $this->paymentService = $paymentService;
97
    }
98
99
    /**
100
     * DI for $registrationRepository
101
     *
102 1
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
103
     */
104 1
    public function injectRegistrationRepository(
105 1
        \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
106
    ) {
107 1
        $this->registrationRepository = $registrationRepository;
108 1
    }
109 1
110 1
    /**
111 1
     * Handles expired registrations. If the $delete parameter is set, then
112 1
     * registrations are deleted, else just hidden
113 1
     *
114 1
     * @param bool $delete Delete
115 1
     *
116 1
     * @return void
117 1
     */
118
    public function handleExpiredRegistrations($delete = false)
119
    {
120
        $registrations = $this->registrationRepository->findExpiredRegistrations(new \DateTime());
121
        if ($registrations->count() > 0) {
122
            foreach ($registrations as $registration) {
123
                /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration */
124
                if ($delete) {
125
                    $this->registrationRepository->remove($registration);
126 1
                } else {
127
                    $registration->setHidden(true);
128 1
                    $this->registrationRepository->update($registration);
129 1
                }
130
            }
131 1
        }
132 1
    }
133 1
134 1
    /**
135
     * Duplicates (all public accessable properties) the given registration the
136
     * amount of times configured in amountOfRegistrations
137
     *
138
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
139
     *
140
     * @return void
141
     */
142
    public function createDependingRegistrations($registration)
143
    {
144 4
        $registrations = $registration->getAmountOfRegistrations();
145
        for ($i = 1; $i <= $registrations - 1; $i++) {
146
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $newReg */
147 4
            $newReg = $this->objectManager->get(Registration::class);
148 4
            $properties = ObjectAccess::getGettableProperties($registration);
149 4
            foreach ($properties as $propertyName => $propertyValue) {
150 4
                ObjectAccess::setProperty($newReg, $propertyName, $propertyValue);
151
            }
152 4
            $newReg->setMainRegistration($registration);
153 1
            $newReg->setAmountOfRegistrations(1);
154 1
            $newReg->setIgnoreNotifications(true);
155 1
            $this->registrationRepository->add($newReg);
156 1
        }
157 3
    }
158
159
    /**
160 4
     * Confirms all depending registrations based on the given main registration
161 1
     *
162 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
163 1
     *
164 1
     * @return void
165
     */
166 4
    public function confirmDependingRegistrations($registration)
167 1
    {
168 1
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
0 ignored issues
show
Documentation Bug introduced by
The method findByMainRegistration does not exist on object<DERHANSEN\SfEvent...RegistrationRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
169 1
        foreach ($registrations as $foundRegistration) {
170 1
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $foundRegistration */
171
            $foundRegistration->setConfirmed(true);
172 4
            $this->registrationRepository->update($foundRegistration);
173 1
        }
174 1
    }
175 1
176 1
    /**
177
     * Checks if the registration can be confirmed and returns an array of variables
178 4
     *
179
     * @param int $reguid UID of registration
180
     * @param string $hmac HMAC for parameters
181
     *
182
     * @return array
183
     */
184 4
    public function checkConfirmRegistration($reguid, $hmac)
185 4
    {
186 4
        /* @var $registration Registration */
187
        $registration = null;
188 4
        $failed = false;
189
        $messageKey = 'event.message.confirmation_successful';
190
        $titleKey = 'confirmRegistration.title.successful';
191
192
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
193
            $failed = true;
194
            $messageKey = 'event.message.confirmation_failed_wrong_hmac';
195
            $titleKey = 'confirmRegistration.title.failed';
196
        } else {
197
            $registration = $this->registrationRepository->findByUid($reguid);
198 1
        }
199
200 1
        if (!$failed && is_null($registration)) {
201 1
            $failed = true;
202 1
            $messageKey = 'event.message.confirmation_failed_registration_not_found';
203 1
            $titleKey = 'confirmRegistration.title.failed';
204 1
        }
205
206
        if (!$failed && $registration->getConfirmationUntil() < new \DateTime()) {
207
            $failed = true;
208
            $messageKey = 'event.message.confirmation_failed_confirmation_until_expired';
209
            $titleKey = 'confirmRegistration.title.failed';
210
        }
211
212
        if (!$failed && $registration->getConfirmed() === true) {
213
            $failed = true;
214 4
            $messageKey = 'event.message.confirmation_failed_already_confirmed';
215
            $titleKey = 'confirmRegistration.title.failed';
216
        }
217 4
218 4
        if (!$failed && $registration->getWaitlist()) {
219 4
            $messageKey = 'event.message.confirmation_waitlist_successful';
220 4
            $titleKey = 'confirmRegistrationWaitlist.title.successful';
221
        }
222 4
223 1
        return [
224 1
            $failed,
225 1
            $registration,
226 1
            $messageKey,
227 3
            $titleKey
228
        ];
229
    }
230 4
231 1
    /**
232 1
     * Cancels all depending registrations based on the given main registration
233 1
     *
234 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
235
     *
236 4
     * @return void
237 1
     */
238 1
    public function cancelDependingRegistrations($registration)
239 1
    {
240 1
        $registrations = $this->registrationRepository->findByMainRegistration($registration);
0 ignored issues
show
Documentation Bug introduced by
The method findByMainRegistration does not exist on object<DERHANSEN\SfEvent...RegistrationRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
241
        foreach ($registrations as $foundRegistration) {
242 4
            $this->registrationRepository->remove($foundRegistration);
243 4
        }
244 4
    }
245 1
246 1
    /**
247 1
     * Checks if the registration can be cancelled and returns an array of variables
248 1
     *
249
     * @param int $reguid UID of registration
250
     * @param string $hmac HMAC for parameters
251 4
     *
252 4
     * @return array
253 4
     */
254
    public function checkCancelRegistration($reguid, $hmac)
255 4
    {
256
        /* @var $registration Registration */
257
        $registration = null;
258
        $failed = false;
259
        $messageKey = 'event.message.cancel_successful';
260
        $titleKey = 'cancelRegistration.title.successful';
261
262
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
263 5
            $failed = true;
264
            $messageKey = 'event.message.cancel_failed_wrong_hmac';
265 5
            $titleKey = 'cancelRegistration.title.failed';
266 1
        } else {
267
            $registration = $this->registrationRepository->findByUid($reguid);
268 4
        }
269
270
        if (!$failed && is_null($registration)) {
271
            $failed = true;
272
            $messageKey = 'event.message.cancel_failed_registration_not_found_or_cancelled';
273
            $titleKey = 'cancelRegistration.title.failed';
274
        }
275
276
        if (!$failed && $registration->getEvent()->getEnableCancel() === false) {
277
            $failed = true;
278
            $messageKey = 'event.message.confirmation_failed_cancel_disabled';
279
            $titleKey = 'cancelRegistration.title.failed';
280
        }
281
282 19
        if (!$failed && $registration->getEvent()->getCancelDeadline() > 0
283
            && $registration->getEvent()->getCancelDeadline() < new \DateTime()
284 19
        ) {
285 19
            $failed = true;
286 2
            $messageKey = 'event.message.cancel_failed_deadline_expired';
287 2
            $titleKey = 'cancelRegistration.title.failed';
288 19
        }
289 2
290 2
        if (!$failed && $registration->getEvent()->getStartdate() < new \DateTime()) {
291 17
            $failed = true;
292 2
            $messageKey = 'event.message.cancel_failed_event_started';
293 2
            $titleKey = 'cancelRegistration.title.failed';
294 15
        }
295 13
296 13
        return [
297 2
            $failed,
298 2
            $registration,
299 13
            $messageKey,
300 11
            $titleKey
301 11
        ];
302 2
    }
303 2
304 11
    /**
305 2
     * Returns the current frontend user object if available
306 2
     *
307 9
     * @return mixed \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null
308 2
     */
309 7
    public function getCurrentFeUserObject()
310 2
    {
311 2
        if (isset($GLOBALS['TSFE']->fe_user->user['uid'])) {
312 7
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
313 5
        }
314 5
315 1
        return null;
316 1
    }
317 19
318
    /**
319
     * Checks, if the registration can successfully be created. Note, that
320
     * $result is passed by reference!
321
     *
322
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
323
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
324
     * @param int $result Result
325
     *
326
     * @return bool
327 2
     */
328
    public function checkRegistrationSuccess($event, $registration, &$result)
329 2
    {
330 2
        $success = true;
331
        if ($event->getEnableRegistration() === false) {
332
            $success = false;
333
            $result = RegistrationResult::REGISTRATION_NOT_ENABLED;
334
        } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new \DateTime()) {
335
            $success = false;
336
            $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED;
337
        } elseif ($event->getStartdate() < new \DateTime()) {
338
            $success = false;
339 2
            $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED;
340
        } elseif ($event->getRegistration()->count() >= $event->getMaxParticipants()
341 2
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
342 1
        ) {
343
            $success = false;
344
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS;
345
        } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations()
346 1
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
347 1
        ) {
348 1
            $success = false;
349
            $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES;
350
        } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) {
351
            $success = false;
352
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED;
353
        } elseif ($event->getUniqueEmailCheck() &&
354
            $this->emailNotUnique($event, $registration->getEmail())
355
        ) {
356
            $success = false;
357
            $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE;
358
        } elseif ($event->getRegistration()->count() >= $event->getMaxParticipants()
359
            && $event->getMaxParticipants() > 0 && $event->getEnableWaitlist()
360
        ) {
361
            $result = RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST;
362 7
        }
363
364 7
        return $success;
365 3
    }
366
367
    /**
368 4
     * Returns if the given e-mail is registered to the given event
369 4
     *
370 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
371 4
     * @param string $email
372 2
     * @return bool
373 2
     */
374 4
    protected function emailNotUnique($event, $email)
375
    {
376
        $registrations = $this->registrationRepository->findEventRegistrationsByEmail($event, $email);
377
378
        return $registrations->count() >= 1;
379
    }
380
381
    /**
382
     * Returns, if payment redirect for the payment method is enabled
383
     *
384
     * @param Registration $registration
385
     * @return bool
386
     */
387
    public function redirectPaymentEnabled($registration)
388
    {
389
        if ($registration->getEvent()->getEnablePayment() === false) {
390
            return false;
391
        }
392
393
        /** @var AbstractPayment $paymentInstance */
394
        $paymentInstance = $this->paymentService->getPaymentInstance($registration->getPaymentmethod());
395
        if ($paymentInstance !== null && $paymentInstance->isRedirectEnabled()) {
396
            return true;
397
        }
398
399
        return false;
400
    }
401
402
    /**
403
     * Returns if the given amount of registrations for the event will be registrations for the waitlist
404
     * (depending on the total amount of registrations and free places)
405
     *
406
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
407
     * @param int $amountOfRegistrations
408
     * @return bool
409
     */
410
    public function isWaitlistRegistration($event, $amountOfRegistrations)
411
    {
412
        if ($event->getMaxParticipants() === 0 || !$event->getEnableWaitlist()) {
413
            return false;
414
        }
415
416
        $result = false;
417
        if (($event->getFreePlaces() > 0 && $event->getFreePlaces() < $amountOfRegistrations)
418
            || $event->getFreePlaces() <= 0) {
419
            $result = true;
420
        }
421
422
        return $result;
423
    }
424
}
425