PermissionFormFields::handle()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 75
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 8.4736
c 0
b 0
f 0
cc 5
eloc 33
nc 6
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Anomaly\UsersModule\Role\Permission;
2
3
use Anomaly\Streams\Platform\Addon\AddonCollection;
4
use Anomaly\UsersModule\Role\Contract\RoleInterface;
5
use Illuminate\Config\Repository;
6
use Illuminate\Translation\Translator;
7
8
/**
9
 * Class PermissionFormFields
10
 *
11
 * @link          http://pyrocms.com/
12
 * @author        PyroCMS, Inc. <[email protected]>
13
 * @author        Ryan Thompson <[email protected]>
14
 */
15
class PermissionFormFields
16
{
17
18
    /**
19
     * Handle the fields.
20
     *
21
     * @param PermissionFormBuilder $builder
22
     * @param AddonCollection $addons
23
     * @param Translator $translator
24
     * @param Repository $config
25
     */
26
    public function handle(
27
        PermissionFormBuilder $builder,
28
        AddonCollection $addons,
29
        Translator $translator,
30
        Repository $config
31
    ) {
32
        /* @var RoleInterface $role */
33
        $role = $builder->getEntry();
34
35
        $fields = [];
36
37
        $namespaces = array_merge(['streams'], $addons->withConfig('permissions')->namespaces());
38
39
        /*
40
         * gather all the addons with a
41
         * permissions configuration file.
42
         *
43
         * @var Addon $addon
44
         */
45
        foreach ($namespaces as $namespace) {
46
47
            foreach ($config->get($namespace . '::permissions', []) as $group => $permissions) {
48
49
                /*
50
                 * Determine the general
51
                 * form UI components.
52
                 */
53
                $label = $namespace . '::permission.' . $group . '.name';
54
55
                if (!$translator->has($warning = $namespace . '::permission.' . $group . '.warning')) {
56
                    $warning = null;
57
                }
58
59
                if (!$translator->has($instructions = $namespace . '::permission.' . $group . '.instructions')) {
60
                    $instructions = null;
61
                }
62
63
                /*
64
                 * Gather the available
65
                 * permissions for the group.
66
                 */
67
                $available = array_combine(
68
                    array_map(
69
                        function ($permission) use ($namespace, $group) {
70
                            return $namespace . '::' . $group . '.' . $permission;
71
                        },
72
                        $permissions
73
                    ),
74
                    array_map(
75
                        function ($permission) use ($namespace, $group) {
76
                            return $namespace . '::permission.' . $group . '.option.' . $permission;
77
                        },
78
                        $permissions
79
                    )
80
                );
81
82
                /*
83
                 * Build the checkboxes field
84
                 * type to handle the UI.
85
                 */
86
                $fields[str_replace('.', '_', $namespace . '::' . $group)] = [
87
                    'label'        => $label,
88
                    'warning'      => $warning,
89
                    'instructions' => $instructions,
90
                    'type'         => 'anomaly.field_type.checkboxes',
91
                    'value'        => $role->getPermissions(),
92
                    'config'       => [
93
                        'options' => $available,
94
                    ],
95
                ];
96
            }
97
        }
98
99
        $builder->setFields($fields);
100
    }
101
}
102