| Conditions | 3 |
| Paths | 3 |
| Total Lines | 27 |
| Code Lines | 15 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 41 | public function handle() |
||
| 42 | { |
||
| 43 | $subdomain = $this->argument('subdomain'); |
||
| 44 | |||
| 45 | if (!ctype_alnum($subdomain)) { |
||
| 46 | throw new RuntimeException('Subdomain can consist of alphanum chars only.'); |
||
| 47 | } |
||
| 48 | |||
| 49 | $num = Tenant::where('subdomain', $subdomain)->count(); |
||
| 50 | |||
| 51 | if ($num) { |
||
| 52 | throw new RuntimeException('Tenant with subdomain \''.$subdomain.'\' already exists.'); |
||
|
|
|||
| 53 | } |
||
| 54 | |||
| 55 | $tenant = new Tenant(); |
||
| 56 | $tenant->subdomain = $subdomain; |
||
| 57 | $tenant->save(); |
||
| 58 | |||
| 59 | $databaseName = 'tenant_'.$tenant->id; |
||
| 60 | DB::connection('tenant')->statement('CREATE DATABASE '.$databaseName); |
||
| 61 | |||
| 62 | $this->callSilent('tenant:migrate', [ |
||
| 63 | 'tenantId' => $tenant->id, |
||
| 64 | ]); |
||
| 65 | |||
| 66 | $this->info('Created tenant with subdomain \''.$subdomain.'\' and ID '.$tenant->id.'.'); |
||
| 67 | $this->info('Database tables has been migrated.'); |
||
| 68 | } |
||
| 70 |