Completed
Push — development ( 6b83c1...4f1043 )
by Torben
11:42
created

RegistrationValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 112
ccs 36
cts 38
cp 0.9474
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A injectConfigurationManager() 0 5 1
A injectObjectManager() 0 4 1
B isValid() 0 34 7
A getValidator() 0 22 3
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 TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
13
use TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator;
14
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
15
16
/**
17
 * RegistrationValidator
18
 *
19
 * @author Torben Hansen <[email protected]>
20
 */
21
class RegistrationValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
22
{
23
    /**
24
     * Configuration Manager
25
     *
26
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
27
     */
28
    protected $configurationManager;
29
30
    /**
31
     * Object Manager
32
     *
33
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
34
     */
35
    protected $objectManager;
36
37
    /**
38
     * DI for $configurationManager
39
     *
40
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
41
     */
42
    public function injectConfigurationManager(
43
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
44
    ) {
45
        $this->configurationManager = $configurationManager;
46
    }
47
48
    /**
49
     * DI for $objectManager
50
     *
51
     * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
52
     */
53
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
54
    {
55 9
        $this->objectManager = $objectManager;
56
    }
57 9
58 9
    /**
59 9
     * Validates the given registration according to required fields set in plugin
60
     * settings. For boolean fields, the booleanValidator is used and it is assumed,
61 9
     * that boolean fields must have the value "TRUE" (for checkboxes)
62
     *
63
     * @param Registration $value Registration
64 9
     *
65 8
     * @return bool
66 9
     */
67 2
    protected function isValid($value)
68
    {
69
        $settings = $this->configurationManager->getConfiguration(
70 7
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
71 7
            'SfEventMgt',
72
            'Pievent'
73 7
        );
74 7
75 6
        // If no required fields are set, then the registration is valid
76
        if ($settings['registration']['requiredFields'] === '' ||
77 6
            !isset($settings['registration']['requiredFields'])
78 6
        ) {
79 2
            return true;
80 2
        }
81 2
82 2
        $requiredFields = array_map('trim', explode(',', $settings['registration']['requiredFields']));
83 2
        $result = true;
84 6
85 7
        foreach ($requiredFields as $requiredField) {
86
            if ($value->_hasProperty($requiredField)) {
87 7
                $validator = $this->getValidator(gettype($value->_getProperty($requiredField)), $requiredField);
88
                /** @var \TYPO3\CMS\Extbase\Error\Result $validationResult */
89
                $validationResult = $validator->validate($value->_getProperty($requiredField));
90
                if ($validationResult->hasErrors()) {
91
                    $result = false;
92
                    foreach ($validationResult->getErrors() as $error) {
93
                        $this->result->forProperty($requiredField)->addError($error);
94
                    }
95
                }
96
            }
97
        }
98 2
99
        return $result;
100
    }
101 2
102
    /**
103 1
     * Returns a validator object depending on the given type of the property
104 1
     *
105 1
     * @param string $type Type
106 1
     * @param string $field The field
107 1
     *
108 1
     * @return \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
109 1
     */
110
    protected function getValidator($type, $field)
111
    {
112
        switch ($type) {
113
            case 'boolean':
114 1
                /** @var \TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator $validator */
115
                $validator = $this->objectManager->get(
116 1
                    BooleanValidator::class,
117 2
                    ['is' => true]
0 ignored issues
show
Unused Code introduced by
The call to ObjectManagerInterface::get() has too many arguments starting with array('is' => true).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
118
                );
119
                break;
120
            default:
121
                if ($field == 'recaptcha') {
122
                    /** @var \DERHANSEN\SfEventMgt\Validation\Validator\RecaptchaValidator $validator */
123
                    $validator = $this->objectManager->get(RecaptchaValidator::class);
124
                } else {
125
                    /** @var \TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator $validator */
126
                    $validator = $this->objectManager->get(NotEmptyValidator::class);
127
                }
128
        }
129
130
        return $validator;
131
    }
132
}
133