CreateTenant   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 129
Duplicated Lines 50.39 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 65
loc 129
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 15 3
A createFakeTenant() 32 32 2
A createNewTenant() 33 33 2

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 CreateTenant extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'tenant:create-tenant {--fake}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Create a new tenant';
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
            if(!$this->option('fake')) {
49
                $this->createNewTenant();
50
            } else {
51
                $this->createFakeTenant();
52
            }
53
54
            $count--;
55
        }
56
57
    }
58
59
    /**
60
     * Setup a fake user using faker
61
     *
62
     * @return void
63
     */
64 View Code Duplication
    private function createFakeTenant()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66
        $name = $this->faker->name;
67
68
        $create_new = $this->anticipate('Would you like to create a new user, or use an existing?', ['New', 'Existing'], 'New');
69
70
        if ($create_new === "New") {
71
            $name = (string) $this->ask('Name');
72
            $email = (string) $this->ask('E-Mail');
73
            $user = config('multi-tenant.user_class')::create([
74
                'name' => $name,
75
                'email' => $email,
76
                'password' => bcrypt('tester'),
77
            ]);
78
        } else {
79
            $headers = ['Name', 'ID'];
80
            $tenants = config('multi-tenant.user_class')::all('name', 'id');
81
            $this->table($headers, $tenants->toArray());
82
83
            $user_id = (int) $this->ask('Please enter the id of the desired user.');
84
85
            $user = config('multi-tenant.user_class')::findOrFail($user_id);
86
        }
87
88
        $tenant = config('multi-tenant.tenant_class')::create([
89
            'name' => $name,
90
            'owner_id' => $user->id,
91
            'slug' => str_slug($name)
92
        ]);
93
94
        $this->comment('The user ' . $user->email . ' is now the owner of ' . $tenant->name . ' with the password `tester`');
95
    }
96
97
    /**
98
     * Setup a new tenant with user supplied settings for the tenant
99
     *
100
     * @return void
101
     */
102 View Code Duplication
    private function createNewTenant()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
103
    {
104
105
        $tenant_name = (string) $this->ask('Please enter a name for your new tenant.');
106
107
        $create_new = $this->anticipate('Would you like to create a new user, or use an existing?', ['New', 'Existing'], 'New');
108
109
        if ($create_new === "New") {
110
            $user_name = (string) $this->ask('Please enter a name for the new user');
111
            $user_email = (string) $this->ask('Please enter an e-mail for the new user');
112
            $user = config('multi-tenant.user_class')::create([
113
                'name' => $user_name,
114
                'email' => $user_email,
115
                'password' => bcrypt('tester'),
116
            ]);
117
        } else {
118
            $headers = ['Name', 'ID'];
119
            $tenants = config('multi-tenant.user_class')::all('name', 'id');
120
            $this->table($headers, $tenants->toArray());
121
122
            $user_id = (int) $this->ask('Please enter the id of the desired user.');
123
124
            $user = config('multi-tenant.user_class')::findOrFail($user_id);
125
        }
126
127
        $tenant = config('multi-tenant.tenant_class')::create([
128
            'name' => $tenant_name,
129
            'owner_id' => $user->id,
130
            'slug' => str_slug($tenant_name)
131
        ]);
132
133
        $this->comment('The user ' . $user->email . ' is now the owner of ' . $tenant->name . ' with the password `tester`');
134
    }
135
136
}
137