Completed
Push — master ( 70dfe1...a97a1c )
by Torben
04:18
created

RegistrationValidator   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 8
dl 0
loc 184
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

6 Methods

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

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

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

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

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

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

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

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

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

Loading history...
205
                }
206
        }
207
208
        return $validator;
209
    }
210
}
211