Passed
Push — 7.x ( ec4412...deb5a9 )
by Torben
42:06
created

getFieldValueFromSubmittedData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 10
rs 10
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\Registration\Field;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
15
use DERHANSEN\SfEventMgt\ViewHelpers\AbstractPrefillViewHelper;
16
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
17
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
18
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
19
20
/**
21
 * PrefillField ViewHelper for registration fields
22
 */
23
class PrefillFieldViewHelper extends AbstractPrefillViewHelper
24
{
25
    public function initializeArguments(): void
26
    {
27
        parent::initializeArguments();
28
        $this->registerArgument('registrationField', 'object', 'The registrationField object', true);
29
    }
30
31
    /**
32
     * Returns a string to be used as prefill value for the given registration field (type=input). If the form
33
     * has already been submitted, the submitted value for the field is returned.
34
     *
35
     * 1. Default value
36
     * 2. fe_user record value
37
     */
38
    public function render(): string
39
    {
40
        /** @var Field $registrationField */
41
        $registrationField = $this->arguments['registrationField'];
42
43
        // If mapping errors occurred for form, return value that has been submitted from POST data
44
        /** @var ExtbaseRequestParameters $extbaseRequestParameters */
45
        $extbaseRequestParameters = $this->renderingContext->getRequest()->getAttribute('extbase');
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
        $extbaseRequestParameters = $this->renderingContext->/** @scrutinizer ignore-call */ getRequest()->getAttribute('extbase');
Loading history...
46
        $originalRequest = $extbaseRequestParameters->getOriginalRequest();
47
48
        if ($originalRequest) {
49
            $registrationData = $originalRequest->getParsedBody()[$this->getPluginNamespace($originalRequest)] ?? [];
50
            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

50
            return $this->getFieldValueFromSubmittedData($registrationData, /** @scrutinizer ignore-type */ $registrationField->getUid());
Loading history...
51
        }
52
53
        $value = $registrationField->getDefaultValue();
54
        return $this->prefillFromFeuserData($registrationField, $value);
55
    }
56
57
    /**
58
     * Returns the submitted value for the given field uid
59
     */
60
    protected function getFieldValueFromSubmittedData(array $submittedData, int $fieldUid): string
61
    {
62
        $result = '';
63
        foreach ($submittedData['registration']['fields'] ?? [] as $submittedFieldUid => $fieldValue) {
64
            if ((int)$submittedFieldUid === $fieldUid) {
65
                $result = $fieldValue;
66
            }
67
        }
68
69
        return $result;
70
    }
71
72
    /**
73
     * Prefills $value with fe_users data if configured in registration field
74
     *
75
     * @param Field $field
76
     * @param string $value
77
     * @return string
78
     */
79
    protected function prefillFromFeuserData(Field $field, string $value): string
80
    {
81
        if (!$this->getTypoScriptFrontendController() ||
82
            !$this->getFrontendUser()->user ||
83
            $field->getFeuserValue() === '' ||
84
            !array_key_exists($field->getFeuserValue(), $this->getFrontendUser()->user)
85
        ) {
86
            return $value;
87
        }
88
89
        return (string)$this->getFrontendUser()->user[$field->getFeuserValue()];
90
    }
91
92
    protected function getFrontendUser(): FrontendUserAuthentication
93
    {
94
        return $this->getTypoScriptFrontendController()->fe_user;
95
    }
96
97
    protected function getTypoScriptFrontendController(): ?TypoScriptFrontendController
98
    {
99
        return $GLOBALS['TSFE'] ?? null;
100
    }
101
}
102