Completed
Push — typo3-10-compatibility ( a024c4...0a9722 )
by Torben
02:36
created

RegistrationValidator::validateDefaultFields()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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