CreateTenantUser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\Tenant;
6
use App\Models\User;
7
use App\Services\TenantManager;
8
use App\TenantUser;
9
use Illuminate\Console\Command;
10
use Illuminate\Support\Facades\DB;
11
use Symfony\Component\Console\Exception\RuntimeException;
12
13
class CreateTenantUser extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'user:create {tenantId} {--email=} {--name=} {--password=}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Creates new user for a tenant';
28
29
    protected $tenantManager;
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     * @param TenantManager $tenantManager
35
     */
36
    public function __construct(TenantManager $tenantManager)
37
    {
38
        parent::__construct();
39
40
        $this->tenantManager = $tenantManager;
41
    }
42
43
    /**
44
     * Execute the console command.
45
     *
46
     * @throws \Throwable
47
     *
48
     * @return mixed
49
     */
50
    public function handle()
51
    {
52
        $tenantId = $this->argument('tenantId');
53
        $tenant = Tenant::find($tenantId);
54
55
        if (!$tenant) {
56
            throw new RuntimeException('Tenant with ID = '.$tenantId.' does not exist.');
0 ignored issues
show
Bug introduced by
Are you sure $tenantId of type array|null|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            throw new RuntimeException('Tenant with ID = './** @scrutinizer ignore-type */ $tenantId.' does not exist.');
Loading history...
57
        }
58
59
        $this->comment('Creating new user for tenant with subdomain \''.$tenant->subdomain.'\'.');
60
61
        $email = $this->option('email');
62
        if (!$email) {
63
            $email = $this->ask('User\'s email');
64
        }
65
66
        $num = TenantUser::where('username', $email)->count();
67
        if ($num) {
68
            throw new RuntimeException('User with email \''.$email.'\' already exists.');
69
        }
70
71
        $name = $this->option('name');
72
        if (!$name) {
73
            $name = $this->ask('User\'s full name');
74
        }
75
76
        $password = $this->option('password');
77
        if (!$password) {
78
            $password = $this->secret('User\'s password');
79
        }
80
81
        $tenantUser = new TenantUser();
82
        $tenantUser->username = $email;
83
        $tenantUser->tenant_id = $tenant->id;
0 ignored issues
show
Bug introduced by
The property tenant_id does not exist on App\TenantUser. Did you mean tenant?
Loading history...
84
85
        $this->tenantManager->setTenant($tenant);
86
        $user = new User();
87
        $user->email = $email;
88
        $user->name = $name;
89
        $user->password = bcrypt($password);
90
91
        $user->assignRole('super-admin');
92
93
        DB::transaction(function () use ($tenantUser, $user) {
94
            $tenantUser->save();
95
            $user->save();
96
        });
97
98
        $this->comment('Created user with username \''.$user->email.'\'.');
99
    }
100
}
101