PermissionFormFields   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A permissionFormFieldData() 0 4 1
A handle() 0 21 3
A fieldsFromModel() 0 14 2
1
<?php
2
3
namespace jeremykenedy\LaravelRoles\App\Services;
4
5
use jeremykenedy\LaravelRoles\Traits\RolesAndPermissionsHelpersTrait;
6
7
class PermissionFormFields
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...es\PermissionFormFields: $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
        'model'         => '',
21
    ];
22
23
    /**
24
     * Create a new job instance.
25
     *
26
     * @param int $id
27
     *
28
     * @return void
29
     */
30
    public function __construct($id = null)
31
    {
32
        $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...
33
    }
34
35
    /**
36
     * Execute the job.
37
     *
38
     * @return void
39
     */
40
    public function handle()
41
    {
42
        $fields = $this->fieldList;
43
44
        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...
45
            $fields = $this->fieldsFromModel($this->id, $fields);
46
        }
47
48
        foreach ($fields as $fieldName => $fieldValue) {
49
            $fields[$fieldName] = old($fieldName, $fieldValue);
50
        }
51
52
        // Get the additional data for the form fields
53
        $permissionFormFieldData = $this->permissionFormFieldData();
54
55
        return array_merge(
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_merge($fiel...ermissionFormFieldData) returns the type array which is incompatible with the documented return type void.
Loading history...
56
            $fields,
57
            $permissionFormFieldData
58
        );
59
60
        return $fields;
0 ignored issues
show
Unused Code introduced by
return $fields is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
61
    }
62
63
    /**
64
     * Return the field values from the model.
65
     *
66
     * @param int   $id
67
     * @param array $fields
68
     *
69
     * @return array
70
     */
71
    protected function fieldsFromModel($id, array $fields)
72
    {
73
        $permission = config('roles.models.permission')::findOrFail($id);
74
75
        $fieldNames = array_keys(array_except($fields, ['permissions']));
76
77
        $fields = [
78
            'id' => $id,
79
        ];
80
        foreach ($fieldNames as $field) {
81
            $fields[$field] = $permission->{$field};
82
        }
83
84
        return $fields;
85
    }
86
87
    /**
88
     * Get the additonal form fields data.
89
     *
90
     * @return array
91
     */
92
    protected function permissionFormFieldData()
93
    {
94
        return [
95
            'permissionModels' => $this->getPermissionModels(),
96
        ];
97
    }
98
}
99