RoleFormFields::fieldsFromModel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 10
1
<?php
2
3
namespace jeremykenedy\LaravelRoles\App\Services;
4
5
use jeremykenedy\LaravelRoles\Traits\RolesAndPermissionsHelpersTrait;
6
7
class RoleFormFields
8
{
9
    use RolesAndPermissionsHelpersTrait;
0 ignored issues
show
introduced by
The trait jeremykenedy\LaravelRole...PermissionsHelpersTrait requires some properties which are not provided by jeremykenedy\LaravelRole...Services\RoleFormFields: $role_id, $user_id, $roles, $permission_id
Loading history...
10
11
    /**
12
     * List of fields and default value for each field.
13
     *
14
     * @var array
15
     */
16
    protected $fieldList = [
17
        'name'          => '',
18
        'slug'          => '',
19
        'description'   => '',
20
        'level'         => '',
21
        'permissions'   => [],
22
    ];
23
24
    /**
25
     * Create a new job instance.
26
     *
27
     * @param int $id
28
     *
29
     * @return void
30
     */
31
    public function __construct($id = null)
32
    {
33
        $this->id = $id;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
    }
35
36
    /**
37
     * Execute the job.
38
     *
39
     * @return void
40
     */
41
    public function handle()
42
    {
43
        $fields = $this->fieldList;
44
        $rolePermissionsIds = [];
45
46
        if ($this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
47
            $fields = $this->fieldsFromModel($this->id, $fields);
48
            $rolePermissionsIds = $this->getRolePermissionsIds($this->id);
49
        }
50
51
        foreach ($fields as $fieldName => $fieldValue) {
52
            $fields[$fieldName] = old($fieldName, $fieldValue);
53
        }
54
55
        // Get the additional data for the form fields
56
        $roleFormFieldData = $this->roleFormFieldData();
57
58
        return array_merge(
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_merge($fiel...s), $roleFormFieldData) returns the type array which is incompatible with the documented return type void.
Loading history...
59
            $fields,
60
            [
61
                'allPermissions'     => config('roles.models.permission')::all(),
62
                'rolePermissionsIds' => $rolePermissionsIds,
63
            ],
64
            $roleFormFieldData
65
        );
66
    }
67
68
    /**
69
     * Return the field values from the model.
70
     *
71
     * @param int   $id
72
     * @param array $fields
73
     *
74
     * @return array
75
     */
76
    protected function fieldsFromModel($id, array $fields)
77
    {
78
        $role = config('roles.models.role')::findOrFail($id);
79
80
        $fieldNames = array_keys(array_except($fields, ['permissions']));
81
82
        $fields = [
83
            'id' => $id,
84
        ];
85
        foreach ($fieldNames as $field) {
86
            $fields[$field] = $role->{$field};
87
        }
88
89
        $fields['permissions'] = $role->permissions();
90
91
        return $fields;
92
    }
93
94
    /**
95
     * Get the additonal form fields data.
96
     *
97
     * @return array
98
     */
99
    protected function roleFormFieldData()
100
    {
101
        $allAvailablePermissions = config('roles.models.permission')::all();
102
103
        return [
104
            'allAvailablePermissions'   => $allAvailablePermissions,
105
        ];
106
    }
107
}
108