Passed
Push — typo3_11 ( 05e136 )
by Torben
43:39
created

RegistrationValidator   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
dl 0
loc 151
rs 10
c 1
b 0
f 0
wmc 20

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidator() 0 16 3
A isSpamCheckFailed() 0 9 1
B isValid() 0 35 9
A validateDefaultFields() 0 26 6
A __construct() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.txt file that was distributed with this source code.
8
 */
9
10
namespace DERHANSEN\SfEventMgt\Validation\Validator;
11
12
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
13
use DERHANSEN\SfEventMgt\Service\SpamCheckService;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
16
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
17
use TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator;
18
use TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator;
19
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
20
21
/**
22
 * RegistrationValidator
23
 */
24
class RegistrationValidator extends AbstractValidator
25
{
26
    /**
27
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
28
     */
29
    protected $configurationManager;
30
31
    protected array $settings;
32
33
    public function __construct(array $options = [])
34
    {
35
        parent::__construct($options);
36
        $this->configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
37
38
        $this->settings = $this->configurationManager->getConfiguration(
39
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
40
            'SfEventMgt',
41
            'Pievent'
42
        );
43
    }
44
45
    /**
46
     * Validates the given registration according to required fields set in plugin
47
     * settings. For boolean fields, the booleanValidator is used and it is assumed,
48
     * that boolean fields must have the value "TRUE" (for checkboxes)
49
     *
50
     * @param Registration $value Registration
51
     *
52
     * @return bool
53
     */
54
    protected function isValid($value)
55
    {
56
        $spamSettings = $this->settings['registration']['spamCheck'] ?? [];
57
        if ((bool)$spamSettings['enabled'] && $this->isSpamCheckFailed($value, $spamSettings)) {
58
            $message = $this->translateErrorMessage('registration.spamCheckFailed', 'SfEventMgt');
59
            $this->addErrorForProperty('spamCheck', $message, 1578855253);
60
61
            return false;
62
        }
63
64
        $result = $this->validateDefaultFields($value);
65
66
        // If no required fields are set, then the registration is valid
67
        if ($this->settings['registration']['requiredFields'] === '' ||
68
            !isset($this->settings['registration']['requiredFields'])
69
        ) {
70
            return $result;
71
        }
72
73
        $requiredFields = array_map('trim', explode(',', $this->settings['registration']['requiredFields']));
74
75
        foreach ($requiredFields as $requiredField) {
76
            if ($value->_hasProperty($requiredField)) {
77
                $validator = $this->getValidator(gettype($value->_getProperty($requiredField)), $requiredField);
78
                $validationResult = $validator->validate($value->_getProperty($requiredField));
79
                if ($validationResult->hasErrors()) {
80
                    $result = false;
81
                    foreach ($validationResult->getErrors() as $error) {
82
                        $this->result->forProperty($requiredField)->addError($error);
83
                    }
84
                }
85
            }
86
        }
87
88
        return $result;
89
    }
90
91
    /**
92
     * Validates the default fields of a registration, that must be filled out. Since domain object validation
93
     * did not work as expected with registration fields (domain object validation results completely ignored)
94
     * this own validation is done
95
     *
96
     * Checks:
97
     * - firstname: NotEmpty
98
     * - lastname: NotEmpty
99
     * - email: NotEmpty, EmailAddress
100
     *
101
     * @param Registration $value
102
     * @return bool
103
     */
104
    protected function validateDefaultFields(Registration $value): bool
105
    {
106
        $result = true;
107
108
        $defaultFields = ['firstname', 'lastname', 'email'];
109
        foreach ($defaultFields as $defaultField) {
110
            $validator = new NotEmptyValidator();
111
            $validationResult = $validator->validate($value->_getProperty($defaultField));
112
            if ($validationResult->hasErrors()) {
113
                $result = false;
114
                foreach ($validationResult->getErrors() as $error) {
115
                    $this->result->forProperty($defaultField)->addError($error);
116
                }
117
            }
118
        }
119
120
        $validator = new EmailAddressValidator();
121
        $validationResult = $validator->validate($value->_getProperty('email'));
122
        if ($validationResult->hasErrors()) {
123
            $result = false;
124
            foreach ($validationResult->getErrors() as $error) {
125
                $this->result->forProperty('email')->addError($error);
126
            }
127
        }
128
129
        return $result;
130
    }
131
132
    /**
133
     * Processes the spam check and returns, if it failed or not
134
     *
135
     * @param Registration $registration
136
     * @param array $settings
137
     * @throws \DERHANSEN\SfEventMgt\SpamChecks\Exceptions\SpamCheckNotFoundException
138
     * @return bool
139
     */
140
    protected function isSpamCheckFailed(Registration $registration, array $settings): bool
141
    {
142
        $spamCheckService = new SpamCheckService(
143
            $registration,
144
            $settings,
145
            GeneralUtility::_GPmerged('tx_sfeventmgt_pievent')
146
        );
147
148
        return $spamCheckService->isSpamCheckFailed();
149
    }
150
151
    /**
152
     * Returns a validator object depending on the given type of the property
153
     *
154
     * @param string $type Type
155
     * @param string $field The field
156
     *
157
     * @return AbstractValidator
158
     */
159
    protected function getValidator(string $type, string $field): AbstractValidator
160
    {
161
        switch ($type) {
162
            case 'boolean':
163
                /** @var BooleanValidator $validator */
164
                $validator = new BooleanValidator(['is' => true]);
165
                break;
166
            default:
167
                if ($field == 'recaptcha') {
168
                    $validator = new RecaptchaValidator();
169
                } else {
170
                    $validator = new NotEmptyValidator();
171
                }
172
        }
173
174
        return $validator;
175
    }
176
}
177