Passed
Push — typo3_11 ( 05e136 )
by Torben
43:39
created

PrefillFieldViewHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 14
dl 0
loc 50
rs 10
c 2
b 1
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 4 1
A render() 0 14 2
A getFieldValueFromSubmittedData() 0 10 3
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\ViewHelpers\Registration\Field;
11
12
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
13
use DERHANSEN\SfEventMgt\ViewHelpers\AbstractPrefillViewHelper;
14
15
/**
16
 * PrefillField ViewHelper for registration fields
17
 */
18
class PrefillFieldViewHelper extends AbstractPrefillViewHelper
19
{
20
    /**
21
     * Initialize arguments
22
     */
23
    public function initializeArguments()
24
    {
25
        parent::initializeArguments();
26
        $this->registerArgument('registrationField', 'object', 'The registrationField object', true);
27
    }
28
29
    /**
30
     * Returns a string to be used as prefill value for the given registration field (type=input). If the form
31
     * has already been submitted, the submitted value for the field is returned.
32
     *
33
     * @return string
34
     */
35
    public function render()
36
    {
37
        /** @var Field $registrationField */
38
        $registrationField = $this->arguments['registrationField'];
39
40
        // If mapping errors occured for form, return value that has been submitted from POST data
41
        $originalRequest = $this->renderingContext->getRequest()->getOriginalRequest();
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on TYPO3Fluid\Fluid\Core\Re...nderingContextInterface. It seems like you code against a sub-type of TYPO3Fluid\Fluid\Core\Re...nderingContextInterface such as TYPO3\CMS\Fluid\Core\Rendering\RenderingContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $originalRequest = $this->renderingContext->/** @scrutinizer ignore-call */ getRequest()->getOriginalRequest();
Loading history...
42
43
        if ($originalRequest) {
44
            $registrationData = $originalRequest->getParsedBody()[$this->getPluginNamespace($originalRequest)] ?? [];
45
            return $this->getFieldValueFromSubmittedData($registrationData, $registrationField->getUid());
0 ignored issues
show
Bug introduced by
It seems like $registrationField->getUid() can also be of type null; however, parameter $fieldUid of DERHANSEN\SfEventMgt\Vie...alueFromSubmittedData() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            return $this->getFieldValueFromSubmittedData($registrationData, /** @scrutinizer ignore-type */ $registrationField->getUid());
Loading history...
46
        }
47
48
        return $registrationField->getDefaultValue();
49
    }
50
51
    /**
52
     * Returns the submitted value for the given field uid
53
     *
54
     * @param array $submittedData
55
     * @param int $fieldUid
56
     * @return string
57
     */
58
    protected function getFieldValueFromSubmittedData(array $submittedData, int $fieldUid): string
59
    {
60
        $result = '';
61
        foreach ($submittedData['registration']['fields'] ?? [] as $submittedFieldUid => $fieldValue) {
62
            if ((int)$submittedFieldUid === $fieldUid) {
63
                $result = $fieldValue;
64
            }
65
        }
66
67
        return $result;
68
    }
69
}
70