Passed
Branch main (b90ec4)
by Thierry
20:23 queued 14:04
created

CreateNewUser::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 1
dl 0
loc 26
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Actions\Fortify;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\Facades\Hash;
7
use Illuminate\Support\Facades\Validator;
8
use Illuminate\Validation\Rule;
9
use Laravel\Fortify\Contracts\CreatesNewUsers;
10
use Siak\Tontine\Model\User;
11
12
class CreateNewUser implements CreatesNewUsers
13
{
14
    use PasswordValidationRules;
15
16
    /**
17
     * Validate and create a newly registered user.
18
     *
19
     * @param  array<string, string>  $input
20
     */
21
    public function create(array $input): User
22
    {
23
        Validator::make($input, [
24
            'name' => ['required', 'string', 'max:255'],
25
            'email' => [
26
                'required',
27
                'string',
28
                'email',
29
                'max:255',
30
                Rule::unique(User::class),
31
            ],
32
            'country' => ['required', 'string', 'size:2'],
33
            'password' => $this->passwordRules(),
34
            'agree' => ['accepted']
35
        ])->validate();
36
37
        return DB::transaction(function() use($input) {
38
            $user = User::create([
39
                'name' => $input['name'],
40
                'email' => $input['email'],
41
                'password' => Hash::make($input['password']),
42
            ]);
43
44
            $user->profile()->create(['country_code' => $input['country']]);
45
46
            return $user;
47
        });
48
    }
49
}
50