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 |
|
|
|
|
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->createNewUser(); |
|
|
|
|
50
|
|
|
} else { |
51
|
|
|
$this->createFakeUser(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$count--; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function createFakeUser() |
60
|
|
|
{ |
61
|
|
|
$name = $this->faker->name; |
62
|
|
|
|
63
|
|
|
$user = config('multi-tenant.user_class')::create([ |
|
|
|
|
64
|
|
|
'name' => $name, |
65
|
|
|
'email' => $this->faker->unique()->safeEmail, |
66
|
|
|
'password' => bcrypt('tester'), |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
$add_to_tenant = $this->anticipate('Would you like to assign the user to a tenant?', ['Yes', 'No'], 'Yes'); |
70
|
|
|
|
71
|
|
|
if ($add_to_tenant == 'Yes') { |
72
|
|
|
$headers = ['Name', 'ID']; |
73
|
|
|
$tenants = config('multi-tenant.tenant_class')::all('name', 'id'); |
74
|
|
|
if($tenants->count() <= 0) { |
75
|
|
|
$this->comment('No tenants available, bye'); |
76
|
|
|
} else { |
77
|
|
|
$this->table($headers, $tenants->toArray()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function createNewUser() |
83
|
|
|
{ |
84
|
|
|
// Ask for some user input and create a new tenant |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.