Passed
Push — master ( e6c475...db71b9 )
by Darío
04:47
created

Module::toFormConfig()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 1
dl 0
loc 20
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Connections;
4
5
use Drone\Mvc\AbstractionModule;
6
use Drone\Mvc\AbstractionController;
7
use Drone\Dom\Element\Form;
8
use Drone\Validator\FormValidator;
9
10
class Module extends AbstractionModule
11
{
12
	public function init(AbstractionController  $c)
13
	{
14
		$config = $this->getUserConfig("Auth");
15
		$_config = $this->toFormConfig($config);
16
17
		# config constraints
18
19
        $components = [
20
            "attributes" => [
21
                "project_name" => [
22
                    "required"  => true,
23
                    "type"      => "text",
24
                    "minlength" => 2,
25
                    "maxlength" => 60
26
                ],
27
                "mail_checking_from" => [
28
                    "required"  => true,
29
                    "type"      => "email"
30
                ],
31
                "authentication_method" => [
32
                    "required"  => true,
33
                    "type"      => "text"
34
                ],
35
                "authentication_key" => [
36
                    "required"  => true,
37
                    "type"      => "text"
38
                ],
39
                "redirect" => [
40
                    "required"  => true,
41
                    "type"      => "text"
42
                ]
43
            ],
44
        ];
45
46
        $options = [
47
            "project" => [
48
                "label"      => "project -> name"
49
            ],
50
            "mail_checking_from" => [
51
                "label"      => "mail -> checking -> from"
52
            ],
53
            "authentication_method" => [
54
                "label"      => "authentication -> method",
55
                "validators" => [
56
                    "InArray"  => ["haystack" => ['_COOKIE', '_SESSION']]
57
                ]
58
            ],
59
            "authentication_key" => [
60
                "label"      => "authentication -> key",
61
            ],
62
            "redirect" => [
63
                "label"      => "redirect"
64
            ],
65
        ];
66
67
        $form = new Form($components);
68
        $form->fill($_config);
0 ignored issues
show
Bug introduced by
It seems like $_config can also be of type mixed; however, parameter $values of Drone\Dom\Element\Form::fill() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $form->fill(/** @scrutinizer ignore-type */ $_config);
Loading history...
69
70
        $validator = new FormValidator($form, $options);
71
        $validator->validate();
72
73
        $data["validator"] = $validator;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
74
75
        # Validar formulario
76
        if (!$validator->isValid())
77
        {
78
            $data["messages"] = $validator->getMessages();
79
            throw new \Exception("Module config errros in user.config!", 300);
80
        }
81
82
        $method = $config["authentication"]["method"];
83
        $key    = $config["authentication"]["key"];
84
85
        switch ($method)
86
        {
87
            case '_COOKIE':
88
89
		        if (!array_key_exists($key, $_COOKIE) || empty($_COOKIE[$key]))
90
		            header("location: " . $c->basePath ."/public/". "Auth");
0 ignored issues
show
Bug introduced by
The property basePath is declared private in Drone\Mvc\AbstractionController and cannot be accessed from this context.
Loading history...
91
92
                break;
93
94
            case '_SESSION':
95
96
		        if (!array_key_exists($key, $_SESSION) || empty($_SESSION[$key]))
97
		            header("location: " . $c->basePath ."/public/". "Auth");
98
99
                break;
100
        }
101
102
        $this->setTranslator($c);
103
	}
104
105
    private function setTranslator(AbstractionController $c)
106
    {
107
        $config = include('config/application.config.php');
108
        $locale = $config["environment"]["locale"];
109
110
        $i18nTranslator = \Zend\I18n\Translator\Translator::factory(
111
            [
112
                'locale'  => "$locale",
113
                'translation_files' => [
114
                    [
115
                        "type" => 'phparray',
116
                        "filename" => __DIR__ . "/lang/$locale.php"
117
                    ]
118
                ]
119
            ]
120
        );
121
122
        $c->translator = new \Zend\Mvc\I18n\Translator($i18nTranslator);
0 ignored issues
show
Bug introduced by
The property translator does not seem to exist on Drone\Mvc\AbstractionController.
Loading history...
123
    }
124
125
	public function getUserConfig($module)
126
	{
127
		return include __DIR__ . "/../$module/config/user.config.php";
128
	}
129
130
	private function toFormConfig($config)
131
	{
132
		$new_config = [];
133
		$again = false;
134
135
		foreach ($config as $param => $configure)
136
		{
137
			if (is_array($configure))
138
			{
139
				foreach ($configure as $key => $value)
140
				{
141
					$again = true;
142
					$new_config[$param . "_" . $key] = $value;
143
				}
144
			}
145
			else
146
				$new_config[$param] = $configure;
147
		}
148
149
		return (!$again) ? $new_config : $this->toFormConfig($new_config);
150
	}
151
}