PrefillMultiValueFieldViewHelper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldValueFromSubmittedData() 0 11 3
A getFieldValueFromDefaultProperty() 0 5 1
A isGivenValueSelected() 0 7 2
A render() 0 22 2
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\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\Core\Utility\GeneralUtility;
18
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...
19
20
/**
21
 * PrefillMultiValueField ViewHelper for registration fields
22
 */
23
class PrefillMultiValueFieldViewHelper extends AbstractPrefillViewHelper
24
{
25
    /**
26
     * Initialize arguments
27
     */
28
    public function initializeArguments(): void
29
    {
30
        parent::initializeArguments();
31
        $this->registerArgument('registrationField', 'object', 'RegistrationField', true);
32
        $this->registerArgument('currentValue', 'strong', 'Current value', true);
33
    }
34
35
    /**
36
     * Returns, if the given $currentValue is selected/checked for the given registration field is selected
37
     * If no originalRequest exist (form is not submitted), true is returned if the given $currentValue
38
     * matches the default value of the field
39
     */
40
    public function render(): bool
41
    {
42
        /** @var Field $registrationField */
43
        $registrationField = $this->arguments['registrationField'];
44
        $currentValue = $this->arguments['currentValue'];
45
46
        // If mapping errors occurred for form, return value that has been submitted
47
        $request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
48
        /** @var ExtbaseRequestParameters $extbaseRequestParameters */
49
        $extbaseRequestParameters = $request->getAttribute('extbase');
50
        $originalRequest = $extbaseRequestParameters->getOriginalRequest();
51
        if ($originalRequest) {
52
            $registrationData = $originalRequest->getParsedBody()[$this->getPluginNamespace($originalRequest)] ?? [];
53
54
            return $this->getFieldValueFromSubmittedData(
55
                $registrationData,
56
                $registrationField->getUid(),
57
                $currentValue
58
            );
59
        }
60
61
        return $this->getFieldValueFromDefaultProperty($registrationField, $currentValue);
62
    }
63
64
    /**
65
     * Returns if the submitted field value is selected
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
    protected function isGivenValueSelected(mixed $fieldValue, string $currentValue): bool
81
    {
82
        if (is_array($fieldValue)) {
83
            return in_array($currentValue, $fieldValue, true);
84
        }
85
86
        return $currentValue === $fieldValue;
87
    }
88
89
    protected function getFieldValueFromDefaultProperty(Field $registrationField, string $currentValue): bool
90
    {
91
        $defaultValues = GeneralUtility::trimExplode(',', $registrationField->getDefaultValue());
92
93
        return in_array($currentValue, $defaultValues);
94
    }
95
}
96