Completed
Push — master ( 70dfe1...a97a1c )
by Torben
04:18
created

RegistrationService   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 537
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 11

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
wmc 72
lcom 3
cbo 11
dl 0
loc 537
ccs 160
cts 164
cp 0.9756
rs 2.64
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A injectFrontendUserRepository() 0 5 1
A injectHashService() 0 4 1
A injectObjectManager() 0 4 1
A injectPaymentService() 0 4 1
A injectRegistrationRepository() 0 5 1
A createDependingRegistrations() 0 17 3
A confirmDependingRegistrations() 0 9 2
B checkConfirmRegistration() 0 46 10
A cancelDependingRegistrations() 0 7 2
B checkCancelRegistration() 0 49 11
A getCurrentFeUserObject() 0 8 2
C checkRegistrationSuccess() 0 38 17
A emailNotUnique() 0 22 1
A redirectPaymentEnabled() 0 14 4
A isWaitlistRegistration() 0 14 6
A fixRegistrationEvent() 0 9 2
A fixRegistationFieldValueLanguage() 0 9 3
A updateRegistrationFieldValueLanguage() 0 14 1
A updateRegistrationEventUid() 0 14 1
A updateEventRegistrationCounters() 0 25 1
A getEventRegistrationCount() 0 21 1

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
3
/*
4
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.txt file that was distributed with this source code.
8
 */
9
10
namespace DERHANSEN\SfEventMgt\Service;
11
12
use DERHANSEN\SfEventMgt\Domain\Model\Event;
13
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
14
use DERHANSEN\SfEventMgt\Payment\AbstractPayment;
15
use DERHANSEN\SfEventMgt\Utility\RegistrationResult;
16
use TYPO3\CMS\Core\Database\Connection;
17
use TYPO3\CMS\Core\Database\ConnectionPool;
18
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
21
22
/**
23
 * RegistrationService
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class RegistrationService
28
{
29
    /**
30
     * The object manager
31
     *
32
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
33
     * */
34
    protected $objectManager;
35
36
    /**
37
     * RegistrationRepository
38
     *
39
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
40
     * */
41
    protected $registrationRepository;
42
43
    /**
44
     * FrontendUserRepository
45
     *
46
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
47
     * */
48
    protected $frontendUserRepository;
49
50
    /**
51
     * Hash Service
52
     *
53
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
54
     * */
55
    protected $hashService;
56
57
    /**
58
     * Payment Service
59
     *
60
     * @var \DERHANSEN\SfEventMgt\Service\PaymentService
61
     * */
62
    protected $paymentService;
63
64
    /**
65
     * DI for $frontendUserRepository
66
     *
67
     * @param \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository
68
     */
69
    public function injectFrontendUserRepository(
70
        \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository
71
    ) {
72
        $this->frontendUserRepository = $frontendUserRepository;
73
    }
74
75
    /**
76
     * DI for $hashService
77
     *
78 4
     * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
79
     */
80 4
    public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
81 4
    {
82 4
        $this->hashService = $hashService;
83
    }
84 4
85 2
    /**
86 2
     * DI for $objectManager
87 2
     *
88 2
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
89
     */
90 4
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
91 4
    {
92 4
        $this->objectManager = $objectManager;
93
    }
94
95
    /**
96
     * DI for $paymentService
97
     *
98
     * @param \DERHANSEN\SfEventMgt\Service\PaymentService $paymentService
99
     */
100
    public function injectPaymentService(\DERHANSEN\SfEventMgt\Service\PaymentService $paymentService)
101
    {
102 2
        $this->paymentService = $paymentService;
103
    }
104 2
105 2
    /**
106
     * DI for $registrationRepository
107 2
     *
108 2
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
109 2
     */
110 2
    public function injectRegistrationRepository(
111 2
        \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
112 2
    ) {
113 2
        $this->registrationRepository = $registrationRepository;
114 2
    }
