PermissionFormFields   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 90
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 78 5
1
<?php namespace Anomaly\UsersModule\User\Permission;
2
3
use Anomaly\Streams\Platform\Addon\AddonCollection;
4
use Anomaly\UsersModule\User\Contract\UserInterface;
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 UserInterface $user */
33
        $user      = $builder->getEntry();
34
        $roles     = $user->getRoles();
35
        $inherited = $roles->permissions();
36
37
        $fields = [];
38
39
        $namespaces = array_merge(['streams'], $addons->withConfig('permissions')->namespaces());
40
41
        /*
42
         * gather all the addons with a
43
         * permissions configuration file.
44
         *
45
         * @var Addon $addon
46
         */
47
        foreach ($namespaces as $namespace) {
48
49
            foreach ($config->get($namespace . '::permissions', []) as $group => $permissions) {
50
51
                /*
52
                 * Determine the general
53
                 * form UI components.
54
                 */
55
                $label = $namespace . '::permission.' . $group . '.name';
56
57
                if (!$translator->has($warning = $namespace . '::permission.' . $group . '.warning')) {
58
                    $warning = null;
59
                }
60
61
                if (!$translator->has($instructions = $namespace . '::permission.' . $group . '.instructions')) {
62
                    $instructions = null;
63
                }
64
65
                /*
66
                 * Gather the available
67
                 * permissions for the group.
68
                 */
69
                $available = array_combine(
70
                    array_map(
71
                        function ($permission) use ($namespace, $group) {
72
                            return $namespace . '::' . $group . '.' . $permission;
73
                        },
74
                        $permissions
75
                    ),
76
                    array_map(
77
                        function ($permission) use ($namespace, $group) {
78
                            return $namespace . '::permission.' . $group . '.option.' . $permission;
79
                        },
80
                        $permissions
81
                    )
82
                );
83
84
                /*
85
                 * Build the checkboxes field
86
                 * type to handle the UI.
87
                 */
88
                $fields[str_replace('.', '_', $namespace . '::' . $group)] = [
89
                    'label'        => $label,
90
                    'warning'      => $warning,
91
                    'instructions' => $instructions,
92
                    'type'         => 'anomaly.field_type.checkboxes',
93
                    'value'        => array_merge($user->getPermissions(), $inherited),
94
                    'config'       => [
95
                        'disabled' => $inherited,
96
                        'options'  => $available,
97
                    ],
98
                ];
99
            }
100
        }
101
102
        $builder->setFields($fields);
103
    }
104
}
105