FilesController::index()   D
last analyzed

Complexity

Conditions 28
Paths 1

Size

Total Lines 98
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 28
eloc 70
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 98
rs 4.1666

How to fix   Long Method    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;
4
5
use App\Models\Upload;
6
use DateTime;
7
use Illuminate\Support\Facades\Auth;
8
use Yajra\DataTables\DataTables;
9
10
11
class FilesController extends Controller
12
{
13
14
    public function index()
15
    {
16
        return Datatables::of(Upload::with(['classRoom'])->where('security_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...
17
            ->editColumn('is_processed', function ($data) {
18
19
                $nowTime = \Carbon\Carbon::now();
20
                $to = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $nowTime);
21
                $from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $data->updated_at);
22
                $diff_in_hours = $to->diffInHours($from);
23
24
                if($diff_in_hours >= 2 && $data->is_processed == 3){
25
                    return "Terminated";
26
                }
27
                elseif ($data->is_processed === 1) {
28
                    return "Success";
29
                }elseif ($data->is_processed === 2){
30
                    return "Failed";
31
                }elseif($diff_in_hours < 2 && $data->is_processed == 3){
32
                    return "Processing";
33
                }elseif ($data->is_processed == 4){
34
                    return "Process Paused";
35
                }else{
36
                    return 'Pending';
37
                };
38
39
            })
40
            ->editColumn('is_email_sent', function ($data) {
41
                if ($data->is_email_sent == 1) {
42
                    return "Success";
43
                }elseif($data->is_email_sent == 2 ){
44
                    return 'Failed';
45
                }else{
46
                    return 'Pending';
47
                };
48
49
            })
50
            ->editColumn('update', function ($data) {
51
                if ((int)$data->update  == 0) {
52
                    return "No Processes";
53
                }elseif((int)$data->update == 1 ){
54
                    return 'Success';
55
                }elseif((int)$data->update == 2 ){
56
                    return 'Failed';
57
                }elseif((int)$data->update == 3 ){
58
                    return 'Partial Success';
59
                };
60
61
            })
62
            ->editColumn('insert', function ($data) {
63
                if ($data->insert == 0) {
64
                    return "No Processes";
65
                }elseif($data->insert == 1 ){
66
                    return 'Success';
67
                }elseif($data->insert == 2 ){
68
                    return 'Failed';
69
                }elseif($data->insert == 3 ){
70
                    return 'Partial Success';
71
                };
72
73
            })
74
            ->editColumn('filename', function ($data) {
75
                if(\App::environment('local') || \App::environment('stage')){
76
                    return '<a href="/download_file/'.$data->filename.'">'.$data->classRoom->name.'</a>';
77
78
                }else{
79
                    return '<a href="/bulk-upload/download_file/'.$data->filename.'">'.$data->classRoom->name.'</a>';
80
                }
81
82
            })
83
             ->editColumn('error', function ($data) {
84
                if(\App::environment('local') || \App::environment('stage')){
85
                    return '<a href="/download/'.$data->filename.'">'.$data->classRoom->name.'</a>';
86
87
                }else{
88
                    return '<a href="/bulk-upload/download/'.$data->filename.'">'.$data->classRoom->name.'</a>';
89
                }
90
91
            })->editColumn('actions', function ($data) {
92
93
                $nowTime = \Carbon\Carbon::now();
94
                $to = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $nowTime);
95
                $from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $data->updated_at);
96
                $diff_in_hours = $to->diffInHours($from);
97
98
                if($diff_in_hours >= 2 && $data->is_processed == 3){
99
                    return '<button onclick="updateProcess('.($data->id).',100)" class="btn btn-primary text-uppercase">reprocess</button>';
100
                }elseif ($data->is_processed == 1){
101
                    return '<div><h6>Processing <span class="badge badge-success text-uppercase">Successful</span></h6></div>';
102
                }elseif ($data->is_processed == 2){
103
                    return '<div><h6>Processing <span class="badge badge-danger text-uppercase">Failed</span></h6></div>';
104
                }elseif ($data->is_processed == 0){
105
                    return '<button onclick="updateProcess('.($data->id).',200)" class="btn btn-block btn-warning text-uppercase">pause</button>';
106
                }elseif ($data->is_processed == 4){
107
                    return '<button onclick="updateProcess('.($data->id).',100)" class="btn btn-block btn-success text-uppercase">resume</button>';
108
                }
109
            })
110
            ->rawColumns(['filename','error','actions'])
111
            ->make(true);
112
    }
113
114
    /**
115
     * Show the form for creating a new resource.
116
     *
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function create()
120
    {
121
        return view('displaydata');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('displaydata') returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
122
    }
123
}
124