Completed
Push — development ( deb2fc...28a639 )
by Torben
04:28 queued 25s
created

injectConfigurationManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace DERHANSEN\SfEventMgt\Validation\Validator;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
18
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19
use TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator;
20
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
21
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
22
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
23
24
/**
25
 * RegistrationValidator
26
 *
27
 * @author Torben Hansen <[email protected]>
28
 */
29
class RegistrationValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
30
{
31
32
    /**
33
     * Configuration Manager
34
     *
35
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
36
     */
37
    protected $configurationManager;
38
39
    /**
40
     * Object Manager
41
     *
42
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
43
     */
44
    protected $objectManager;
45
46
    /**
47
     * DI for $configurationManager
48
     *
49
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
50
     */
51
    public function injectConfigurationManager(
52
        \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager
53
    ) {
54
        $this->configurationManager = $configurationManager;
55 9
    }
56
57 9
    /**
58 9
     * DI for $objectManager
59 9
     *
60
     * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
61 9
     */
62
    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
63
    {
64 9
        $this->objectManager = $objectManager;
65 8
    }
66 9
67 2
    /**
68
     * Validates the given registration according to required fields set in plugin
69
     * settings. For boolean fields, the booleanValidator is used and it is assumed,
70 7
     * that boolean fields must have the value "TRUE" (for checkboxes)
71 7
     *
72
     * @param Registration $value Registration
73 7
     *
74 7
     * @return bool
75 6
     */
76
    protected function isValid($value)
77 6
    {
78 6
        $settings = $this->configurationManager->getConfiguration(
79 2
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
80 2
            'SfEventMgt',
81 2
            'Pievent'
82 2
        );
83 2
84 6
        // If no required fields are set, then the registration is valid
85 7
        if ($settings['registration']['requiredFields'] === '' ||
86
            !isset($settings['registration']['requiredFields'])
87 7
        ) {
88
            return true;
89
        }
90
91
        $requiredFields = array_map('trim', explode(',', $settings['registration']['requiredFields']));
92
        $result = true;
93
94
        foreach ($requiredFields as $requiredField) {
95
            if ($value->_hasProperty($requiredField)) {
96
                $validator = $this->getValidator(gettype($value->_getProperty($requiredField)), $requiredField);
97
                /** @var \TYPO3\CMS\Extbase\Error\Result $validationResult */
98 2
                $validationResult = $validator->validate($value->_getProperty($requiredField));
99
                if ($validationResult->hasErrors()) {
100
                    $result = false;
101 2
                    foreach ($validationResult->getErrors() as $error) {
102
                        $this->result->forProperty($requiredField)->addError($error);
103 1
                    }
104 1
                }
105 1
            }
106 1
        }
107 1
108 1
        return $result;
109 1
    }
110
111
    /**
112
     * Returns a validator object depending on the given type of the property
113
     *
114 1
     * @param string $type Type
115
     * @param string $field The field
116 1
     *
117 2
     * @return \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
118
     */
119
    protected function getValidator($type, $field)
120
    {
121
        switch ($type) {
122
            case 'boolean':
123
                /** @var \TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator $validator */
124
                $validator = $this->objectManager->get(
125
                    BooleanValidator::class,
126
                    ['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...
127
                );
128
                break;
129
            default:
130
                if ($field == 'recaptcha') {
131
                    /** @var \DERHANSEN\SfEventMgt\Validation\Validator\RecaptchaValidator $validator */
132
                    $validator = $this->objectManager->get(RecaptchaValidator::class);
133
                } else {
134
                    /** @var \TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator $validator */
135
                    $validator = $this->objectManager->get(NotEmptyValidator::class);
136
                }
137
        }
138
        return $validator;
139
    }
140
}
141