Passed
Push — master ( da5c3a...138f6a )
by Torben
132:09 queued 128:49
created

RegistrationValidator::validateDefaultFields()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 8
nop 1
dl 0
loc 28
ccs 0
cts 0
cp 0
crap 42
rs 9.1111
c 0
b 0
f 0
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\Error;
17
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
18
use TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator;
19
use TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator;
20
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
21
22
/**
23
 * RegistrationValidator
24
 */
25
class RegistrationValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
26
{
27
    /**
28
     * Configuration Manager
29
     *
30
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
31
     */
32
    protected $configurationManager;
33
34
    /**
35
     * Object Manager
36
     *
37
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
38
     */
39
    protected $objectManager;
40
41
    /**
42
     * DI for $configurationManager
43
     *
44
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
45
     */
46
    public function injectConfigurationManager(
47
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
48
    ) {
49
        $this->configurationManager = $configurationManager;
50
    }
51
52
    /**
53
     * DI for $objectManager
54
     *
55 18
     * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
56
     */
57 18
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
58 18
    {
59 18
        $this->objectManager = $objectManager;
60
    }
61 18
62
    /**
63
     * Validates the given registration according to required fields set in plugin
64 18
     * settings. For boolean fields, the booleanValidator is used and it is assumed,
65 16
     * that boolean fields must have the value "TRUE" (for checkboxes)
66 18
     *
67 4
     * @param Registration $value Registration
68
     *
69
     * @return bool
70 14
     */
71 14
    protected function isValid($value)
72
    {
73 14
        $settings = $this->configurationManager->getConfiguration(
74 14
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
75 12
            'SfEventMgt',
76
            'Pievent'
77 12
        );
78 12
79 4
        $spamSettings = $settings['registration']['spamCheck'] ?? [];
80 4
        if ((bool)$spamSettings['enabled'] && $this->isSpamCheckFailed($value, $spamSettings)) {
81 4
            $message = $this->translateErrorMessage('registration.spamCheckFailed', 'SfEventMgt');
82 4
            $error = new Error($message, 1578855253965);
83 4
            $this->result->forProperty('spamCheck')->addError($error);
84 12
85 14
            return false;
86
        }
87 14
88
        $result = $this->validateDefaultFields($value);
89
90
        // If no required fields are set, then the registration is valid
91
        if ($settings['registration']['requiredFields'] === '' ||
92
            !isset($settings['registration']['requiredFields'])
93
        ) {
94
            return true;
95
        }
96
97
        $requiredFields = array_map('trim', explode(',', $settings['registration']['requiredFields']));
98 4
99
        foreach ($requiredFields as $requiredField) {
100
            if ($value->_hasProperty($requiredField)) {
101 4
                $validator = $this->getValidator(gettype($value->_getProperty($requiredField)), $requiredField);
102
                /** @var \TYPO3\CMS\Extbase\Error\Result $validationResult */
103 2
                $validationResult = $validator->validate($value->_getProperty($requiredField));
104 2
                if ($validationResult->hasErrors()) {
105 2
                    $result = false;
106 2
                    foreach ($validationResult->getErrors() as $error) {
107 2
                        $this->result->forProperty($requiredField)->addError($error);
108 2
                    }
109 2
                }
110
            }
111
        }
112
113
        return $result;
114 2
    }
115
116 2
    /**
117 4
     * Validates the default fields of a registration, that must be filled out. Since domain object validation
118
     * did not work as expected with registration fields (domain object validation results completely ignored)
119
     * this own validation is done
120
     *
121
     * Checks:
122
     * - firstname: NotEmpty
123
     * - lastname: NotEmpty
124
     * - email: NotEmpty, EmailAddress
125
     *
126
     * @param Registration $value
127
     * @return bool
128
     */
129
    protected function validateDefaultFields(Registration $value): bool
130
    {
131
        $result = true;
132
133
        $defaultFields = ['firstname', 'lastname', 'email'];
134
        foreach ($defaultFields as $defaultField) {
135
            $validator = GeneralUtility::makeInstance(NotEmptyValidator::class);
136
            /** @var \TYPO3\CMS\Extbase\Error\Result $validationResult */
137
            $validationResult = $validator->validate($value->_getProperty($defaultField));
138
            if ($validationResult->hasErrors()) {
139
                $result = false;
140
                foreach ($validationResult->getErrors() as $error) {
141
                    $this->result->forProperty($defaultField)->addError($error);
142
                }
143
            }
144
        }
145
146
        $validator = GeneralUtility::makeInstance(EmailAddressValidator::class);
147
        /** @var \TYPO3\CMS\Extbase\Error\Result $validationResult */
148
        $validationResult = $validator->validate($value->_getProperty('email'));
149
        if ($validationResult->hasErrors()) {
150
            $result = false;
151
            foreach ($validationResult->getErrors() as $error) {
152
                $this->result->forProperty('email')->addError($error);
153
            }
154
        }
155
156
        return $result;
157
    }
158
159
    /**
160
     * Processes the spam check and returns, if it failed or not
161
     *
162
     * @param Registration $registration
163
     * @param array $settings
164
     * @throws \DERHANSEN\SfEventMgt\SpamChecks\Exceptions\SpamCheckNotFoundException
165
     * @return bool
166
     */
167
    protected function isSpamCheckFailed(Registration $registration, array $settings): bool
168
    {
169
        $spamCheckService = new SpamCheckService(
170
            $registration,
171
            $settings,
172
            GeneralUtility::_GPmerged('tx_sfeventmgt_pievent')
173
        );
174
175
        return $spamCheckService->isSpamCheckFailed();
176
    }
177
178
    /**
179
     * Returns a validator object depending on the given type of the property
180
     *
181
     * @param string $type Type
182
     * @param string $field The field
183
     *
184
     * @return AbstractValidator
185
     */
186
    protected function getValidator($type, $field)
187
    {
188
        switch ($type) {
189
            case 'boolean':
190
                /** @var BooleanValidator $validator */
191
                $validator = $this->objectManager->get(
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

191
                $validator = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
192
                    BooleanValidator::class,
193
                    ['is' => true]
194
                );
195
                break;
196
            default:
197
                if ($field == 'recaptcha') {
198
                    /** @var \DERHANSEN\SfEventMgt\Validation\Validator\RecaptchaValidator $validator */
199
                    $validator = $this->objectManager->get(RecaptchaValidator::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

199
                    $validator = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(RecaptchaValidator::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
200
                } else {
201
                    /** @var \TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator $validator */
202
                    $validator = $this->objectManager->get(NotEmptyValidator::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

202
                    $validator = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(NotEmptyValidator::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
203
                }
204
        }
205
206
        return $validator;
207
    }
208
}
209