Conditions | 10 |
Paths | 104 |
Total Lines | 50 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
16 | public function __invoke(Request $request) |
||
17 | { |
||
18 | $allowed = null; |
||
19 | $role = \Auth::user()->role_id; |
||
|
|||
20 | $user_id = \Auth::user()->id; |
||
21 | $dna = Dna::where('user_id', '=', $user_id)->count(); |
||
22 | if (in_array($role, [1, 2, 9, 10])) { |
||
23 | $allowed = true; |
||
24 | } |
||
25 | if (in_array($role, [4, 5, 6]) && $dna < 1) { |
||
26 | $allowed = true; |
||
27 | } |
||
28 | |||
29 | if (in_array($role, [7, 8]) && $dna < 5) { |
||
30 | $allowed = true; |
||
31 | } |
||
32 | if ($allowed === true) { |
||
33 | if ($request->hasFile('file')) { |
||
34 | if ($request->file('file')->isValid()) { |
||
35 | try { |
||
36 | $currentUser = Auth::user(); |
||
37 | $file_name = 'dna_'.$request->file('file')->getClientOriginalName().uniqid().'.'.$request->file('file')->extension(); |
||
38 | $request->file->storeAs('dna', $file_name); |
||
39 | define('STDIN', fopen('php://stdin', 'r')); |
||
40 | $random_string = $this->unique_random('dnas', 'name', 5); |
||
41 | $var_name = 'var_'.$random_string; |
||
42 | $filename = 'app/dna/'.$file_name; |
||
43 | $user_id = $currentUser->id; |
||
44 | $dna = new Dna(); |
||
45 | $dna->name = 'DNA Kit for user '.$user_id; |
||
46 | $dna->user_id = $user_id; |
||
47 | $dna->variable_name = $var_name; |
||
48 | $dna->file_name = $file_name; |
||
49 | $dna->save(); |
||
50 | DnaMatching::dispatch($currentUser, $var_name, $file_name); |
||
51 | |||
52 | return [ |
||
53 | 'message' => __('The dna was successfully created'), |
||
54 | 'redirect' => 'dna.edit', |
||
55 | 'param' => ['dna' => $dna->id], |
||
56 | ]; |
||
57 | } catch (\Exception $e) { |
||
58 | return $e->getMessage(); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | return ['File corrupted']; |
||
63 | } |
||
64 | |||
65 | return response()->json(['Not uploaded'], 422); |
||
66 | } |
||
69 |