Passed
Push — master ( af9933...6d2375 )
by Torben
04:41 queued 01:21
created

PrefillViewHelper::render()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 3
nop 0
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
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;
13
14
/**
15
 * Prefill ViewHelper
16
 */
17
class PrefillViewHelper extends AbstractPrefillViewHelper
18
{
19
    /**
20
     * Initialize arguments
21
     */
22
    public function initializeArguments()
23
    {
24
        parent::initializeArguments();
25
        $this->registerArgument('fieldname', 'string', 'FieldName', true);
26
        $this->registerArgument('prefillSettings', 'array', 'PrefillSettings', false, []);
27
    }
28
29
    /**
30
     * If the current field is available in POST data of the current request, return the value, otherwise
31
     * a property from fe_user (if logged in and if the given field is configured to be prefilled) is returned.
32
     *
33
     * @return string
34
     */
35
    public function render(): string
36 16
    {
37
        $fieldname = $this->arguments['fieldname'];
38 16
        $prefillSettings = $this->arguments['prefillSettings'];
39 16
40 2
        $request = $this->renderingContext->getRequest();
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

40
        /** @scrutinizer ignore-call */ 
41
        $request = $this->renderingContext->getRequest();
Loading history...
41
        $registrationData = $request->getParsedBody()[$this->getPluginNamespace($request)] ?? [];
42 14
        if (isset($registrationData['registration'][$fieldname])) {
43 8
            return $registrationData['registration'][$fieldname];
44 14
        }
45 8
46
        if (!isset($GLOBALS['TSFE']) || !$GLOBALS['TSFE']->fe_user->user || empty($prefillSettings) ||
47
            !array_key_exists($fieldname, $prefillSettings)
48 6
        ) {
49 6
            return '';
50 2
        }
51 2
52
        return (string)($GLOBALS['TSFE']->fe_user->user[$prefillSettings[$fieldname]]);
53 4
    }
54
}
55