Completed
Push — master ( 17edcb...7372de )
by Torben
06:23
created

RegistrationService::injectNotificationService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Event\AfterRegistrationMovedFromWaitlist;
15
use DERHANSEN\SfEventMgt\Payment\AbstractPayment;
16
use DERHANSEN\SfEventMgt\Utility\MessageType;
17
use DERHANSEN\SfEventMgt\Utility\RegistrationResult;
18
use Psr\EventDispatcher\EventDispatcherInterface;
19
use TYPO3\CMS\Core\Database\Connection;
20
use TYPO3\CMS\Core\Database\ConnectionPool;
21
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
24
25
/**
26
 * RegistrationService
27
 *
28
 * @author Torben Hansen <[email protected]>
29
 */
30
class RegistrationService
31
{
32
    /**
33
     * The object manager
34
     *
35
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
36
     * */
37
    protected $objectManager;
38
39
    /**
40
     * @var EventDispatcherInterface
41
     */
42
    protected $eventDispatcher;
43
44
    /**
45
     * RegistrationRepository
46
     *
47
     * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository
48
     * */
49
    protected $registrationRepository;
50
51
    /**
52
     * FrontendUserRepository
53
     *
54
     * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
55
     * */
56
    protected $frontendUserRepository;
57
58
    /**
59
     * Hash Service
60
     *
61
     * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
62
     * */
63
    protected $hashService;
64
65
    /**
66
     * Payment Service
67
     *
68
     * @var \DERHANSEN\SfEventMgt\Service\PaymentService
69
     * */
70
    protected $paymentService;
71
72
    /**
73
     * Notification Service
74
     *
75
     * @var \DERHANSEN\SfEventMgt\Service\NotificationService
76
     */
77
    protected $notificationService;
78 4
79
    /**
80 4
     * DI for $frontendUserRepository
81 4
     *
82 4
     * @param \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository
83
     */
84 4
    public function injectFrontendUserRepository(
85 2
        \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository
86 2
    ) {
87 2
        $this->frontendUserRepository = $frontendUserRepository;
88 2
    }
89
90 4
    /**
91 4
     * DI for $hashService
92 4
     *
93
     * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
94
     */
95
    public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
96
    {
97
        $this->hashService = $hashService;
98
    }
99
100
    /**
101
     * @param \DERHANSEN\SfEventMgt\Service\NotificationService $notificationService
102 2
     */
103
    public function injectNotificationService(\DERHANSEN\SfEventMgt\Service\NotificationService $notificationService)
104 2
    {
105 2
        $this->notificationService = $notificationService;
106
    }
107 2
108 2
    /**
109 2
     * DI for $objectManager
110 2
     *
111 2
     * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
112 2
     */
113 2
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
114 2
    {
115 2
        $this->objectManager = $objectManager;
116 2
    }
117 2
118
    /**
119
     * @param EventDispatcherInterface $eventDispatcher
120
     */
121
    public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher)
122
    {
123
        $this->eventDispatcher = $eventDispatcher;
124
    }
125
126 2
    /**
127
     * DI for $paymentService
128 2
     *
129 2
     * @param \DERHANSEN\SfEventMgt\Service\PaymentService $paymentService
130
     */
131 2
    public function injectPaymentService(\DERHANSEN\SfEventMgt\Service\PaymentService $paymentService)
132 2
    {
133 2
        $this->paymentService = $paymentService;
134 2
    }
135
136
    /**
137
     * DI for $registrationRepository
138
     *
139
     * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
140
     */
141
    public function injectRegistrationRepository(
142
        \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository
143
    ) {
144 8
        $this->registrationRepository = $registrationRepository;
145
    }
146
147 8
    /**
148 8
     * Duplicates (all public accessable properties) the given registration the
149 8
     * amount of times configured in amountOfRegistrations
150 8
     *
151
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
152 8
     */
153 2
    public function createDependingRegistrations($registration)
154 2
    {
155 2
        $registrations = $registration->getAmountOfRegistrations();
156 2
        for ($i = 1; $i <= $registrations - 1; $i++) {
157 6
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $newReg */
158
            $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...
159
            $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...
160 8
            foreach ($properties as $propertyName => $propertyValue) {
161 2
                ObjectAccess::setProperty($newReg, $propertyName, $propertyValue);
162 2
            }
163 2
            $newReg->setMainRegistration($registration);
164 2
            $newReg->setAmountOfRegistrations(1);
165
            $newReg->setIgnoreNotifications(true);
166 8
            $newReg->_setProperty('_languageUid', $registration->_getProperty('_languageUid'));
167 2
            $this->registrationRepository->add($newReg);
168 2
        }
169 2
    }
170 2
171
    /**
172 8
     * Confirms all depending registrations based on the given main registration
173 2
     *
174 2
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
175 2
     */
176 2
    public function confirmDependingRegistrations($registration)
177
    {
178 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...
179
        foreach ($registrations as $foundRegistration) {
180
            /** @var \DERHANSEN\SfEventMgt\Domain\Model\Registration $foundRegistration */
181
            $foundRegistration->setConfirmed(true);
182
            $this->registrationRepository->update($foundRegistration);
183
        }
184 8
    }
185 8
186 8
    /**
187
     * Checks if the registration can be confirmed and returns an array of variables
188 8
     *
189
     * @param int $reguid UID of registration
190
     * @param string $hmac HMAC for parameters
191
     *
192
     * @return array
193
     */
194
    public function checkConfirmRegistration($reguid, $hmac)
195
    {
196
        /* @var $registration Registration */
197
        $registration = null;
198 2
        $failed = false;
199
        $messageKey = 'event.message.confirmation_successful';
200 2
        $titleKey = 'confirmRegistration.title.successful';
201 2
202 2
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
203 2
            $failed = true;
204 2
            $messageKey = 'event.message.confirmation_failed_wrong_hmac';
205
            $titleKey = 'confirmRegistration.title.failed';
206
        } else {
207
            $registration = $this->registrationRepository->findByUid($reguid);
208
        }
209
210
        if (!$failed && is_null($registration)) {
211
            $failed = true;
212
            $messageKey = 'event.message.confirmation_failed_registration_not_found';
213
            $titleKey = 'confirmRegistration.title.failed';
214 8
        }
215
216
        if (!$failed && $registration->getConfirmationUntil() < new \DateTime()) {
217 8
            $failed = true;
218 8
            $messageKey = 'event.message.confirmation_failed_confirmation_until_expired';
219 8
            $titleKey = 'confirmRegistration.title.failed';
220 8
        }
221
222 8
        if (!$failed && $registration->getConfirmed() === true) {
223 2
            $failed = true;
224 2
            $messageKey = 'event.message.confirmation_failed_already_confirmed';
225 2
            $titleKey = 'confirmRegistration.title.failed';
226 2
        }
227 6
228
        if (!$failed && $registration->getWaitlist()) {
229
            $messageKey = 'event.message.confirmation_waitlist_successful';
230 8
            $titleKey = 'confirmRegistrationWaitlist.title.successful';
231 2
        }
232 2
233 2
        return [
234 2
            $failed,
235
            $registration,
236 8
            $messageKey,
237 2
            $titleKey
238 2
        ];
239 2
    }
240 2
241
    /**
242 8
     * Cancels all depending registrations based on the given main registration
243 8
     *
244 8
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
245 2
     */
246 2
    public function cancelDependingRegistrations($registration)
247 2
    {
248 2
        $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...
249
        foreach ($registrations as $foundRegistration) {
250
            $this->registrationRepository->remove($foundRegistration);
251 8
        }
252 8
    }
253 8
254
    /**
255 8
     * Checks if the registration can be cancelled and returns an array of variables
256
     *
257
     * @param int $reguid UID of registration
258
     * @param string $hmac HMAC for parameters
259
     *
260
     * @return array
261
     */
262
    public function checkCancelRegistration($reguid, $hmac)
