Passed
Push — master ( d58bf8...6af014 )
by Faiq
04:49
created

DocController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Articles;
6
use App\DoctorDetail;
7
use App\Hospital;
8
use App\Thread;
9
use Carbon\Carbon;
10
use App\City;
11
use App\DoctorSpecialization;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Auth;
14
use Illuminate\Support\Facades\Hash;
15
use Illuminate\Support\Facades\Storage;
16
17
class DocController extends Controller
18
{
19
    /**
20
     * Create a new controller instance
21
     *
22
     * DocController constructor.
23
     * @return void
24
     */
25
    public function __construct()
26
    {
27
        $this->middleware('auth:doctor');
28
    }
29
30
    /**
31
     * Show dashboard page
32
     *
33
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
34
     * @throws \Exception
35
     */
36
    public function dashboard()
37
    {
38
        $doctor = $this->currentUser();
39
40
        $since = new Carbon(Auth::guard('doctor')->user()->created_at);
41
        $data = [
42
            'doctor' => $doctor,
43
            'articles' => count(Articles::all()),
44
            'threads' => count(Thread::all()),
45
            'role' => session('role'),
46
            'since' => $since,
47
            'warning' => null
48
49
        ];
50
51
        if( $doctor->city_id == null ||
52
            $doctor->gender == null ||
53
            $doctor->biography == null ||
54
            $doctor->profile_picture == 'user-default.jpg') {
55
56
            $data['warning'] = 'Sepertinya anda belum melengkapi data diri anda, segera lengkapi data diri anda.';
57
        }
58
        return view('pages.dashboard')->with('data', $data);
59
    }
60
61
    /**
62
     * Show profile page
63
     *
64
     * @param $id
65
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
66
     */
67
    public function profile($id)
68
    {
69
        $doctor = $this->currentUser();
70
        if($doctor->id == $id) {
71
            $data = [
72
                'doctor' => $doctor
73
            ];
74
75
            return view('pages.profile')->with('data', $data);
76
        }
77
        return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.');
78
    }
79
80
    /**
81
     * Show edit profile form
82
     *
83
     * @param $id
84
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
85
     */
86
    public function edit($id)
87
    {
88
        $doctor = $this->currentUser();
89
        if($doctor->id == $id) {
90
            $specialization = DoctorSpecialization::pluck('name', 'id');
91
            $cities = City::pluck('name', 'id');
92
93
            $data = [
94
                'doctor' => $doctor,
95
                'specialization' => $specialization,
96
                'cities' => $cities
97
            ];
98
99
            return view('pages.profile-edit')->with('data', $data);
100
        }
101
        return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.');
102
    }
103
104
    /**
105
     * Update profile
106
     *
107
     * @param Request $request
108
     * @param $id
109
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
110
     * @throws \Illuminate\Validation\ValidationException
111
     */
112
    public function update(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

112
    public function update(Request $request, /** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
        $this->validate($request, [
115
            'profile_picture' => 'image|nullable|max:3999',
116
            'name' => 'required',
117
            'email' => 'required|email',
118
            'gender' => 'required',
119
            'city_id' => 'required',
120
            'specialization_id' => 'required',
121
            'biography' => 'required|min:200'
122
        ]);
123
124
        $doctor = $this->currentUser();
125
        $img = null;
126
127
        if($request->hasFile('profile_picture')) {
128
129
            if( $doctor->profile_picture != "user-default.jpg") {
130
                Storage::delete('public/user_images/'.$doctor->profile_picture);
131
            }
132
133
            // Get Filename.ext
134
            $fileNameWExt = $request->file('profile_picture')->getClientOriginalName();
135
            // Get Filename
136
            $fileName = pathinfo($fileNameWExt, PATHINFO_FILENAME);
137
            // Get ext
138
            $ext = $request->file('profile_picture')->getClientOriginalExtension();
139
            // Filename to Store
140
            $img = $fileName.'_'.time().'.'.$ext;
141
            // Upload Image
142
            $path = $request->file('profile_picture')->storeAs('public/user_images', $img);
0 ignored issues
show
Unused Code introduced by
The assignment to $path is dead and can be removed.
Loading history...
143
        }
144
145
        $doctor->name = $request->input('name');
146
        $doctor->email = $request->input('email');
147
        $doctor->city_id = $request->input('city_id');
148
        $doctor->gender = $request->input('gender');
149
        $doctor->specialization_id = $request->input('specialization_id');
150
        $doctor->biography = $request->input('biography');
151
        if($request->hasFile('profile_picture')) {
152
            $doctor->profile_picture = $img;
153
        }
154
155
        if($doctor->save()) {
156
            return redirect(route('doctor.profile', $doctor->id))->with('success', 'Profil berhasil diperbarui !');
157
        }
158
159
        return redirect(route('doctor.profile.edit', $doctor->id))->with('failed', 'Pembaruan profil gagal !');
160
    }
161
162
    /**
163
     * Show edit password form
164
     *
165
     * @param $id
166
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
167
     */
168
    public function editPass($id)
169
    {
170
        $doctor = $this->currentUser();
171
        if($doctor->id == $id) {
172
            $data = [
173
                'doctor' => $doctor
174
            ];
175
            return view('pages.profile-password')->with('data', $data);
176
        }
177
178
        return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.');
179
    }
180
181
    /**
182
     * Change current password
183
     *
184
     * @param Request $request
185
     * @param $id
186
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
187
     * @throws \Illuminate\Validation\ValidationException
188
     */
189
    public function updatePass(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

189
    public function updatePass(Request $request, /** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
    {
191
        $doctor = $this->currentUser();
192
        if($this->validatePass($request->input('old_password'))) {
193
            if($request->input('old_password') == $request->input('new_password')) {
194
                return redirect(route('doctor.password.edit', $doctor->id))->with('warning', 'Password baru tidak boleh sama dengan Password lama.');
195
            }
196
197
            $this->validate($request, [
198
                'old_password' => 'required|min:6',
199
                'new_password' => 'required_with:password_confirmation|same:password_confirmation|min:6',
200
                'password_confirmation' => 'required|min:6'
201
            ]);
202
203
            $doctor->password = Hash::make($request->input('new_password'));
204
            $doctor->save();
205
206
            return redirect(route('doctor.profile', $doctor->id))->with('success', 'Password berhasil diubah !');
207
        }
208
        return redirect(route('doctor.password.edit', $doctor->id))->with('failed', 'Password lama tidak cocok.');
209
    }
210
211
    /**
212
     * Remove current used profile picture
213
     *
214
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
215
     */
216
    public function removeImage()
217
    {
218
        $doctor = $this->currentUser();
219
        if($doctor->profile_picture != "user-default.jpg") {
220
            Storage::delete('public/user_images/'.$doctor->profile_picture);
221
        }
222
223
        $doctor->profile_picture = "user-default.jpg";
224
        if($doctor->save()) {
225
            return redirect(route('doctor.profile.edit', $doctor->id))->with('success', 'Foto profil berhasil dihapus !');
226
        }
227
        return redirect(route('doctor.profile.edit', $doctor->id))->with('failed', 'Gagal menghapus foto profil.');
228
    }
229
230
    /**
231
     * Delete account
232
     *
233
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
234
     */
235
    public function destroy()
236
    {
237
        $doctor = $this->currentUser();
238
        if($doctor->delete()) {
239
            session()->flush();
240
            return redirect(route('doctor.login'))->with('success', 'Akun telah dihapus !');
241
        }
242
        return redirect(route('doctor.dashboard'))->with('failed', 'Penghapusan akun gagal.');
243
    }
244
245
    /**
246
     * Show Hospital page on profile
247
     *
248
     * @param $id
249
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
250
     */
251
    public function hospital($id)
252
    {
253
        $doctor = $this->currentUser();
254
        if($doctor->id == $id) {
255
            $data = [
256
                'doctor' => $doctor,
257
                'hospitals' => Hospital::where('city_id', $doctor->city_id)->paginate(5),
258
                'detail' => DoctorDetail::where('doctor_id', $doctor->city_id)
259
            ];
260
261
            return view('pages.profile-hospital')->with('data', $data);
262
        }
263
        return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.');
264
    }
265
266
    public function regHospital(Request $request)
267
    {
268
        $this->validate($request, [
269
            'hospital_id' => 'required'
270
        ]);
271
272
        $dd = new DoctorDetail;
273
        $dd->doctor_id = $this->currentUser()->id;
274
        $dd->hospital_id = $request->input('hospital_id');
275
276
        if($dd->save()) {
277
            return redirect(route('doctor.profile.hospital', $this->currentUser()->id))->with('success', 'Rumah Sakit baru ditambahkan !');
278
        }
279
        return redirect(route('doctor.profile.hospital', $this->currentUser()->id))->with('failed', 'Gagal menambah Rumah Sakit.');
280
    }
281
282
    public function unregHospital($doctorId, $hospitalId)
283
    {
284
        if($this->currentUser()->id != $doctorId) {
285
            return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.');
286
        }
287
        $dd = DoctorDetail::where('doctor_id', $this->currentUser()->id)
288
            ->where('hospital_id', $hospitalId);
289
        if($dd != null) {
290
            if($dd->delete()) {
291
                return redirect(route('doctor.profile.hospital', $this->currentUser()->id))->with('success', 'Rumah Sakit dihapus !');
292
            }
293
            return redirect(route('doctor.profile.hospital', $this->currentUser()->id))->with('failed', 'Gagal menghapus Rumah Sakit, Data tidak ditemukan.');
294
        }
295
        return redirect(route('doctor.profile.hospital', $this->currentUser()->id))->with('failed', 'Error !, Telah terjadi kesalahan.');
296
    }
297
298
    /**
299
     * Get current logged in Doctor
300
     *
301
     * @return mixed
302
     */
303
    private function currentUser()
304
    {
305
        return Auth::guard('doctor')->user();
306
    }
307
308
    /**
309
     * Validate old password
310
     *
311
     * @param string $oldPassword
312
     * @return bool
313
     */
314
    private function validatePass(string $oldPassword)
315
    {
316
        $doctor = $this->currentUser();
317
        if(Hash::check($oldPassword, $doctor->password)) {
318
            return true;
319
        }
320
321
        return false;
322
    }
323
324
}
325