Completed
Push — feature/spam-protection ( be04ad )
by Torben
02:50
created

RegistrationValidator::isSpamCheckFailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\BooleanValidator;
17
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
18
19
/**
20
 * RegistrationValidator
21
 *
22
 * @author Torben Hansen <[email protected]>
23
 */
24
class RegistrationValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
25
{
26
    /**
27
     * Configuration Manager
28
     *
29
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
30
     */
31
    protected $configurationManager;
32
33
    /**
34
     * Object Manager
35
     *
36
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
37
     */
38
    protected $objectManager;
39
40
    /**
41
     * DI for $configurationManager
42
     *
43
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
44
     */
45
    public function injectConfigurationManager(
46
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
47
    ) {
48
        $this->configurationManager = $configurationManager;
49
    }
50
51
    /**
52
     * DI for $objectManager
53
     *
54
     * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
55
     */
56
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
57
    {
58
        $this->objectManager = $objectManager;
59
    }
60
61
    /**
62
     * Validates the given registration according to required fields set in plugin
63
     * settings. For boolean fields, the booleanValidator is used and it is assumed,
64
     * that boolean fields must have the value "TRUE" (for checkboxes)
65
     *
66
     * @param Registration $value Registration
67
     *
68
     * @return bool
69
     */
70
    protected function isValid($value)
71
    {
72
        $settings = $this->configurationManager->getConfiguration(
73
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
74
            'SfEventMgt',
75
            'Pievent'
76
        );
77
78
        $spamSettings = $settings['registration']['spamCheck'] ?? [];
79
        if ((bool)$spamSettings['enabled'] && $this->isSpamCheckFailed($value, $spamSettings)) {
80
            $message = $this->translateErrorMessage('registration.spamCheckFailed', 'SfEventMgt');
81
            $error = new Error($message, 1578855253965);
82
            $this->result->forProperty('spamCheck')->addError($error);
83
            return false;
84
        }
85
86
        // If no required fields are set, then the registration is valid
87
        if ($settings['registration']['requiredFields'] === '' ||
88
            !isset($settings['registration']['requiredFields'])
89
        ) {
90
            return true;
91
        }
92
93
        $requiredFields = array_map('trim', explode(',', $settings['registration']['requiredFields']));
94
        $result = true;
95
96
        foreach ($requiredFields as $requiredField) {
97
            if ($value->_hasProperty($requiredField)) {
98
                $validator = $this->getValidator(gettype($value->_getProperty($requiredField)), $requiredField);
99
                /** @var \TYPO3\CMS\Extbase\Error\Result $validationResult */
100
                $validationResult = $validator->validate($value->_getProperty($requiredField));
101
                if ($validationResult->hasErrors()) {
102
                    $result = false;
103
                    foreach ($validationResult->getErrors() as $error) {
104
                        $this->result->forProperty($requiredField)->addError($error);
105
                    }
106
                }
107
            }
108
        }
109
110
        return $result;
111
    }
112
113
    /**
114
     * Processes the spam check and returns, if it failed or not
115
     *
116
     * @param Registration $registration
117
     * @param array $settings
118
     * @return bool
119
     * @throws \DERHANSEN\SfEventMgt\SpamChecks\Exceptions\SpamCheckNotFoundException
120
     */
121
    protected function isSpamCheckFailed(Registration $registration, array $settings): bool
122
    {
123
        $spamCheckService = new SpamCheckService(
124
            $registration,
125
            $settings,
126
            GeneralUtility::_GPmerged('tx_sfeventmgt_pievent')
127
        );
128
129
        return $spamCheckService->isSpamCheckFailed();
130
    }
131
132
    /**
133
     * Returns a validator object depending on the given type of the property
134
     *
135
     * @param string $type Type
136
     * @param string $field The field
137
     *
138
     * @return \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
139
     */
140
    protected function getValidator($type, $field)
141
    {
142
        switch ($type) {
143
            case 'boolean':
144
                /** @var \TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator $validator */
145
                $validator = $this->objectManager->get(
146
                    BooleanValidator::class,
147
                    ['is' => true]
148
                );
149
                break;
150
            default:
151
                if ($field == 'recaptcha') {
152
                    /** @var \DERHANSEN\SfEventMgt\Validation\Validator\RecaptchaValidator $validator */
153
                    $validator = $this->objectManager->get(RecaptchaValidator::class);
154
                } else {
155
                    /** @var \TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator $validator */
156
                    $validator = $this->objectManager->get(NotEmptyValidator::class);
157
                }
158
        }
159
160
        return $validator;
161
    }
162
}
163