Passed
Push — dev5a ( d46f4b...f24c41 )
by Ron
07:14
created

SetPermissions::updateRole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Domains\Roles;
4
5
use Illuminate\Support\Facades\Log;
6
7
use App\UserRolePermissions;
8
9
use App\UserRoleType;
10
11
class SetPermissions
12
{
13 6
    public function submitRole($request)
14
    {
15 6
        if($request->role_id != null)
16
        {
17 2
            $this->updateRole($request);
18
        }
19
        else
20
        {
21 4
            $this->createRole($request);
22
        }
23
24 6
        return true;
25
    }
26
27 2
    protected function updateRole($request)
28
    {
29 2
        UserRoleType::findOrFail($request->role_id)->update([
30 2
            'name'        => $request->name,
31 2
            'description' => $request->description,
32
        ]);
33
34 2
        foreach($request->user_role_permissions as $perm)
35
        {
36 2
            Log::emergency($perm);
37 2
            UserRolePermissions::where('role_id', $request->role_id)->where('perm_type_id', $perm['perm_type_id'])->update([
38 2
                'allow' => $perm['allow'],
39
            ]);
40
        }
41 2
    }
42
43 4
    protected function createRole($request)
44
    {
45 4
        $role = UserRoleType::create([
46 4
            'name'        => $request->name,
47 4
            'description' => $request->description,
48
        ]);
49
50 4
        foreach($request->user_role_permissions as $perm)
51
        {
52 4
            UserRolePermissions::create([
53 4
                'role_id'      => $role['role_id'],
54 4
                'perm_type_id' => $perm['perm_type_id'],
55 4
                'allow'        => isset($perm['allow']) ? true : false,
56
            ]);
57
        }
58 4
    }
59
}
60