Completed
Push — development ( eb3219...d28feb )
by Torben
02:44
created

SimultaneousRegistrationsViewHelper::render()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 8.9137
c 0
b 0
f 0
cc 6
nc 6
nop 0
crap 6
1
<?php
2
namespace DERHANSEN\SfEventMgt\ViewHelpers\Event;
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\Event;
12
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
13
14
/**
15
 * SimultaneousRegistrations ViewHelper
16
 *
17
 * @author Torben Hansen <[email protected]>
18
 */
19
class SimultaneousRegistrationsViewHelper extends AbstractViewHelper
20
{
21
    /**
22
     * Initialize arguments
23
     */
24
    public function initializeArguments()
25
    {
26
        parent::initializeArguments();
27
        $this->registerArgument('event', 'object', 'The event', true);
28
    }
29
30
    /**
31
     * Returns an array with the amount of possible simultaneous registrations
32
     * respecting the maxRegistrationsPerUser setting and also respects the amount
33
     * of remaining free places.
34
     *
35
     * The returned array index starts at 1 if at least one registration is possible
36
     *
37
     * @return array
38 8
     */
39
    public function render()
40 8
    {
41 8
        /** @var Event $event */
42 8
        $event = $this->arguments['event'];
43 5
        $minPossibleRegistrations = 1;
44 8
        if ($event->getMaxParticipants() > 0 &&
45 4
            $event->getMaxRegistrationsPerUser() >= $event->getFreePlaces()
46 4
        ) {
47 4
            if ($event->getEnableWaitlist() && $event->getFreePlaces() === 0) {
48
                $maxPossibleRegistrations = $event->getMaxRegistrationsPerUser();
49 8
            } else {
50 8
                $maxPossibleRegistrations = $event->getFreePlaces();
51 6
            }
52 6
        } else {
53 6
            $maxPossibleRegistrations = $event->getMaxRegistrationsPerUser();
54 8
        }
55
        $result = [$maxPossibleRegistrations];
56
        if ($maxPossibleRegistrations >= $minPossibleRegistrations) {
57
            $arrayWithZeroAsIndex = range($minPossibleRegistrations, $maxPossibleRegistrations);
58
            $result = array_combine(range(1, count($arrayWithZeroAsIndex)), $arrayWithZeroAsIndex);
59
        }
60
61
        return $result;
62
    }
63
}
64