PrefillViewHelper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 20 5
A initializeArguments() 0 5 1
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;
13
14
use Psr\Http\Message\ServerRequestInterface;
15
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...
16
17
class PrefillViewHelper extends AbstractPrefillViewHelper
18
{
19
    public function initializeArguments(): void
20
    {
21
        parent::initializeArguments();
22
        $this->registerArgument('fieldname', 'string', 'Fieldname', true);
23
        $this->registerArgument('prefillSettings', 'array', 'PrefillSettings', false, []);
24
    }
25
26
    /**
27
     * If the current field is available in POST data of the current request, return the value, otherwise
28
     * a property from fe_user (if logged in and if the given field is configured to be prefilled) is returned.
29
     */
30
    public function render(): string
31
    {
32
        $fieldname = $this->arguments['fieldname'];
33
        $prefillSettings = $this->arguments['prefillSettings'];
34
35
        /** @var Request $request */
36
        $request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
37
        $registrationData = $request->getParsedBody()[$this->getPluginNamespace($request)] ?? [];
38
        if (isset($registrationData['registration'][$fieldname])) {
39
            return $registrationData['registration'][$fieldname];
40
        }
41
42
        $frontendUser = $request->getAttribute('frontend.user');
43
        if (!$frontendUser->user || empty($prefillSettings) ||
44
            !array_key_exists($fieldname, $prefillSettings)
45
        ) {
46
            return '';
47
        }
48
49
        return (string)($frontendUser->user[$prefillSettings[$fieldname]] ?? '');
50
    }
51
}
52