Created
August 29, 2018 03:18
-
-
Save kareypowell/45f079504b4ffe30c54b3ce9afff1b23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use App\User; | |
use Hyn\Tenancy\Contracts\Repositories\CustomerRepository; | |
use Hyn\Tenancy\Contracts\Repositories\HostnameRepository; | |
use Hyn\Tenancy\Contracts\Repositories\WebsiteRepository; | |
use Hyn\Tenancy\Environment; | |
use Hyn\Tenancy\Models\Customer; | |
use Hyn\Tenancy\Models\Hostname; | |
use Hyn\Tenancy\Models\Website; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Hash; | |
class CreateTenant extends Command | |
{ | |
protected $signature = 'tenant:create {name} {email}'; | |
protected $description = 'Creates a tenant with the provided name and email address e.g. php artisan tenant:create boise [email protected]'; | |
public function handle() | |
{ | |
$name = $this->argument('name'); | |
$email = $this->argument('email'); | |
if ($this->tenantExists($name, $email)) { | |
$this->error("A tenant with name '{$name}' and/or '{$email}' already exists."); | |
return; | |
} | |
$hostname = $this->registerTenant($name, $email); | |
app(Environment::class)->hostname($hostname); | |
// we'll create a random secure password for our to-be admin | |
$password = str_random(); | |
$this->addAdmin($name, $email, $password); | |
$this->info("Tenant '{$name}' is created and is now accessible at {$hostname->fqdn}"); | |
$this->info("Admin {$email} can log in using password {$password}"); | |
} | |
private function tenantExists($name, $email) | |
{ | |
return Customer::where('name', $name)->orWhere('email', $email)->exists(); | |
} | |
private function registerTenant($name, $email) | |
{ | |
// create a customer | |
$customer = new Customer; | |
$customer->name = $name; | |
$customer->email = $email; | |
app(CustomerRepository::class)->create($customer); | |
// associate the customer with a website | |
$website = new Website; | |
$website->customer()->associate($customer); | |
app(WebsiteRepository::class)->create($website); | |
// associate the website with a hostname | |
$hostname = new Hostname; | |
$baseUrl = config('app.url_base'); | |
$hostname->fqdn = "{$name}.{$baseUrl}"; | |
$hostname->customer()->associate($customer); | |
app(HostnameRepository::class)->attach($hostname, $website); | |
return $hostname; | |
} | |
private function addAdmin($name, $email, $password) | |
{ | |
$admin = User::create(['name' => $name, 'email' => $email, 'password' => Hash::make($password)]); | |
return $admin; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment