Store::__invoke()   B
last analyzed

Complexity

Conditions 10
Paths 104

Size

Total Lines 50
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 37
nc 104
nop 1
dl 0
loc 50
rs 7.6333
c 1
b 0
f 0

How to fix   Complexity   

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 App\Http\Controllers\Dna;
4
5
use App\Jobs\DnaMatching;
6
use App\Models\Dna;
7
use App\Traits\UniqueStringTrait;
8
use Auth;
9
use Illuminate\Http\Request;
10
use Illuminate\Routing\Controller;
11
12
class Store extends Controller
13
{
14
    use UniqueStringTrait;
15
16
    public function __invoke(Request $request)
17
    {
18
        $allowed = null;
19
        $role = \Auth::user()->role_id;
0 ignored issues
show
Bug introduced by
Accessing role_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
20
        $user_id = \Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $filename is dead and can be removed.
Loading history...
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
        }
67
    }
68
}
69