115 2
116 2
    /**
117 2
     * Duplicates (all public accessable properties) the given registration the
118
     * amount of times configured in amountOfRegistrations
119
     *
120
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
121
     */
122
    public function createDependingRegistrations($registration)
123
    {
124
        $registrations = $registration->getAmountOfRegistrations();
125
        for ($i = 1; $i <= $registrations - 1; $i++) {
126 2
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $newReg */
127
            $newReg = $this->objectManager->get(Registration::class);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Object\ObjectManager::get() has been deprecated with message: since TYPO3 10.4, will be removed in version 12.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
128 2
            $properties = ObjectAccess::getGettableProperties($registration);
0 ignored issues
show
Documentation introduced by
$registration is of type object<DERHANSEN\SfEvent...ain\Model\Registration>, but the function expects a object<TYPO3\CMS\Extbase\Reflection\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129 2
            foreach ($properties as $propertyName => $propertyValue) {
130
                ObjectAccess::setProperty($newReg, $propertyName, $propertyValue);
131 2
            }
132 2
            $newReg->setMainRegistration($registration);
133 2
            $newReg->setAmountOfRegistrations(1);
134 2
            $newReg->setIgnoreNotifications(true);
135
            $newReg->_setProperty('_languageUid', $registration->_getProperty('_languageUid'));
136
            $this->registrationRepository->add($newReg);
137
        }
138
    }
139
140
    /**
141
     * Confirms all depending registrations based on the given main registration
142
     *
143
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
144 8
     */
145
    public function confirmDependingRegistrations($registration)
146
    {
147 8
        $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...
148 8
        foreach ($registrations as $foundRegistration) {
149 8
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $foundRegistration */
150 8
            $foundRegistration->setConfirmed(true);
151
            $this->registrationRepository->update($foundRegistration);
152 8
        }
153 2
    }
154 2
155 2
    /**
156 2
     * Checks if the registration can be confirmed and returns an array of variables
157 6
     *
158
     * @param int $reguid UID of registration
159
     * @param string $hmac HMAC for parameters
160 8
     *
161 2
     * @return array
162 2
     */
163 2
    public function checkConfirmRegistration($reguid, $hmac)
164 2
    {
165
        /* @var $registration Registration */
166 8
        $registration = null;
167 2
        $failed = false;
168 2
        $messageKey = 'event.message.confirmation_successful';
169 2
        $titleKey = 'confirmRegistration.title.successful';
170 2
171
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
172 8
            $failed = true;
173 2
            $messageKey = 'event.message.confirmation_failed_wrong_hmac';
174 2
            $titleKey = 'confirmRegistration.title.failed';
175 2
        } else {
176 2
            $registration = $this->registrationRepository->findByUid($reguid);
177
        }
178 8
179
        if (!$failed && is_null($registration)) {
180
            $failed = true;
181
            $messageKey = 'event.message.confirmation_failed_registration_not_found';
182
            $titleKey = 'confirmRegistration.title.failed';
183
        }
184 8
185 8
        if (!$failed && $registration->getConfirmationUntil() < new \DateTime()) {
186 8
            $failed = true;
187
            $messageKey = 'event.message.confirmation_failed_confirmation_until_expired';
188 8
            $titleKey = 'confirmRegistration.title.failed';
189
        }
190
191
        if (!$failed && $registration->getConfirmed() === true) {
192
            $failed = true;
193
            $messageKey = 'event.message.confirmation_failed_already_confirmed';
194
            $titleKey = 'confirmRegistration.title.failed';
195
        }
196
197
        if (!$failed && $registration->getWaitlist()) {
198 2
            $messageKey = 'event.message.confirmation_waitlist_successful';
199
            $titleKey = 'confirmRegistrationWaitlist.title.successful';
200 2
        }
201 2
202 2
        return [
203 2
            $failed,
204 2
            $registration,
205
            $messageKey,
206
            $titleKey
207
        ];
208
    }
