Completed
Push — master ( d0984d...8e3192 )
by Jeremy
10:29 queued 05:27
created

RoleFormFields   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldsFromModel() 0 16 2
A roleFormFieldData() 0 6 1
A __construct() 0 3 1
A handle() 0 23 3
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
                'allPermissions'     => config('roles.models.permission')::all(),
61
                'rolePermissionsIds' => $rolePermissionsIds,
62
            ],
63
            $roleFormFieldData
64
        );
65
    }
66
67
    /**
68
     * Return the field values from the model.
69
     *
70
     * @param int   $id
71
     * @param array $fields
72
     *
73
     * @return array
74
     */
75
    protected function fieldsFromModel($id, array $fields)
76
    {
77
        $role = config('roles.models.role')::findOrFail($id);
78
79
        $fieldNames = array_keys(array_except($fields, ['permissions']));
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated: Arr::except() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

79
        $fieldNames = array_keys(/** @scrutinizer ignore-deprecated */ array_except($fields, ['permissions']));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
80
81
        $fields = [
82
            'id' => $id,
83
        ];
84
        foreach ($fieldNames as $field) {
85
            $fields[$field] = $role->{$field};
86
        }
87
88
        $fields['permissions'] = $role->permissions();
89
90
        return $fields;
91
    }
92
93
    /**
94
     * Get the additonal form fields data.
95
     *
96
     * @return array
97
     */
98
    protected function roleFormFieldData()
99
    {
100
        $allAvailablePermissions = config('roles.models.permission')::all();
101
102
        return [
103
            'allAvailablePermissions'   => $allAvailablePermissions,
104
        ];
105
    }
106
}
107