Completed
Push — 4.x ( 70dfe1 )
by Torben
43:43 queued 43:36
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 16
     */
39
    public function render()
40 16
    {
41 16
        /** @var Event $event */
42 16
        $event = $this->arguments['event'];
43 10
        $minPossibleRegistrations = 1;
44 16
        if ($event->getMaxParticipants() > 0 &&
45 8
            $event->getMaxRegistrationsPerUser() >= $event->getFreePlaces()
46 8
        ) {
47 8
            if ($event->getEnableWaitlist() && $event->getFreePlaces() <= 0) {
48
                $maxPossibleRegistrations = $event->getMaxRegistrationsPerUser();
49 16
            } else {
50 16
                $maxPossibleRegistrations = $event->getFreePlaces();
51 12
            }
52 12
        } else {
53 12
            $maxPossibleRegistrations = $event->getMaxRegistrationsPerUser();
54 16
        }
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