209
210
    /**
211
     * Cancels all depending registrations based on the given main registration
212
     *
213
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
214 8
     */
215
    public function cancelDependingRegistrations($registration)
216
    {
217 8
        $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...
218 8
        foreach ($registrations as $foundRegistration) {
219 8
            $this->registrationRepository->remove($foundRegistration);
220 8
        }
221
    }
222 8
223 2
    /**
224 2
     * Checks if the registration can be cancelled and returns an array of variables
225 2
     *
226 2
     * @param int $reguid UID of registration
227 6
     * @param string $hmac HMAC for parameters
228
     *
229
     * @return array
230 8
     */
231 2
    public function checkCancelRegistration($reguid, $hmac)
232 2
    {
233 2
        /* @var $registration Registration */
234 2
        $registration = null;
235
        $failed = false;
236 8
        $messageKey = 'event.message.cancel_successful';
237 2
        $titleKey = 'cancelRegistration.title.successful';
238 2
239 2
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
240 2
            $failed = true;
241
            $messageKey = 'event.message.cancel_failed_wrong_hmac';
242 8
            $titleKey = 'cancelRegistration.title.failed';
243 8
        } else {
244 8
            $registration = $this->registrationRepository->findByUid($reguid);
245 2
        }
246 2
247 2
        if (!$failed && is_null($registration)) {
248 2
            $failed = true;
249
            $messageKey = 'event.message.cancel_failed_registration_not_found_or_cancelled';
250
            $titleKey = 'cancelRegistration.title.failed';
251 8
        }
252 8
253 8
        if (!$failed && $registration->getEvent()->getEnableCancel() === false) {
254
            $failed = true;
255 8
            $messageKey = 'event.message.confirmation_failed_cancel_disabled';
256
            $titleKey = 'cancelRegistration.title.failed';
257
        }
258
259
        if (!$failed && $registration->getEvent()->getCancelDeadline() > 0
260
            && $registration->getEvent()->getCancelDeadline() < new \DateTime()
261
        ) {
262
            $failed = true;
263 10
            $messageKey = 'event.message.cancel_failed_deadline_expired';
264
            $titleKey = 'cancelRegistration.title.failed';
265 10
        }
266 2
267
        if (!$failed && $registration->getEvent()->getStartdate() < new \DateTime()) {
268 8
            $failed = true;
269
            $messageKey = 'event.message.cancel_failed_event_started';
270
            $titleKey = 'cancelRegistration.title.failed';
271
        }
272
273
        return [
274
            $failed,
275
            $registration,
276
            $messageKey,
277
            $titleKey
278
        ];
279
    }
280
281
    /**
282 38
     * Returns the current frontend user object if available
283
     *
284 38
     * @return mixed \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null
285 38
     */
286 4
    public function getCurrentFeUserObject()
287 4
    {
288 38
        if (isset($GLOBALS['TSFE']->fe_user->user['uid'])) {
289 4
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
290 4
        }
291 34
292 4
        return null;
293 4
    }
294 30
295 26
    /**
296 26
     * Checks, if the registration can successfully be created. Note, that
297 4
     * $result is passed by reference!
298 4
     *
299 26
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
300 22
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
301 22
     * @param int $result Result
302 4
     *
303 4
     * @return array
304 22
     */
305 4
    public function checkRegistrationSuccess($event, $registration, $result)
306 4
    {
307 18
        $success = true;
308 4
        if ($event->getEnableRegistration() === false) {
309 14
            $success = false;
310 4
            $result = RegistrationResult::REGISTRATION_NOT_ENABLED;
311 4
        } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new \DateTime()) {
312 14
            $success = false;
313 10
            $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED;
314 10
        } elseif ($event->getStartdate() < new \DateTime()) {
315 2
            $success = false;
316 2
            $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED;
317 38
        } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants()
318
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
319
        ) {
320
            $success = false;
321
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS;
322
        } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations()
323
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
324
        ) {
325
            $success = false;
326
            $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES;
327 4
        } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) {
328
            $success = false;
329 4
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED;
330 4
        } elseif ($event->getUniqueEmailCheck() &&
331
            $this->emailNotUnique($event, $registration->getEmail())
332
        ) {
333
            $success = false;
334
            $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE;
335
        } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants()
336
            && $event->getMaxParticipants() > 0 && $event->getEnableWaitlist()
337
        ) {
338
            $result = RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST;
339 4
        }
340
341 4
        return [$success, $result];
342 2
    }
343
344
    /**
345
     * Returns if the given email is registered to the given event
346 2
     *
347 2
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
348 2
     * @param string $email
349
     * @return bool
350
     */
351
    protected function emailNotUnique($event, $email)
352
    {
353
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
354
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
355
        $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
356
        $registrations = $queryBuilder->count('email')
357
            ->from('tx_sfeventmgt_domain_model_registration')
358
            ->where(
359
                $queryBuilder->expr()->eq(
360
                    'event',
361
                    $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT)
362 14
                ),
363
                $queryBuilder->expr()->eq(
364 14
                    'email',
365 6
                    $queryBuilder->createNamedParameter($email, Connection::PARAM_STR)
366
                )
367
            )
368 8
            ->execute()
369 8
            ->fetchColumn();
370 2
371 8
        return $registrations >= 1;
372 4
    }
373 4
374 8
    /**
375
     * Returns, if payment redirect for the payment method is enabled
376
     *
377
     * @param Registration $registration
378
     * @return bool
379
     */
380
    public function redirectPaymentEnabled($registration)
381
    {
382
        if ($registration->getEvent()->getEnablePayment() === false) {
383
            return false;
384
        }
385
386
        /** @var AbstractPayment $paymentInstance */
387
        $paymentInstance = $this->paymentService->getPaymentInstance($registration->getPaymentmethod());
388
        if ($paymentInstance !== null && $paymentInstance->isRedirectEnabled()) {
389
            return true;
390
        }
391
392
        return false;
393
    }
394
395
    /**
396
     * Returns if the given amount of registrations for the event will be registrations for the waitlist
397
     * (depending on the total amount of registrations and free places)
398
     *
399
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
400
     * @param int $amountOfRegistrations
401
     * @return bool
402
     */
403
    public function isWaitlistRegistration($event, $amountOfRegistrations)
404
    {
405
        if ($event->getMaxParticipants() === 0 || !$event->getEnableWaitlist()) {
406
            return false;
407
        }
408
409
        $result = false;
410
        if (($event->getFreePlaces() > 0 && $event->getFreePlaces() < $amountOfRegistrations)
411
            || $event->getFreePlaces() <= 0) {
412
            $result = true;
413
        }
414
415
        return $result;
416
    }
417
418
    /**
419
     * Fixes the event uid of a registration if the event has been saved as a child of a translated event.
420
     *
421
     * Since TYPO3 9.5 (#82363), registrations for events are saved to the translated event record
422
     *
423
     * Example:
424
     *
425
     * When a registration is saved for a translated event, the registration $registration->setEvent($event) will
426
     * now save the UID of the translated event instead of the uid of the event in default language.
427
     *
428
     * This behavior breaks limitations on events (e.g. max participants). Therefore, the registration must always
429
     * be related to the default event language (Extbase behavior before TYPO3 9.5)
430
     *
431
     * @param Registration $registration
432
     * @param Event $event
433
     */
434
    public function fixRegistrationEvent(Registration $registration, Event $event)
435
    {
436
        // Early return when event is in default language
437
        if ((int)$event->_getProperty('_languageUid') === 0) {
438
            return;
439
        }
440
        $this->updateRegistrationEventUid($registration, $event);
441
        $this->updateEventRegistrationCounters($event);
442
    }
