SimultaneousRegistrationsViewHelper::render()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 6
nop 0
dl 0
loc 23
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\ViewHelpers\Event;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Event;
15
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
16
17
/**
18
 * SimultaneousRegistrations ViewHelper
19
 */
20
class SimultaneousRegistrationsViewHelper extends AbstractViewHelper
21
{
22
    public function initializeArguments(): void
23
    {
24
        parent::initializeArguments();
25
        $this->registerArgument('event', 'object', 'The event', true);
26
    }
27
28
    /**
29
     * Returns an array with the amount of possible simultaneous registrations
30
     * respecting the maxRegistrationsPerUser setting and also respects the amount
31
     * of remaining free places.
32
     *
33
     * The returned array index starts at 1 if at least one registration is possible
34
     */
35
    public function render(): array
36
    {
37
        /** @var Event $event */
38
        $event = $this->arguments['event'];
39
        $minPossibleRegistrations = 1;
40
        if ($event->getMaxParticipants() > 0 &&
41
            $event->getMaxRegistrationsPerUser() >= $event->getFreePlaces()
42
        ) {
43
            if ($event->getEnableWaitlist() && $event->getFreePlaces() <= 0) {
44
                $maxPossibleRegistrations = $event->getMaxRegistrationsPerUser();
45
            } else {
46
                $maxPossibleRegistrations = $event->getFreePlaces();
47
            }
48
        } else {
49
            $maxPossibleRegistrations = $event->getMaxRegistrationsPerUser();
50
        }
51
        $result = [$maxPossibleRegistrations];
52
        if ($maxPossibleRegistrations >= $minPossibleRegistrations) {
53
            $arrayWithZeroAsIndex = range($minPossibleRegistrations, $maxPossibleRegistrations);
54
            $result = array_combine(range(1, count($arrayWithZeroAsIndex)), $arrayWithZeroAsIndex);
55
        }
56
57
        return $result;
58
    }
59
}
60