Passed
Pull Request — master (#46)
by Faiq
08:16 queued 03:49
created

DocController::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

103
    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...
104
    {
105
        $this->validate($request, [
106
            'profile_picture' => 'image|nullable|max:3999',
107
            'name' => 'required',
108
            'email' => 'required|email',
109
            'gender' => 'required',
110
            'city_id' => 'required',
111
            'specialization_id' => 'required',
112
            'biography' => 'required|min:200'
113
        ]);
114
115
        $doctor = $this->currentUser();
116
        $img = null;
117
118
        if($request->hasFile('profile_picture')) {
119
120
            if( $doctor->profile_picture != "user-default.jpg") {
121
                Storage::delete('public/user_images/'.$doctor->profile_picture);
122
            }
123
124
            // Get Filename.ext
125
            $fileNameWExt = $request->file('profile_picture')->getClientOriginalName();
126
            // Get Filename
127
            $fileName = pathinfo($fileNameWExt, PATHINFO_FILENAME);
128
            // Get ext
129
            $ext = $request->file('profile_picture')->getClientOriginalExtension();
130
            // Filename to Store
131
            $img = $fileName.'_'.time().'.'.$ext;
132
            // Upload Image
133
            $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...
134
        }
135
136
        $doctor->name = $request->input('name');
137
        $doctor->email = $request->input('email');
138
        $doctor->city_id = $request->input('city_id');
139
        $doctor->gender = $request->input('gender');
140
        $doctor->specialization_id = $request->input('specialization_id');
141
        $doctor->biography = $request->input('biography');
142
        if($request->hasFile('profile_picture')) {
143
            $doctor->profile_picture = $img;
144
        }
145
146
        if($doctor->save()) {
147
            return redirect(route('doctor.profile', $doctor->id))->with('success', 'Profil berhasil diperbarui !');
148
        }
149
150
        return redirect(route('doctor.profile.edit', $doctor->id))->with('failed', 'Pembaruan profil gagal !');
151
    }
152
153
    /**
154
     * Show edit password form
155
     *
156
     * @param $id
157
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
158
     */
159
    public function editPass($id)
160
    {
161
        $doctor = $this->currentUser();
162
        if($doctor->id == $id) {
163
            $data = [
164
                'doctor' => $doctor
165
            ];
166
            return view('pages.profile-password')->with('data', $data);
167
        }
168
169
        return redirect()->back()->with('warning', 'Anda tidak berhak mengakses laman tersebut.');
170
    }
171
172
    /**
173
     * @param Request $request
174
     * @param $id
175
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
176
     * @throws \Illuminate\Validation\ValidationException
177
     */
178
    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

178
    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...
179
    {
180
        $doctor = $this->currentUser();
181
        if($this->validatePass($request->input('old_password'))) {
182
            if($request->input('old_password') == $request->input('new_password')) {
183
                return redirect(route('doctor.password.edit', $doctor->id))->with('warning', 'Password baru tidak boleh sama dengan Password lama.');
184
            }
185
186
            $this->validate($request, [
187
                'old_password' => 'required|min:6',
188
                'new_password' => 'required_with:password_confirmation|same:password_confirmation|min:6',
189
                'password_confirmation' => 'required|min:6'
190
            ]);
191
192
            $doctor->password = Hash::make($request->input('new_password'));
193
            $doctor->save();
194
195
            return redirect(route('doctor.profile', $doctor->id))->with('success', 'Password berhasil diubah !');
196
        }
197
        return redirect(route('doctor.password.edit', $doctor->id))->with('failed', 'Password lama tidak cocok.');
198
    }
199
200
    /**
201
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
202
     */
203
    public function removeImage()
204
    {
205
        $doctor = $this->currentUser();
206
        if($doctor->profile_picture != "user-default.jpg") {
207
            Storage::delete('public/user_images/'.$doctor->profile_picture);
208
        }
209
210
        $doctor->profile_picture = "user-default.jpg";
211
        if($doctor->save()) {
212
            return redirect(route('admin.profile.edit', $doctor->id))->with('success', 'Foto profil berhasil dihapus !');
213
        }
214
        return redirect(route('admin.profile.edit', $doctor->id))->with('failed', 'Gagal menghapus foto profil.');
215
    }
216
217
218
    public function destroy()
219
    {
220
        $doctor = $this->currentUser();
221
        if($doctor->delete()) {
222
            session()->flush();
223
            return redirect(route('doctor.login'))->with('success', 'Akun telah dihapus !');
224
        }
225
        return redirect(route('doctor.dashboard'))->with('failed', 'Penghapusan akun gagal.');
226
    }
227
228
    /**
229
     * Get current logged in Doctor
230
     *
231
     * @return mixed
232
     */
233
    private function currentUser()
234
    {
235
        return Auth::guard('doctor')->user();
236
    }
237
238
    /**
239
     * @param string $oldPassword
240
     * @return bool
241
     */
242
    private function validatePass(string $oldPassword)
243
    {
244
        $doctor = $this->currentUser();
245
        if(Hash::check($oldPassword, $doctor->password)) {
246
            return true;
247
        }
248
249
        return false;
250
    }
251
252
}
253