Skip to content

Instantly share code, notes, and snippets.

@ashokgelal
Created December 10, 2017 19:09
Show Gist options
  • Save ashokgelal/11d9d2e7797f942a4bf7b766a14dced5 to your computer and use it in GitHub Desktop.
Save ashokgelal/11d9d2e7797f942a4bf7b766a14dced5 to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
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 Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
class DeleteTenant extends Command
{
protected $signature = 'tenant:delete {name}';
protected $description = 'Deletes a tenant of the provided name. Only available on the local environment e.g. php artisan tenant:delete boise';
public function handle()
{
// because this is a destructive command, we'll only allow to run this command
// if you are on the local environment
if (!app()->isLocal()) {
$this->error('This command is only avilable on the local environment.');
return;
}
$name = $this->argument('name');
$this->deleteTenant($name);
}
private function deleteTenant($name)
{
if ($customer = Customer::where('name', $name)->with(['websites', 'hostnames'])->firstOrFail()) {
$hostname = $customer->hostnames->first();
$website = $customer->websites->first();
app(HostnameRepository::class)->delete($hostname, true);
app(WebsiteRepository::class)->delete($website, true);
app(CustomerRepository::class)->delete($customer, true);
$this->info("Tenant {$name} successfully deleted.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment