Completed
Push — master ( 599909...5473e4 )
by
unknown
01:38
created

PassingGradeController::index()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 45
Code Lines 27

Duplication

Lines 9
Ratio 20 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 32
nop 1
dl 9
loc 45
rs 5.3846
c 0
b 0
f 0
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->user_id          = isset(Auth::User()->id) ?? null;
45
        $this->admin_sekolah    = AdminSekolah::where('admin_sekolah_id', $this->user_id)->first();
46
        $this->admin_sekolah_id = isset($this->admin_sekolah->sekolah_id) ?? null;
47
    }
48
49
    /**
50
     * Display a listing of the resource.
51
     *
52
     * @return \Illuminate\Http\Response
53
     */
54
    public function index(Request $request)
55
    {
56 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...
57
            list($sortCol, $sortDir) = explode('|', request()->sort);
58
59
            $query = $this->sekolah
60
                ->orderBy($sortCol, $sortDir);
61
        } else {
62
            $query = $this->sekolah
63
                ->orderBy('npsn', 'asc');
64
        }
65
66
        if ($request->exists('filter')) {
67
            $query->where(function($q) use($request) {
68
                $value = "%{$request->filter}%";
69
70
                if (Auth::User()->hasRole(['superadministrator'])) {
71
                    $q->where('nama', 'like', $value)
72
                        ->orWhere('npsn', 'like', $value);
73
                } else {
74
                    $q->where('nama', 'like', $value)
75
                        ->orWhere('npsn', 'like', $value);
76
                }
77
            });
78
        }
79
80
        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...
81
            //
82
        } else {
83
            $query->where('id', $this->admin_sekolah_id);
84
        }
85
86
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
87
        $response = $query->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->paginate($perPage);
88
89
        if (is_null($this->admin_sekolah) && !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...
90
            // $response = (object) [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
92
            //
93
        }
94
95
        return response()->json($response)
96
            ->header('Access-Control-Allow-Origin', '*')
97
            ->header('Access-Control-Allow-Methods', 'GET');
98
    }
99
100
    /**
101
     * Display the specified resource.
102
     *
103
     * @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...
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function show($id, $track = null, Request $request)
107
    {
108 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...
109
            list($sortCol, $sortDir) = explode('|', request()->sort);
110
111
            $query = $this->siswa->orderBy($sortCol, $sortDir);
112
        } else {
113
            $query = $this->siswa
114
                ->orderBy('nomor_un', 'asc');
115
        }
116
117
        if ($request->exists('filter')) {
118
            $query->where(function($q) use($request) {
119
                $value = "%{$request->filter}%";
120
121
                if (Auth::User()->hasRole(['superadministrator'])) {
122
                    $q->where('nomor_un', 'like', $value)
123
                        ->orWhere('nik', 'like', $value)
124
                        ->orWhere('nama_siswa', 'like', $value)
125
                        ->orWhere('no_kk', 'like', $value)
126
                        ->orWhere('nisn', 'like', $value);
127
                } else {
128
                    $q->where('nomor_un', 'like', $value)
129
                        ->orWhere('nik', 'like', $value)
130
                        ->orWhere('nama_siswa', 'like', $value)
131
                        ->orWhere('no_kk', 'like', $value)
132
                        ->orWhere('nisn', 'like', $value);
133
                }
134
            });
135
        }
136
137
        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...
138
            //
139
        } else {
140
            $query->where('sekolah_id', $this->admin_sekolah_id);
141
        }
142
143
        if ($track == 'umum') {
144
            $query->whereIn('kegiatan_id', ['12', '22']);
145
        } else if ($track == 'prestasi') {
146
            $query->whereIn('kegiatan_id', ['11', '21']);
147
        }
148
149
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
150
        $response   = $query->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'akademik', 'nilai'])->paginate($perPage);
151
152
        if (is_null($this->admin_sekolah) && !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...
153
            // $response = (object) [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
154
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

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

could be turned into

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

This is much more concise to read.

Loading history...
155
            //
156
        }
157
158
        foreach ($response as $siswa) {
159
            if (isset($siswa->prodi_sekolah->program_keahlian)) {
160
                $siswa->prodi_sekolah->program_keahlian;
161
            }
162
        }
163
164
        return response()->json($response)
165
            ->header('Access-Control-Allow-Origin', '*')
166
            ->header('Access-Control-Allow-Methods', 'GET');
167
    }
168
}
169