PrefillFieldViewHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 4 1
A render() 0 20 2
A getFieldValueFromSubmittedData() 0 10 3
A prefillFromFeuserData() 0 13 4
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 Psr\Http\Message\ServerRequestInterface;
17
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use TYPO3\CMS\Extbase\Mvc\Request;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Frontend\Authe...ntendUserAuthentication was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
class PrefillFieldViewHelper extends AbstractPrefillViewHelper
22
{
23
    public function initializeArguments(): void
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
     * 1. Default value
34
     * 2. fe_user record value
35
     */
36
    public function render(): string
37
    {
38
        /** @var Field $registrationField */
39
        $registrationField = $this->arguments['registrationField'];
40
41
        // If mapping errors occurred for form, return value that has been submitted from POST data
42
        /** @var Request $request */
43
        $request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
44
        /** @var ExtbaseRequestParameters $extbaseRequestParameters */
45
        $extbaseRequestParameters = $request->getAttribute('extbase');
46
        $originalRequest = $extbaseRequestParameters->getOriginalRequest();
47
48
        if ($originalRequest) {
49
            $registrationData = $originalRequest->getParsedBody()[$this->getPluginNamespace($originalRequest)] ?? [];
50
            return $this->getFieldValueFromSubmittedData($registrationData, $registrationField->getUid());
51
        }
52
53
        $frontendUser = $request->getAttribute('frontend.user');
54
        $value = $registrationField->getDefaultValue();
55
        return $this->prefillFromFeuserData($frontendUser, $registrationField, $value);
56
    }
57
58
    /**
59
     * Returns the submitted value for the given field uid
60
     */
61
    protected function getFieldValueFromSubmittedData(array $submittedData, int $fieldUid): string
62
    {
63
        $result = '';
64
        foreach ($submittedData['registration']['fields'] ?? [] as $submittedFieldUid => $fieldValue) {
65
            if ((int)$submittedFieldUid === $fieldUid) {
66
                $result = $fieldValue;
67
            }
68
        }
69
70
        return $result;
71
    }
72
73
    /**
74
     * Prefills $value with fe_users data if configured in registration field
75
     */
76
    protected function prefillFromFeuserData(
77
        FrontendUserAuthentication $frontendUser,
78
        Field $field,
79
        string $value
80
    ): string {
81
        if (!$frontendUser->user ||
82
            $field->getFeuserValue() === '' ||
83
            !array_key_exists($field->getFeuserValue(), $frontendUser->user)
84
        ) {
85
            return $value;
86
        }
87
88
        return (string)$frontendUser->user[$field->getFeuserValue()];
89
    }
90
}
91