Passed
Push — bugfix/manager-not-created-aft... ( fcd1af )
by Chris
10:00
created

CheckUserRole   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 9
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A handle() 0 11 4
1
<?php
2
3
namespace App\Listeners;
4
5
use App\Events\UserUpdated;
6
use App\Models\Manager;
7
use Illuminate\Support\Facades\Log;
8
9
class CheckUserRole
10
{
11
    /**
12
     * Create the event listener.
13
     *
14
     * @return void
15
     */
16 3
    public function __construct()
17
    {
18
        //
19 3
    }
20
21
    /**
22
     * Create a manager object for a User if one does not exist.
23
     *
24
     * @param UserUpdated $event Fires after successful database update.
25
     * @return void
26
     */
27 3
    public function handle(UserUpdated $event) : void
28
    {
29 3
        if ($event->user->hasRole('manager') ||
30 3
            $event->user->hasRole('admin')
31
        ) {
32 1
            $managerProfile = $event->user->manager;
33 1
            Log::notice("User " . $event->user->id . " manager account: " . $event->user->manager);
34 1
            if ($managerProfile === null) {
35 1
                $managerProfile = new Manager();
36 1
                $managerProfile->user_id = $event->user->id;
37 1
                $managerProfile->save();
38
            }
39
        }
40 2
    }
41
}
42