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

ThreadAnswerController::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Thread;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Auth;
8
9
class ThreadAnswerController extends Controller
10
{
11
    public function __construct()
12
    {
13
        $this->middleware('auth:doctor');
14
    }
15
16
    /**
17
     * Display a listing of the resource.
18
     *
19
     * @return \Illuminate\Http\Response
20
     */
21
    public function index($id)
22
    {
23
        $doctor = $this->currentUser();
24
        if($doctor->id != $id) {
25
            return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...akses laman tersebut.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
26
        }
27
28
        $data = [
29
            'doctor' => $doctor,
30
            'thread' => Thread::where('doctor_id', $doctor->id)->paginate(15)
31
        ];
32
        return view('pages.profile-thread')->with('data', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('pages.profi...')->with('data', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
33
    }
34
35
    /**
36
     * Store a newly created resource in storage.
37
     *
38
     * @param Request $request
39
     * @param $id
40
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
41
     * @throws \Illuminate\Validation\ValidationException
42
     */
43
    public function store(Request $request, $id)
44
    {
45
        $this->validate($request, [
46
            'answer' => 'required|min:150'
47
        ]);
48
49
        $doctor = $this->currentUser();
50
        $thread = Thread::find($id);
51
52
        $thread->doctor_id = $doctor->id;
53
        $thread->answer = $request->input('answer');
54
        $thread->status = true;
55
56
        if($thread->save()) {
57
            return redirect(route('doctor.thread.show', $id))->with('success', 'Jawaban terkirim !');
58
        }
59
        return redirect()->back()->with('failed', 'Gagal mengirim jawaban.');
60
    }
61
62
    /**
63
     * Show the form for editing the specified resource.
64
     *
65
     * @param $id
66
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
67
     */
68
    public function edit($id)
69
    {
70
        $thread = Thread::find($id);
71
        if(!$thread) {
72
            abort(404);
73
        }
74
75
        $doctor = $this->currentUser();
76
        $data = [
77
            'doctor' => $doctor,
78
            'thread' => $thread
79
        ];
80
81
        return view('pages.ext.edit-thread')->with('data', $data);
82
    }
83
84
    /**
85
     * Update the specified resource in storage.
86
     *
87
     * @param Request $request
88
     * @param $id
89
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
90
     * @throws \Illuminate\Validation\ValidationException
91
     */
92
    public function update(Request $request, $id)
93
    {
94
        $this->validate($request, [
95
            'answer' => 'required|min:150'
96
        ]);
97
98
        $thread = Thread::find($id);
99
        $thread->answer = $request->input('answer');
100
        $thread->status = true;
101
102
        if($thread->save()) {
103
            return redirect(route('doctor.thread.show', $id))->with('success', 'Perubahan jawaban terkirim !');
104
        }
105
        return redirect()->back()->with('failed', 'Gagal mengirim perubahan jawaban.');
106
    }
107
108
    /**
109
     * Remove the specified resource from storage.
110
     *
111
     * @param  int  $id
112
     * @return \Illuminate\Http\Response
113
     */
114
    public function destroy($id)
115
    {
116
        $thread = Thread::find($id);
117
        if($thread->doctor_id != $this->currentUser()->id) {
118
            return redirect()->back()->with('warning', 'Anda tidak berhak menghapus jawaban untuk diskusi tersebut.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...tuk diskusi tersebut.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
119
        }
120
121
        $thread->doctor_id = null;
122
        $thread->answer = null;
123
        $thread->status = false;
124
        if($thread->save()) {
125
            return redirect()->back()->with('success', 'Jawaban dihapus !');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...', 'Jawaban dihapus !') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
126
        }
127
        return redirect()->back()->with('failed', 'Gagal mengahapus jawaban.');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...l mengahapus jawaban.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
128
    }
129
130
    /**
131
     * @return mixed
132
     */
133
    private function currentUser()
134
    {
135
        return Auth::guard('doctor')->user();
136
    }
137
}
138