Completed
Push — master ( 872a00...e2f753 )
by Troy
01:33
created

SyncRolesPermissionsFeatures::syncPermissions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace MultiTenantLaravel\App\Commands;
4
5
use Illuminate\Console\Command;
6
7
class SyncRolesPermissionsFeatures extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'tenant:sync';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Sync the multi-tenant roles, permissions & features';
22
23
    /**
24
     * Execute the console command.
25
     *
26
     * @return mixed
27
     */
28
    public function handle()
29
    {
30
        // Sync all roles
31
        $this->syncRoles();
32
33
        // Sync all features
34
        $this->syncFeatures();
35
36
        // Sync all permissions
37
        $this->syncPermissions();
38
    }
39
40
    /**
41
     * Sync the roles for the project.
42
     */
43
    private function syncRoles()
44
    {
45
        // Make sure we have a super user role
46
        config('multi-tenant.role_class')::firstOrCreate(['name' => 'super', 'label' => 'Super']);
47
48
        foreach(config('multi-tenant.roles') as $key => $label) {
49
            config('multi-tenant.role_class')::firstOrCreate(['name' => $key, 'label' => $label]);
50
        }
51
    }
52
53
    /**
54
     * Sync the features for the project.
55
     */
56
    private function syncFeatures()
57
    {
58
        foreach(config('multi-tenant.features') as $key => $feature) {
59
            config('multi-tenant.feature_class')::firstOrCreate(['name' => $key, 'label' => $feature['label'], 'model' => $feature['model']]);
60
        }
61
    }
62
63
    /**
64
     * Sync the permissions for the project.
65
     */
66
    private function syncPermissions()
67
    {
68
        // Loop through all the features, then loop through the permission types
69
        foreach (config('multi-tenant.feature_class')::get() as $feature) {
70
            // Loop through the permission types and create a
71
            // permission for each permission type on this feature
72
            foreach (config('multi-tenant.permission_types') as $permission_type) {
73
                config('multi-tenant.permission_class')::firstOrCreate([
74
                    'name' => $feature->name . '_' . $permission_type,
75
                ]);
76
            }
77
        }
78
    }
79
}
80