263 10
    {
264
        /* @var $registration Registration */
265 10
        $registration = null;
266 2
        $failed = false;
267
        $messageKey = 'event.message.cancel_successful';
268 8
        $titleKey = 'cancelRegistration.title.successful';
269
270
        if (!$this->hashService->validateHmac('reg-' . $reguid, $hmac)) {
271
            $failed = true;
272
            $messageKey = 'event.message.cancel_failed_wrong_hmac';
273
            $titleKey = 'cancelRegistration.title.failed';
274
        } else {
275
            $registration = $this->registrationRepository->findByUid($reguid);
276
        }
277
278
        if (!$failed && is_null($registration)) {
279
            $failed = true;
280
            $messageKey = 'event.message.cancel_failed_registration_not_found_or_cancelled';
281
            $titleKey = 'cancelRegistration.title.failed';
282 38
        }
283
284 38
        if (!$failed && $registration->getEvent()->getEnableCancel() === false) {
285 38
            $failed = true;
286 4
            $messageKey = 'event.message.confirmation_failed_cancel_disabled';
287 4
            $titleKey = 'cancelRegistration.title.failed';
288 38
        }
289 4
290 4
        if (!$failed && $registration->getEvent()->getCancelDeadline() > 0
291 34
            && $registration->getEvent()->getCancelDeadline() < new \DateTime()
292 4
        ) {
293 4
            $failed = true;
294 30
            $messageKey = 'event.message.cancel_failed_deadline_expired';
295 26
            $titleKey = 'cancelRegistration.title.failed';
296 26
        }
297 4
298 4
        if (!$failed && $registration->getEvent()->getStartdate() < new \DateTime()) {
299 26
            $failed = true;
300 22
            $messageKey = 'event.message.cancel_failed_event_started';
301 22
            $titleKey = 'cancelRegistration.title.failed';
302 4
        }
303 4
304 22
        return [
305 4
            $failed,
306 4
            $registration,
307 18
            $messageKey,
308 4
            $titleKey
309 14
        ];
310 4
    }
311 4
312 14
    /**
313 10
     * Returns the current frontend user object if available
314 10
     *
315 2
     * @return mixed \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null
316 2
     */
317 38
    public function getCurrentFeUserObject()
318
    {
319
        if (isset($GLOBALS['TSFE']->fe_user->user['uid'])) {
320
            return $this->frontendUserRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
321
        }
322
323
        return null;
324
    }
325
326
    /**
327 4
     * Checks, if the registration can successfully be created. Note, that
328
     * $result is passed by reference!
329 4
     *
330 4
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
331
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration
332
     * @param int $result Result
333
     *
334
     * @return array
335
     */
336
    public function checkRegistrationSuccess($event, $registration, $result)
337
    {
338
        $success = true;
339 4
        if ($event->getEnableRegistration() === false) {
340
            $success = false;
341 4
            $result = RegistrationResult::REGISTRATION_NOT_ENABLED;
342 2
        } elseif ($event->getRegistrationDeadline() != null && $event->getRegistrationDeadline() < new \DateTime()) {
343
            $success = false;
344
            $result = RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED;
345
        } elseif ($event->getStartdate() < new \DateTime()) {
346 2
            $success = false;
347 2
            $result = RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED;
348 2
        } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants()
349
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
350
        ) {
351
            $success = false;
352
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS;
353
        } elseif ($event->getFreePlaces() < $registration->getAmountOfRegistrations()
354
            && $event->getMaxParticipants() > 0 && !$event->getEnableWaitlist()
355
        ) {
356
            $success = false;
357
            $result = RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES;
358
        } elseif ($event->getMaxRegistrationsPerUser() < $registration->getAmountOfRegistrations()) {
359
            $success = false;
360
            $result = RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED;
361
        } elseif ($event->getUniqueEmailCheck() &&
362 14
            $this->emailNotUnique($event, $registration->getEmail())
363
        ) {
364 14
            $success = false;
365 6
            $result = RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE;
366
        } elseif ($event->getRegistrations()->count() >= $event->getMaxParticipants()
367
            && $event->getMaxParticipants() > 0 && $event->getEnableWaitlist()
368 8
        ) {
369 8
            $result = RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST;
370 2
        }
371 8
372 4
        return [$success, $result];
373 4
    }
374 8
375
    /**
376
     * Returns if the given email is registered to the given event
377
     *
378
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
379
     * @param string $email
380
     * @return bool
381
     */
382
    protected function emailNotUnique($event, $email)
383
    {
384
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
385
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
386
        $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
387
        $registrations = $queryBuilder->count('email')
388
            ->from('tx_sfeventmgt_domain_model_registration')
389
            ->where(
390
                $queryBuilder->expr()->eq(
391
                    'event',
392
                    $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT)
393
                ),
394
                $queryBuilder->expr()->eq(
395
                    'email',
396
                    $queryBuilder->createNamedParameter($email, Connection::PARAM_STR)
397
                )
398
            )
