Completed
Push — master ( b45908...599909 )
by
unknown
02:56 queued 01:17
created

PassingGradeController::show()   D

Complexity

Conditions 13
Paths 432

Size

Total Lines 61
Code Lines 41

Duplication

Lines 8
Ratio 13.11 %

Importance

Changes 0
Metric Value
cc 13
eloc 41
nc 432
nop 3
dl 8
loc 61
rs 4.474
c 0
b 0
f 0

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 Bantenprov\PassingGrade\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\DB;
9
use Bantenprov\PassingGrade\Facades\PassingGradeFacade;
10
11
/* Models */
12
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
13
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
14
use App\User;
15
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah;
16
17
/* Etc */
18
use Validator;
19
use Auth;
20
21
/**
22
 * The PassingGradeController class.
23
 *
24
 * @package Bantenprov\PassingGrade
25
 * @author  bantenprov <[email protected]>
26
 */
27
class PassingGradeController extends Controller
28
{
29
    protected $sekolah;
30
    protected $user;
31
    protected $admin_sekolah;
32
    protected $user_id;
33
34
    /**
35
     * Create a new controller instance.
36
     *
37
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
38
     */
39
    public function __construct()
40
    {
41
        $this->sekolah          = new Sekolah;
42
        $this->siswa            = new Siswa;
43
        $this->user             = new User;
44
        $this->admin_sekolah    = AdminSekolah::where('admin_sekolah_id', $this->user_id)->first();
45
        $this->user_id          = isset(Auth::User()->id) ? Auth::User()->id : null;
46
    }
47
48
    /**
49
     * Display a listing of the resource.
50
     *
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function index(Request $request)
54
    {
55 View Code Duplication
        if (request()->has('sort')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            list($sortCol, $sortDir) = explode('|', request()->sort);
57
58
            $query = $this->sekolah
59
                ->orderBy($sortCol, $sortDir);
60
        } else {
61
            $query = $this->sekolah
62
                ->orderBy('npsn', 'asc');
63
        }
64
65
        if ($request->exists('filter')) {
66
            $query->where(function($q) use($request) {
67
                $value = "%{$request->filter}%";
68
69
                if (Auth::User()->hasRole(['superadministrator'])) {
70
                    $q->where('nama', 'like', $value)
71
                        ->orWhere('npsn', 'like', $value);
72
                } else {
73
                    $q->where('nama', 'like', $value)
74
                        ->orWhere('npsn', 'like', $value);
75
                }
76
            });
77
        }
78
79
        if (Auth::User()->hasRole(['superadministrator'])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
80
            //
81
        } else if (!is_null($this->admin_sekolah)) {
82
            $query->where('id', $this->admin_sekolah->sekolah_id);
83
        }
84
85
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
86
        $response = $query->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->paginate($perPage);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
88
        if (is_null($this->admin_sekolah) && !Auth::User()->hasRole(['superadministrator'])) {
89
            $response   = $query->with(['user'])->paginate($perPage);
90
        } else {
91
            $response   = $query->with(['user'])->paginate($perPage);
92
        }
93
94
        return response()->json($response)
95
            ->header('Access-Control-Allow-Origin', '*')
96
            ->header('Access-Control-Allow-Methods', 'GET');
97
    }
98
99
    /**
100
     * Display the specified resource.
101
     *
102
     * @param  \App\Sekolah  $sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $sekolah. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
103
     * @return \Illuminate\Http\Response
104
     */
105
    public function show($id, $track = null, Request $request)
106
    {
107 View Code Duplication
        if (request()->has('sort')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
            list($sortCol, $sortDir) = explode('|', request()->sort);
109
110
            $query = $this->siswa->orderBy($sortCol, $sortDir);
111
        } else {
112
            $query = $this->siswa
113
                ->orderBy('nomor_un', 'asc');
114
        }
115
116
        if ($request->exists('filter')) {
117
            $query->where(function($q) use($request) {
118
                $value = "%{$request->filter}%";
119
120
                if (Auth::User()->hasRole(['superadministrator'])) {
121
                    $q->where('nomor_un', 'like', $value)
122
                        ->orWhere('nik', 'like', $value)
123
                        ->orWhere('nama_siswa', 'like', $value)
124
                        ->orWhere('no_kk', 'like', $value)
125
                        ->orWhere('nisn', 'like', $value);
126
                } else {
127
                    $q->where('nomor_un', 'like', $value)
128
                        ->orWhere('nik', 'like', $value)
129
                        ->orWhere('nama_siswa', 'like', $value)
130
                        ->orWhere('no_kk', 'like', $value)
131
                        ->orWhere('nisn', 'like', $value);
132
                }
133
            });
134
        }
135
136
        if (Auth::User()->hasRole(['superadministrator'])) {
137
            $query->where('sekolah_id', '=', $id);
138
        } else if (!is_null($this->admin_sekolah)) {
139
            $query->where('sekolah_id', '=', $this->admin_sekolah->sekolah_id);
140
        }
141
142
        if ($track == 'umum') {
143
            $query->whereIn('kegiatan_id', ['12', '22']);
144
        } else if ($track == 'prestasi') {
145
            $query->whereIn('kegiatan_id', ['11', '21']);
146
        }
147
148
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
149
150
        if (is_null($this->admin_sekolah) && !Auth::User()->hasRole(['superadministrator'])) {
151
            $response   = $query->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'akademik', 'nilai'])->paginate($perPage);
152
        } else {
153
            $response   = $query->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'akademik', 'nilai'])->paginate($perPage);
154
        }
155
156
        foreach ($response as $siswa) {
157
            if (isset($siswa->prodi_sekolah->program_keahlian)) {
158
                $siswa->prodi_sekolah->program_keahlian;
159
            }
160
        }
161
162
        return response()->json($response)
163
            ->header('Access-Control-Allow-Origin', '*')
164
            ->header('Access-Control-Allow-Methods', 'GET');
165
    }
166
}
167