Module::init()   B
last analyzed

Complexity

Conditions 5
Paths 13

Size

Total Lines 168
Code Lines 104

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 104
dl 0
loc 168
rs 7.6888
c 0
b 0
f 0
cc 5
nc 13
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Auth;
4
5
use Drone\Dom\Element\Form;
6
Use Drone\Mvc\AbstractionModule;
7
use Drone\Mvc\AbstractionController;
8
use Drone\Mvc\Layout;
9
use Drone\Validator\FormValidator;
10
use Drone\Util\ArrayDimension;
11
12
class Module extends AbstractionModule
13
{
14
    public function init(AbstractionController $c)
15
    {
16
        $config = $this->getUserConfig();
17
18
        $_config = ArrayDimension::toUnidimensional($config, "_");
19
20
        $this->setTranslator($c);
21
22
        # config constraints
23
24
        $components = [
25
            "attributes" => [
26
                "project_name" => [
27
                    "required"  => true,
28
                    "type"      => "text",
29
                    "minlength" => 2,
30
                    "maxlength" => 60
31
                ],
32
                "mail_checking_enabled" => [
33
                    "required" => true,
34
                    "type"     => "text"
35
                ],
36
                "mail_checking_from" => [
37
                    "required"  => false,
38
                    "type"      => "email"
39
                ],
40
                "authentication_method" => [
41
                    "required"  => true,
42
                    "type"      => "text"
43
                ],
44
                "authentication_key" => [
45
                    "required"  => true,
46
                    "type"      => "text",
47
                    "minlength" => 1
48
                ],
49
                "authentication_type" => [
50
                    "required"  => true,
51
                    "type"      => "text"
52
                ],
53
                "authentication_gateway_entity" => [
54
                    "required"  => false,
55
                    "type"      => "text"
56
                ],
57
                "authentication_gateway_credentials_username" => [
58
                    "required"  => false,
59
                    "type"      => "text"
60
                ],
61
                "authentication_gateway_credentials_password" => [
62
                    "required"  => false,
63
                    "type"      => "text"
64
                ],
65
                "authorization_enabled" => [
66
                    "required" => true,
67
                    "type"     => "text"
68
                ],
69
                "database_prefix" => [
70
                    "required"  => false,
71
                    "type"      => "text"
72
                ],
73
                "redirect" => [
74
                    "required"  => true,
75
                    "type"      => "text"
76
                ]
77
            ],
78
        ];
79
80
        $options = [
81
            "project" => [
82
                "label"      => "project -> name"
83
            ],
84
            "mail_checking_enabled" => [
85
                "label"      => "mail -> checking -> enabled",
86
                "validators" => [
87
                    "InArray"  => ["haystack" => ['Y', 'N']]
88
                ]
89
            ],
90
            "mail_checking_from" => [
91
                "label"      => "mail -> checking -> from"
92
            ],
93
            "authentication_method" => [
94
                "label"      => "authentication -> method",
95
                "validators" => [
96
                    "InArray"  => ["haystack" => ['_COOKIE', '_SESSION']]
97
                ]
98
            ],
99
            "authentication_key" => [
100
                "label"      => "authentication -> key",
101
            ],
102
            "authentication_type" => [
103
                "label"      => "authentication -> type",
104
                "validators" => [
105
                    "InArray"  => ["haystack" => ['db_table', 'db_user']]
106
                ]
107
            ],
108
            "authentication_gateway_entity" => [
109
                "label"      => "authentication -> gateway -> entity"
110
            ],
111
            "authentication_gateway_credentials_username" => [
112
                "label"      => "authentication -> gateway -> credentials -> username"
113
            ],
114
            "authentication_gateway_credentials_pasword" => [
115
                "label"      => "authentication -> gateway -> credentials -> password"
116
            ],
117
            "authorization_enabled" => [
118
                "label"      => "authorization -> enabled",
119
                "validators" => [
120
                    "InArray"  => ["haystack" => ['Y', 'N']]
121
                ]
122
            ],
123
            "database_prefix" => [
124
                "label"      => "database -> prefix"
125
            ],
126
            "redirect" => [
127
                "label"      => "redirect"
128
            ],
129
        ];
130
131
        $form = new Form($components);
132
        $form->fill($_config);
133
134
        $validator = new FormValidator($form, $options);
135
        $validator->validate();
136
137
        $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...
138
139
        try
140
        {
141
            if (!$validator->isValid())
142
            {
143
                $data["messages"] = $validator->getMessages();
144
                throw new \Exception("Module config errros in user.config!", 300);
145
            }
146
        }
147
        catch (\Exception $e)
148
        {
149
            $file = str_replace('\\', '', __CLASS__);
150
            $storage = new \Drone\Exception\Storage("cache/$file.json");
151
152
            # stores the error code
153
            if (($errorCode = $storage->store($e)) === false)
0 ignored issues
show
introduced by
The condition $errorCode = $storage->store($e) === false is always true.
Loading history...
154
            {
155
                $errors = $storage->getErrors();
156
157
                # if error storing is not possible, handle it (internal app error)
158
                //$this->handleErrors($errors, __METHOD__);
159
            }
160
161
            $data["code"]    = $errorCode;
162
            $data["message"] = $e->getMessage();
163
164
            $config = include 'config/application.config.php';
165
            $data["dev_mode"] = $config["environment"]["dev_mode"];
166
167
            # stops current controller execution
168
            $c->stopExecution(false);
0 ignored issues
show
Unused Code introduced by
The call to Drone\Mvc\AbstractionController::stopExecution() has too many arguments starting with false. ( Ignorable by Annotation )

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

168
            $c->/** @scrutinizer ignore-call */ 
169
                stopExecution(false);

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. Please note the @ignore annotation hint above.

Loading history...
169
170
            # loads error view
171
            $layoutManager = new Layout();
172
            $layoutManager->setBasePath($c->getBasePath());
173
174
            $layoutManager->setView($this, "validation");
175
            $layoutManager->setParams($data);
176
177
            # for AJAX requests!
178
            if ($c->isXmlHttpRequest())
179
                $layoutManager->content();
180
            else
181
                $layoutManager->fromTemplate($this, 'blank');
182
        }
183
    }
184
185
    private function setTranslator(AbstractionController $c)
186
    {
187
        $config = include('config/application.config.php');
188
        $locale = $config["environment"]["locale"];
189
190
        $i18nTranslator = \Zend\I18n\Translator\Translator::factory(
191
            [
192
                'locale'  => "$locale",
193
                'translation_files' => [
194
                    [
195
                        "type" => 'phparray',
196
                        "filename" => __DIR__ . "/lang/$locale.php"
197
                    ]
198
                ]
199
            ]
200
        );
201
202
        $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...
203
    }
204
205
	public function getUserConfig()
206
	{
207
		return include __DIR__ . "/config/user.config.php";
208
	}
209
}