Completed
Pull Request — master (#168)
by
unknown
02:59
created

FormValidator   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 20
c 3
b 0
f 3
lcom 0
cbo 3
dl 0
loc 94
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validateSettingForm() 0 55 16
A validateProfileForm() 0 20 4
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\SettingsBundle\Service;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Yaml\Parser;
16
17
class FormValidator
18
{
19
    /**
20
     * Validates add or edit setting form
21
     *
22
     * @param Request $request
23
     *
24
     * @return array
25
     */
26
    public function validateSettingForm(Request $request)
27
    {
28
        $parser = new Parser();
29
        $return = [];
30
        $return['error'] = '';
31
        $return['name'] = $request->request->get('settingName');
32
        $return['type'] = $request->request->get('settingType');
33
        $return['description'] = $request->request->get('settingDescription');
34
        $profiles = $request->request->get('settingProfiles');
35
36
        is_string($profiles) ? $return['profiles'] = [$profiles] : $return['profiles'] = $profiles;
37
38
        if ($return['name'] == '') {
39
            $return['error'] = 'You must set a name to the setting. ';
40
        }
41
        if (count($return['profiles']) == 0) {
42
            $return['error'] = $return['error'].'At least 1 profile has to be set. ';
43
        }
44
        switch ($return['type']) {
45
            case 'bool':
46
                $request->request->get('setting-boolean') == 'true' ?
47
                    $return['value'] = true :
48
                    $return['value'] = false;
49
                break;
50
            case 'string':
51
                $return['value'] = $request->request->get('setting-default');
52
                if ($return['value'] == '') {
53
                    $return['error'] = $return['error'].'You must set a value to the setting. ';
54
                }
55
                break;
56
            case 'object':
57
                try {
58
                    $return['value'] = json_encode($parser->parse($request->request->get('setting-object')));
59
                } catch (\Exception $e) {
60
                    $return['error'] = $return['error'].'Passed setting value does not contain valid yaml. ';
61
                }
62
                if ($return['value'] == '') {
63
                    $return['error'] = $return['error'].'You must set a value to the setting. ';
64
                }
65
                break;
66
            case 'array':
67
                $return['value'] = [];
68
                foreach ($request->request->all() as $key => $item) {
69
                    if (preg_match('/setting-array_[0-9]*/', $key)) {
70
                        $return['value'][] = $item;
71
                    }
72
                }
73
                if (count($return['value']) == 0 || $return['value'][0] == '') {
74
                    $return['error'] = $return['error'].'You must set a value to the setting. ';
75
                }
76
                break;
77
        }
78
79
        return $return;
80
    }
81
82
    /**
83
     * Validates profile form
84
     *
85
     * @param Request $request
86
     * @param array   $profiles
87
     *
88
     * @return array
89
     */
90
    public function validateProfileForm(Request $request, array $profiles)
91
    {
92
        $return = [];
93
        $return['error'] = '';
94
        $return['name'] = $request->request->get('profileName');
95
        $return['description'] = $request->request->get('profileDescription');
96
97
        if ($return['name'] == '') {
98
            $return['error'] = 'You must set a unique name of the profile.';
99
            return $return;
100
        }
101
        foreach ($profiles as $profile) {
102
            if ($return['name'] == $profile['name']) {
103
                $return['error'] = 'The profile `'.$profile['name'].'` is already set.';
104
                return $return;
105
            }
106
        }
107
108
        return $return;
109
    }
110
}
111
112
113
114