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

getFieldValueFromSubmittedData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
16
/**
17
 * PrefillMultiValueField ViewHelper for registration fields
18
 */
19
class PrefillMultiValueFieldViewHelper extends AbstractPrefillViewHelper
20
{
21
    /**
22
     * Initialize arguments
23
     */
24
    public function initializeArguments()
25
    {
26
        parent::initializeArguments();
27
        $this->registerArgument('registrationField', 'object', 'RegistrationField', true);
28
        $this->registerArgument('currentValue', 'strong', 'Current value', true);
29
    }
30
31
    /**
32
     * Returns, if the given $currentValue is selected/checked for the given registration field is selected
33
     * If no originalRequest exist (form is not submitted), true is returned if the given $currentValue
34
     * matches the default value of the field
35
     *
36
     * @return bool
37
     */
38
    public function render()
39
    {
40
        /** @var Field $registrationField */
41
        $registrationField = $this->arguments['registrationField'];
42
        $currentValue = $this->arguments['currentValue'];
43
44
        // If mapping errors occured for form, return value that has been submitted
45
        $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

45
        $originalRequest = $this->renderingContext->/** @scrutinizer ignore-call */ getRequest()->getOriginalRequest();
Loading history...
46
        if ($originalRequest) {
47
            $registrationData = $originalRequest->getParsedBody()[$this->getPluginNamespace($originalRequest)] ?? [];
48
49
            return $this->getFieldValueFromSubmittedData(
50
                $registrationData,
51
                $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

51
                /** @scrutinizer ignore-type */ $registrationField->getUid(),
Loading history...
52
                $currentValue
53
            );
54
        }
55
56
        return $this->getFieldValueFromDefaultProperty($registrationField, $currentValue);
57
    }
58
59
    /**
60
     * Returns if the submitted field value is selected
61
     *
62
     * @param array $submittedData
63
     * @param int $fieldUid
64
     * @param string $currentValue
65
     * @return bool
66
     */
67
    protected function getFieldValueFromSubmittedData(array $submittedData, int $fieldUid, string $currentValue): bool
68
    {
69
        $result = false;
70
71
        foreach ($submittedData['registration']['fields'] ?? [] as $submittedFieldUid => $fieldValue) {
72
            if ((int)$submittedFieldUid === $fieldUid) {
73
                $result = $this->isGivenValueSelected($fieldValue, $currentValue);
74
            }
75
        }
76
77
        return $result;
78
    }
79
80
    /**
81
     * @param mixed $fieldValue
82
     * @param string $currentValue
83
     * @return bool
84
     */
85
    protected function isGivenValueSelected($fieldValue, string $currentValue): bool
86
    {
87
        if (is_array($fieldValue)) {
88
            return in_array($currentValue, $fieldValue);
89
        }
90
91
        return $currentValue === $fieldValue;
92
    }
93
94
    /**
95
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration\Field $registrationField
96
     * @param string $currentValue
97
     * @return bool
98
     */
99
    protected function getFieldValueFromDefaultProperty(Field $registrationField, string $currentValue)
100
    {
101
        $defaultValues = GeneralUtility::trimExplode(',', $registrationField->getDefaultValue());
102
103
        return in_array($currentValue, $defaultValues);
104
    }
105
}
106