Completed
Pull Request — develop (#51)
by Neil
08:28
created

UserController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B preferences() 0 34 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
            $ports   = [];
29
        }
30
        else {
31
            $devices = User::find($request->user()->user_id)->devices()->get();
32
            $ports   = User::find($request->user()->user_id)->ports()->get();
33
        }
34
35
        if ($method === "POST")
36
        {
37
            $this->validate($request, [
38
                'current_password' => 'required|max:255',
39
                'new_password' => 'required|min:8|max:255',
40
                'repeat_password' => 'required|same:new_password|min:8|max:255',
41
            ]);
42
            if (!Hash::check($request->current_password, $request->user()->password))
43
            {
44
                return back()->withInput()->withErrors(['current_password'=>'Current password is incorrect']);
45
            }
46
            $user = User::where('user_id', $request->user()->user_id)->first();
47
            $user->password = Hash::make($request->new_password);
48
            if ($user->save())
49
            {
50
                $updated = true;
51
            }
52
        }
53
        return view('users.preferences', ['updated' => $updated, 'devices' => $devices, 'ports' => $ports]);
54
    }
55
}
56