443
444
    /**
445
     * Ensures, that the field "sys_language_uid" for registration fields values has the same value as the
446
     * language of the registration and event. This is required, so emails include registration field values
447
     * and correct registration field labels in their translated state.
448
     *
449
     * @param Registration $registration
450
     * @param Event $event
451
     */
452
    public function fixRegistationFieldValueLanguage(Registration $registration, Event $event)
453
    {
454
        // Early return when event is in default language or no registration fields
455
        if ((int)$event->_getProperty('_languageUid') === 0 || $event->getRegistrationFields()->count() === 0) {
456
            return;
457
        }
458
459
        $this->updateRegistrationFieldValueLanguage($registration, $event->_getProperty('_languageUid'));
460
    }
461
462
    /**
463
     * Updates the field "sys_language_uid" for all registration field values of the given registration
464
     *
465
     * @param Registration $registration
466
     * @param int $sysLanguageUid
467
     */
468
    protected function updateRegistrationFieldValueLanguage(Registration $registration, int $sysLanguageUid)
469
    {
470
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
471
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
472
        $queryBuilder->update('tx_sfeventmgt_domain_model_registration_fieldvalue')
473
            ->set('sys_language_uid', $sysLanguageUid)
474
            ->where(
475
                $queryBuilder->expr()->eq(
476
                    'registration',
477
                    $queryBuilder->createNamedParameter($registration->getUid(), Connection::PARAM_INT)
478
                )
479
            )
480
            ->execute();
481
    }
482
483
    /**
484
     * Sets the "event" field of the given registration to the uid of the given event
485
     *
486
     * @param Registration $registration
487
     * @param Event $event
488
     */
489
    protected function updateRegistrationEventUid(Registration $registration, Event $event)
490
    {
491
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
492
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
493
        $queryBuilder->update('tx_sfeventmgt_domain_model_registration')
494
            ->set('event', $event->getUid())
495
            ->where(
496
                $queryBuilder->expr()->eq(
497
                    'uid',
498
                    $queryBuilder->createNamedParameter($registration->getUid(), Connection::PARAM_INT)
499
                )
500
            )
501
            ->execute();
502
    }
503
504
    /**
505
     * Updates registration/waitlist registration counters for the given event
506
     *
507
     * @param Event $event
508
     */
509
    protected function updateEventRegistrationCounters(Event $event)
510
    {
511
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
512
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_event');
513
514
        $countRegistrations = $this->getEventRegistrationCount($event, 0);
515
        $countRegistrationsWaitlist = $this->getEventRegistrationCount($event, 1);
516
517
        $queryBuilder->update('tx_sfeventmgt_domain_model_event')
518
            ->set('registration', $countRegistrations)
519
            ->set('registration_waitlist', $countRegistrationsWaitlist)
520
            ->where(
521
                $queryBuilder->expr()->eq(
522
                    'uid',
523
                    $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT)
524
                )
525
            )
526
            ->orWhere(
527
                $queryBuilder->expr()->eq(
528
                    'l10n_parent',
529
                    $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT)
530
                )
531
            )
532
            ->execute();
533
    }
534
535
    /**
536
     * Returns the total amount of registrations/waitlist registrations for an event
537
     *
538
     * @param Event $event
539
     * @param int $waitlist
540
     * @return mixed
541
     */
542
    protected function getEventRegistrationCount(Event $event, int $waitlist = 0)
543
    {
544
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
545
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
546
        $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
547
548
        return $queryBuilder->count('uid')
549
            ->from('tx_sfeventmgt_domain_model_registration')
550
            ->where(
551
                $queryBuilder->expr()->eq(
552
                    'event',
553
                    $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT)
554
                ),
555
                $queryBuilder->expr()->eq(
556
                    'waitlist',
557
                    $queryBuilder->createNamedParameter($waitlist, Connection::PARAM_INT)
558
                )
559
            )
560
            ->execute()
561
            ->fetchColumn();
562
    }
563
}
564