Completed
Pull Request — develop (#51)
by Neil
07:52
created

UserController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 4
dl 0
loc 41
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B preferences() 0 32 5
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Dingo\Api\Http;
6
use Dingo\Api\Routing\Helpers;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\RedirectResponse;
9
use Route;
10
use Hash;
11
use App\User;
12
13
class UserController extends Controller
14
{
15
    use Helpers;
16
17
    public function __construct(Request $request) {
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
18
        $this->middleware('auth');
19
    }
20
21
    public function preferences(Request $request) {
22
23
        $method = $request->method();
24
        $updated = false;
25
        if ($request->user()->hasGlobalRead() === true)
26
        {
27
            $devices = [];
28
        }
29
        else {
30
            $devices = User::find($request->user()->user_id)->devices()->get();
31
        }
32
33
        if ($method === "POST")
34
        {
35
            $this->validate($request, [
36
                'current_password' => 'required|max:255',
37
                'new_password' => 'required|min:8|max:255',
38
                'repeat_password' => 'required|same:new_password|min:8|max:255',
39
            ]);
40
            if (!Hash::check($request->current_password, $request->user()->password))
41
            {
42
                return back()->withInput()->withErrors(['current_password'=>'Current password is incorrect']);
43
            }
44
            $user = User::where('user_id', $request->user()->user_id)->first();
45
            $user->password = Hash::make($request->new_password);
46
            if ($user->save())
47
            {
48
                $updated = true;
49
            }
50
        }
51
        return view('users.preferences', ['updated' => $updated, 'devices' => $devices]);
52
    }
53
}
54