399
            ->execute()
400
            ->fetchColumn();
401
402
        return $registrations >= 1;
403
    }
404
405
    /**
406
     * Returns, if payment redirect for the payment method is enabled
407
     *
408
     * @param Registration $registration
409
     * @return bool
410
     */
411
    public function redirectPaymentEnabled($registration)
412
    {
413
        if ($registration->getEvent()->getEnablePayment() === false) {
414
            return false;
415
        }
416
417
        /** @var AbstractPayment $paymentInstance */
418
        $paymentInstance = $this->paymentService->getPaymentInstance($registration->getPaymentmethod());
419
        if ($paymentInstance !== null && $paymentInstance->isRedirectEnabled()) {
420
            return true;
421
        }
422
423
        return false;
424
    }
425
426
    /**
427
     * Returns if the given amount of registrations for the event will be registrations for the waitlist
428
     * (depending on the total amount of registrations and free places)
429
     *
430
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event
431
     * @param int $amountOfRegistrations
432
     * @return bool
433
     */
434
    public function isWaitlistRegistration($event, $amountOfRegistrations)
435
    {
436
        if ($event->getMaxParticipants() === 0 || !$event->getEnableWaitlist()) {
437
            return false;
438
        }
439
440
        $result = false;
441
        if (($event->getFreePlaces() > 0 && $event->getFreePlaces() < $amountOfRegistrations)
442
            || $event->getFreePlaces() <= 0) {
443
            $result = true;
444
        }
445
446
        return $result;
447
    }
448
449
    /**
450
     * Fixes the event uid of a registration if the event has been saved as a child of a translated event.
451
     *
452
     * Since TYPO3 9.5 (#82363), registrations for events are saved to the translated event record
453
     *
454
     * Example:
455
     *
456
     * When a registration is saved for a translated event, the registration $registration->setEvent($event) will
457
     * now save the UID of the translated event instead of the uid of the event in default language.
458
     *
459
     * This behavior breaks limitations on events (e.g. max participants). Therefore, the registration must always
460
     * be related to the default event language (Extbase behavior before TYPO3 9.5)
461
     *
462
     * @param Registration $registration
463
     * @param Event $event
464
     */
465
    public function fixRegistrationEvent(Registration $registration, Event $event)
466
    {
467
        // Early return when event is in default language
468
        if ((int)$event->_getProperty('_languageUid') === 0) {
469
            return;
470
        }
471
        $this->updateRegistrationEventUid($registration, $event);
472
        $this->updateEventRegistrationCounters($event);
473
    }
474
475
    /**
476
     * Ensures, that the field "sys_language_uid" for registration fields values has the same value as the
477
     * language of the registration and event. This is required, so emails include registration field values
478
     * and correct registration field labels in their translated state.
479
     *
480
     * @param Registration $registration
481
     * @param Event $event
482
     */
483
    public function fixRegistationFieldValueLanguage(Registration $registration, Event $event)
484
    {
485
        // Early return when event is in default language or no registration fields
486
        if ((int)$event->_getProperty('_languageUid') === 0 || $event->getRegistrationFields()->count() === 0) {
487
            return;
488
        }
489
490
        $this->updateRegistrationFieldValueLanguage($registration, $event->_getProperty('_languageUid'));
491
    }
492
493
    /**
494
     * Updates the field "sys_language_uid" for all registration field values of the given registration
495
     *
496
     * @param Registration $registration
497
     * @param int $sysLanguageUid
498
     */
499
    protected function updateRegistrationFieldValueLanguage(Registration $registration, int $sysLanguageUid)
500
    {
501
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
502
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
503
        $queryBuilder->update('tx_sfeventmgt_domain_model_registration_fieldvalue')
504
            ->set('sys_language_uid', $sysLanguageUid)
505
            ->where(
506
                $queryBuilder->expr()->eq(
507
                    'registration',
508
                    $queryBuilder->createNamedParameter($registration->getUid(), Connection::PARAM_INT)
509
                )
510
            )
511
            ->execute();
512
    }
513
514
    /**
515
     * Sets the "event" field of the given registration to the uid of the given event
516
     *
517
     * @param Registration $registration
518
     * @param Event $event
519
     */
