Completed
Pull Request — master (#1538)
by
unknown
15:50
created

CreateRole::makePermissions()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 6
nop 1
dl 0
loc 47
rs 7.6008
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Commands;
4
5
use Illuminate\Support\Arr;
6
use Spatie\Permission\Guard;
7
use Illuminate\Console\Command;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Spatie\Permission\Traits\AdaptiveCommandParams;
11
use Spatie\Permission\Contracts\Role as RoleContract;
12
use Spatie\Permission\Contracts\Permission as PermissionContract;
13
14
class CreateRole extends Command
15
{
16
    use AdaptiveCommandParams;
17
18
    /**
19
     * The name of command.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'permission:create-role';
24
25
    /**
26
     * The command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a role';
31
32
    /**
33
     * The role app.
34
     *
35
     * @var object
36
     */
37
    protected $roleApp;
38
39
    /**
40
     * The command constructor.
41
     */
42
    public function __construct()
43
    {
44
        $this->roleApp = app(RoleContract::class);
45
46
        $this->registerCustomColumns($this->roleApp);
47
48
        parent::__construct();
49
    }
50
51
    /**
52
     * Execute the command.
53
     *
54
     * @return void
55
     */
56
    public function handle()
57
    {
58
        $input = $this->getInputParams();
59
        $role = $this->roleApp::findOrCreate(Arr::except($input, ['permissions']));
60
61
        $role->givePermissionTo($this->makePermissions($this->option('permissions')));
62
63
        $this->info("Role `{$role->name}` created");
64
    }
65
66
    /**
67
     * Create permissions.
68
     *
69
     * @param string|null $string
70
     *
71
     * @return Illuminate\Support\Collection|null
72
     */
73
    protected function makePermissions($string = null)
74
    {
75
        if (empty($string)) {
76
            return;
77
        }
78
79
        $permissionApp = app(PermissionContract::class);
80
        $permissions = array_map('trim', explode('|', $string));
81
        $models = [];
82
83
        foreach ($permissions as $permission) {
84
            if (false === strpos($permission, ',')) {
85
                // This is the case where the user only enters the permission name
86
                // We will create the permission with the given name and the guard_name from the guard option
87
88
                $params = [
89
                    'name'       => $permission,
90
                    'guard_name' => $this->option('guard'),
91
                ];
92
            } else {
93
                // This is the case where the user enters additional column information for permission
94
                // We need to analyze to get the column names and values
95
96
                $params = [];
97
                $splitParts = array_map('trim', explode(',', $permission));
98
99
                foreach ($splitParts as $permissionParams) {
100
                    $keyValuePair = array_map('trim', explode(':', $permissionParams));
101
                    $column = $keyValuePair[0];
102
                    $value = (isset($keyValuePair[1]) && $keyValuePair[1]) ? $keyValuePair[1] : null;
103
104
                    if ($column) {
105
                        $params[$column] = $value;
106
                    }
107
                }
108
109
                // Overwrite the guard_name parameter
110
                $params['guard_name'] = $this->option('guard');
111
            }
112
113
            if ($model = $permissionApp::findOrCreate($params)) {
114
                $models[] = $model;
115
            }
116
        }
117
118
        return collect($models);
119
    }
120
121
    /**
122
     * Get the command arguments.
123
     *
124
     * @return array
125
     */
126
    protected function getArguments()
127
    {
128
        return array_merge([
129
            ['name', InputArgument::REQUIRED, 'The name of the role'],
130
        ], $this->requiredExtraFields);
131
    }
132
133
    /**
134
     * Get the command options.
135
     *
136
     * @return array
137
     */
138
    protected function getOptions()
139
    {
140
        return array_merge([
141
            ['guard', null, InputOption::VALUE_OPTIONAL, 'The name of the guard', Guard::getDefaultName($this->roleApp)],
142
            ['permissions', null, InputOption::VALUE_OPTIONAL, 'A list of permissions to assign to the role (separated by |)'],
143
        ], $this->optionalExtraFields);
144
    }
145
}
146