Passed
Branch dev5a (c41270)
by Ron
12:51
created

SetUserDetails::createUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Domains\User;
4
5
use App\Events\NewUserCreated;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Log;
9
10
use Carbon\Carbon;
11
12
use App\User;
13
use App\UserSettings;
14
15
class SetUserDetails
16
{
17
    //  Create a new user
18 4
    public function createUser($request)
19
    {
20 4
        $request = collect($request);
21 4
        $request->put('password', bcrypt(strtoLower(Str::random(15))));
22 4
        $request->put('password_expires', Carbon::now()->subDay());
23
24 4
        $user = User::create($request->toArray());
25 4
        UserSettings::create([
26 4
            'user_id' => $user->user_id,
27
        ]);
28
29
        //  Event will trigger the welcome email
30 4
        event(new NewUserCreated($user->makeVisible('user_id')));
31
32 4
        return $user->user_id;
33
    }
34
35
    //  Update a users primary details (name and email address)
36 4
    public function updateUser($request, $userID)
37
    {
38 4
        User::find($userID)->update($request->toArray());
39
40 4
        return true;
41
    }
42
43
    //  Update the notification settings for when the user gets an email or dashboard notification
44 4
    public function updateSettings($request, $userID)
45
    {
46 4
        UserSettings::where('user_id', $userID)->update($request->toArray());
47
48 4
        return true;
49
    }
50
51
    //  Update the users password
52 6
    public function updatePassword($password, $userID, $forceChange = false)
53
    {
54
        //  Determine if there is a new password expire's date (only triggered by an administrator)
55 6
        if(!$forceChange)
56
        {
57 4
            $newExpire = config('auth.passwords.settings.expire') != null ?
58 4
            Carbon::now()->addDays(config('auth.passwords.settings.expire')) : null;
59
        }
60
        else
61
        {
62 2
            $newExpire = Carbon::now()->subDay();
63
        }
64
65
        //  Update the users password
66 6
        User::find($userID)->update([
67 6
            'password'         => bcrypt($password),
68 6
            'password_expires' => $newExpire,
69
        ]);
70
71 6
        return true;
72
    }
73
}
74