Passed
Push — main ( f98caf...46400b )
by Torben
71:32 queued 29:51
created

RegistrationFieldValidator::getFieldValue()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 6
nop 2
dl 0
loc 16
rs 9.6111
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\Validation\Validator;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
17
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
18
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
19
20
/**
21
 * RegistrationFieldValidator
22
 */
23
class RegistrationFieldValidator extends AbstractValidator
24
{
25
    /**
26
     * Validates the additional fields of the given registration.
27
     *
28
     * If $registration->getEvent() is null, the registration does not contain any registration fields
29
     *
30
     * @param Registration $value
31
     */
32
    protected function isValid(mixed $value): void
33
    {
34
        if ($value->getEvent() === null || ($value->getFieldValues()->count() === 0 &&
35
            $value->getEvent()->getRegistrationFields()->count() === 0)) {
36
            return;
37
        }
38
39
        /** @var Registration\Field $registrationField */
40
        foreach ($value->getEvent()->getRegistrationFields() as $registrationField) {
41
            $this->validateField($registrationField, $value->getFieldValues());
42
        }
43
    }
44
45
    /**
46
     * Validates the given registrationField
47
     */
48
    protected function validateField(Registration\Field $registrationField, ObjectStorage $fieldValues): void
49
    {
50
        if (!$registrationField->getRequired()) {
51
            return;
52
        }
53
54
        $validator = $this->getNotEmptyValidator();
55
56
        $fieldValue = $this->getFieldValue($registrationField, $fieldValues);
57
        $validationResult = $validator->validate($fieldValue);
58
        if ($validationResult->hasErrors()) {
59
            $result = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
60
            foreach ($validationResult->getErrors() as $error) {
61
                $this->result->forProperty('fields.' . $registrationField->getUid())->addError($error);
62
            }
63
        }
64
    }
65
66
    protected function getNotEmptyValidator(): NotEmptyValidator
67
    {
68
        return GeneralUtility::makeInstance(NotEmptyValidator::class);
69
    }
70
71
    /**
72
     * Returns the value for the given registrationField from the given fieldValues
73
     *
74
     * @param Registration\Field $registrationField
75
     * @param ObjectStorage $fieldValues
76
     * @return string|array
77
     */
78
    protected function getFieldValue(Registration\Field $registrationField, ObjectStorage $fieldValues)
79
    {
80
        $result = '';
81
        /** @var Registration\FieldValue $fieldValue */
82
        foreach ($fieldValues as $fieldValue) {
83
            if ($fieldValue->getField()->getUid() === $registrationField->getUid()) {
0 ignored issues
show
Bug introduced by
The method getField() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            if ($fieldValue->/** @scrutinizer ignore-call */ getField()->getUid() === $registrationField->getUid()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
                $result = $fieldValue->getValue();
85
            }
86
        }
87
88
        // If field value is an array, then treat one single element with an empty string as an empty value
89
        if (is_array($result) && $result === [0 => '']) {
90
            $result = '';
91
        }
92
93
        return $result;
94
    }
95
}
96