520
    protected function updateRegistrationEventUid(Registration $registration, Event $event)
521
    {
522
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
523
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
524
        $queryBuilder->update('tx_sfeventmgt_domain_model_registration')
525
            ->set('event', $event->getUid())
526
            ->where(
527
                $queryBuilder->expr()->eq(
528
                    'uid',
529
                    $queryBuilder->createNamedParameter($registration->getUid(), Connection::PARAM_INT)
530
                )
531
            )
532
            ->execute();
533
    }
534
535
    /**
536
     * Updates registration/waitlist registration counters for the given event
537
     *
538
     * @param Event $event
539
     */
540
    protected function updateEventRegistrationCounters(Event $event)
541
    {
542
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
543
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_event');
544
545
        $countRegistrations = $this->getEventRegistrationCount($event, 0);
546
        $countRegistrationsWaitlist = $this->getEventRegistrationCount($event, 1);
547
548
        $queryBuilder->update('tx_sfeventmgt_domain_model_event')
549
            ->set('registration', $countRegistrations)
550
            ->set('registration_waitlist', $countRegistrationsWaitlist)
551
            ->where(
552
                $queryBuilder->expr()->eq(
553
                    'uid',
554
                    $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT)
555
                )
556
            )
557
            ->orWhere(
558
                $queryBuilder->expr()->eq(
559
                    'l10n_parent',
560
                    $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT)
561
                )
562
            )
563
            ->execute();
564
    }
565
566
    /**
567
     * Returns the total amount of registrations/waitlist registrations for an event
568
     *
569
     * @param Event $event
570
     * @param int $waitlist
571
     * @return mixed
572
     */
573
    protected function getEventRegistrationCount(Event $event, int $waitlist = 0)
574
    {
575
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
576
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
577
        $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
578
579
        return $queryBuilder->count('uid')
580
            ->from('tx_sfeventmgt_domain_model_registration')
581
            ->where(
582
                $queryBuilder->expr()->eq(
583
                    'event',
584
                    $queryBuilder->createNamedParameter($event->getUid(), Connection::PARAM_INT)
585
                ),
586
                $queryBuilder->expr()->eq(
587
                    'waitlist',
588
                    $queryBuilder->createNamedParameter($waitlist, Connection::PARAM_INT)
589
                )
590
            )
591
            ->execute()
592
            ->fetchColumn();
593
    }
594
595
    /**
596
     * Handles the process of moving registration up from the waitlist.
597
     *
598
     * @param Event $event
599
     * @param array $settings
600
     */
601
    public function moveUpWaitlistRegistrations(Event $event, array $settings)
602
    {
603
        // Early return if move up not enabled, no registrations on waitlist or no free places left
604
        if (!$event->getEnableWaitlistMoveup() || $event->getRegistrationsWaitlist()->count() === 0 ||
605
            $event->getFreePlaces() === 0
606
        ) {
607
            return;
608
        }
609
610
        $freePlaces = $event->getFreePlaces();
611
        $moveupRegistrations = $this->registrationRepository->findWaitlistMoveUpRegistrations($event);
612
613
        /** @var Registration $registration */
614
        foreach ($moveupRegistrations as $registration) {
615
            $registration->setWaitlist(false);
616
            $this->registrationRepository->update($registration);
617
618
            // Send messages to user and admin
619
            $this->notificationService->sendUserMessage(
620
                $event,
621
                $registration,
622
                $settings,
623
                MessageType::REGISTRATION_WAITLIST_MOVE_UP
624
            );
625
            $this->notificationService->sendAdminMessage(
626
                $registration->getEvent(),
627
                $registration,
628
                $settings,
629
                MessageType::REGISTRATION_WAITLIST_MOVE_UP
630
            );
631
632
            $this->eventDispatcher->dispatch(new AfterRegistrationMovedFromWaitlist($registration, $this));
0 ignored issues
show
Documentation introduced by
new \DERHANSEN\SfEventMg...t($registration, $this) is of type object<DERHANSEN\SfEvent...ationMovedFromWaitlist>, but the function expects a object<Psr\EventDispatcher\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...
633
634
            $freePlaces--;
635
            if ($freePlaces === 0) {
636
                break;
637
            }
638
        }
639
    }
640
}
641