Conditions | 16 |
Paths | 27 |
Total Lines | 83 |
Code Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
137 | public function import() |
||
138 | { |
||
139 | if (!empty($this->request->data['json'])) { |
||
140 | $this->loadModel('User.Acos'); |
||
141 | $json = $this->request->data['json']; |
||
142 | $dst = TMP . $json['name']; |
||
143 | if (file_exists($dst)) { |
||
144 | $file = new File($dst); |
||
145 | $file->delete(); |
||
146 | } |
||
147 | |||
148 | if (!move_uploaded_file($json['tmp_name'], $dst)) { |
||
149 | $this->Flash->danger(__d('user', 'File could not be uploaded, please check write permissions on /tmp directory.')); |
||
150 | } else { |
||
151 | $file = new File($dst); |
||
152 | $info = json_decode($file->read(), true); |
||
153 | $added = []; |
||
154 | $error = false; |
||
155 | |||
156 | if (is_array($info) && !empty($info)) { |
||
157 | foreach ($info as $role => $paths) { |
||
158 | if (!is_string($role)) { |
||
159 | $error = true; |
||
160 | $this->Flash->danger(__d('user', 'Given file seems to be corrupt.')); |
||
161 | break; |
||
162 | } |
||
163 | $role = $this->Acos->Roles |
||
164 | ->find() |
||
165 | ->where(['slug' => $role]) |
||
166 | ->limit(1) |
||
167 | ->first(); |
||
168 | |||
169 | if (!$role) { |
||
170 | continue; |
||
171 | } |
||
172 | |||
173 | if (is_array($paths)) { |
||
174 | foreach ($paths as $path) { |
||
175 | $nodes = $this->Acos->node($path); |
||
176 | |||
177 | if ($nodes) { |
||
178 | $leaf = $nodes->first(); |
||
179 | $exists = $this->Acos->Permissions |
||
180 | ->exists([ |
||
181 | 'aco_id' => $leaf->id, |
||
182 | 'role_id' => $role->id |
||
183 | ]); |
||
184 | if (!$exists) { |
||
185 | $newPermission = $this->Acos->Permissions->newEntity([ |
||
186 | 'aco_id' => $leaf->id, |
||
187 | 'role_id' => $role->id |
||
188 | ]); |
||
189 | if ($this->Acos->Permissions->save($newPermission)) { |
||
190 | $added[] = "<strong>{$role->name}</strong>: {$path}"; |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | } else { |
||
196 | $error = true; |
||
197 | $this->Flash->danger(__d('user', 'Given file seems to be corrupt.')); |
||
198 | break; |
||
199 | } |
||
200 | } |
||
201 | } else { |
||
202 | $error = true; |
||
203 | $this->Flash->danger(__d('user', 'Invalid file given.')); |
||
204 | } |
||
205 | |||
206 | if (!$error) { |
||
207 | if (!empty($added)) { |
||
208 | $imported = '<br />' . implode('<br />', $added); |
||
209 | $this->Flash->success(__d('user', 'The following entries were imported: {0}', $imported)); |
||
210 | } else { |
||
211 | $this->Flash->success(__d('user', 'Success, but nothing was imported')); |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | $this->title(__d('user', 'Import Permissions')); |
||
218 | $this->redirect($this->referer()); |
||
219 | } |
||
220 | } |
||
221 |