Passed
Push — master ( 532256...bd524b )
by Faiq
07:50 queued 03:38
created

ThreadController::index()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 3
nop 1
dl 0
loc 27
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Thread;
6
use App\ThreadTopic;
7
use Illuminate\Support\Facades\Auth;
8
9
class ThreadController extends Controller
10
{
11
    public function __construct()
12
    {
13
        $this->middleware('auth:admin', ['except' => [
14
            'index', 'show'
15
        ]]);
16
    }
17
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @param $query
22
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
23
     */
24
    public function index($query)
25
    {
26
        $threads = null;
27
        $count = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
28
        if($query == "all") {
29
            $threads = Thread::orderBy('created_at', 'desc')->paginate(15);
30
        } elseif($query == "answered") {
31
            $threads = Thread::where('status', true)
32
                            ->orderBy('created_at', 'desc')
33
                            ->paginate(15);
34
        } else {
35
            $threads = Thread::where('status', false)
36
                            ->orderBy('created_at', 'desc')
37
                            ->paginate(15);
38
        }
39
40
        $data = [
41
            'threads' => $threads,
42
            'query' => $query,
43
            'count' => [
44
                'all' => count(Thread::all()),
45
                'answered' => count(Thread::where('status', true)->get()),
46
                'unanswered' => count(Thread::where('status', false)->get())
47
            ]
48
        ];
49
50
        return view('pages.thread')->with('data', $data);
51
    }
52
53
54
    /**
55
     * Display the specified resource.
56
     *
57
     * @param  int  $id
58
     * @return \Illuminate\Http\Response
59
     */
60
    public function show($id)
61
    {
62
        $thread = Thread::find($id);
63
        if(!$thread) {
64
            abort(404);
65
        }
66
67
        $doctor = Auth::guard('doctor')->user();
68
        $data = [
69
            'thread' => $thread,
70
            'doctor' => $doctor
71
        ];
72
        return view('pages.ext.view-thread')->with('data', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.ext.v...')->with('data', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
73
    }
74
75
76
    /**
77
     * Remove the specified resource from storage.
78
     *
79
     * @param  int  $id
80
     * @return \Illuminate\Http\Response
81
     */
82
    public function destroy($id)
83
    {
84
        $thread = Thread::find($id);
85
        if($thread->delete() && $this->deleteTopic($thread->id_topic)) {
86
            return redirect(route('admin.thread.index', ['query' => "all"]))->with('success', 'Diskusi dihapus !');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('a...', 'Diskusi dihapus !') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
87
        }
88
        return redirect()->back()->with('failed', 'Gagal menghapus diskusi.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...al menghapus diskusi.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
89
    }
90
91
92
    /**
93
     * Delete topic with given id
94
     *
95
     * @param $id
96
     * @return bool
97
     */
98
    public function deleteTopic($id)
99
    {
100
        $topic = ThreadTopic::find($id);
101
        if($topic->delete()) {
102
            return true;
103
        }
104
        return false;
105
    }
106
}
107