CreateUser   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 29.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 18 3
A createFakeUser() 18 31 3
A createNewUser() 21 36 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace MultiTenantLaravel\App\Commands;
4
5
use Illuminate\Console\Command;
6
use Faker\Generator as Faker;
7
8
class CreateUser extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'tenant:create-user {--fake}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Create a new user with the option to assign to tenants';
23
24
    protected $faker;
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31
    public function __construct(Faker $faker)
32
    {
33
        $this->faker = $faker;
34
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $count = (int) $this->ask('How many would you like to create?');
46
47
        while ($count > 0) {
48
49
50
            if (!$this->option('fake')) {
51
52
                $this->createNewUser();
53
            } else {
54
                $this->createFakeUser();
55
            }
56
57
            $count--;
58
        }
59
60
    }
61
62
    /**
63
     * Setup a fake user using faker
64
     *
65
     * @return void
66
     */
67
    private function createFakeUser()
68
    {
69
        $name = $this->faker->name;
70
71
        $user = config('multi-tenant.user_class')::create([
72
            'name' => $name,
73
            'email' => $this->faker->unique()->safeEmail,
74
            'password' => bcrypt('tester'),
75
        ]);
76
77
        $add_to_tenant = $this->anticipate('Would you like to assign the user to a tenant?', ['Yes', 'No'], 'Yes');
78
79 View Code Duplication
        if ($add_to_tenant == 'Yes') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            $headers = ['Name', 'ID'];
81
            $tenants = config('multi-tenant.tenant_class')::all('name', 'id');
82
83
            if($tenants->count() <= 0) {
84
                $this->comment($user->email . ' with the password `tester` was created without any tenants');
85
            } else {
86
                $this->table($headers, $tenants->toArray());
87
88
                $tenant_id = (int) $this->ask('Please enter the id of the desired tenant.');
89
90
                $tenant = config('multi-tenant.tenant_class')::findOrFail($tenant_id);
91
92
                $tenant->update(['owner_id' => $user->id]);
93
94
                $this->comment('The user ' . $user->email . ' is now the owner of ' . $tenant->name . ' with the password `tester`');
95
            }
96
        }
97
    }
98
99
    /**
100
     * Create a new user with user supplied input for the users settings
101
     *
102
     * @return void
103
     */
104
    private function createNewUser()
105
    {
106
107
        $name = (string) $this->ask('Name');
108
        $email = (string) $this->ask('E-Mail');
109
        $user = config('multi-tenant.user_class')::create([
110
            'name' => $name,
111
            'email' => $email,
112
            'password' => bcrypt('tester'),
113
        ]);
114
115
        $add_to_tenant = $this->anticipate('Would you like to assign the user to a tenant?', ['Yes', 'No'], 'Yes');
116
117 View Code Duplication
        if ($add_to_tenant == 'Yes') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
            $headers = ['Name', 'ID'];
119
            $tenants = config('multi-tenant.tenant_class')::all('name', 'id');
120
121
            if($tenants->count() <= 0) {
122
                $this->comment($user->email . ' with the password `tester` was created without any tenants');
123
            } else {
124
                $this->table($headers, $tenants->toArray());
125
126
                $tenant_id = (int) $this->ask('Please enter the id of the desired tenant.');
127
128
                $tenant = config('multi-tenant.tenant_class')::findOrFail($tenant_id);
129
130
                $tenant->update(['owner_id' => $user->id]);
131
132
                $this->comment('The user ' . $user->email . ' is now the owner of ' . $tenant->name . ' with the password `tester`');
133
            }
134
        }
135
        else{
136
            $this->comment($user->email . ' with the password `tester` was created without any tenants');
137
        }
138
139
    }
140
}
141