Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Commands/Domain/CreateCommand.php
- This file was moved from src/app/Console/Commands/DomainAdd.php.
<?php | <?php | ||||
namespace App\Console\Commands; | namespace App\Console\Commands\Domain; | ||||
use App\Domain; | use App\Console\Command; | ||||
use App\Entitlement; | |||||
use Illuminate\Console\Command; | |||||
use Illuminate\Support\Facades\Queue; | use Illuminate\Support\Facades\Queue; | ||||
class DomainAdd extends Command | class CreateCommand extends Command | ||||
{ | { | ||||
/** | /** | ||||
* The name and signature of the console command. | * The name and signature of the console command. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $signature = 'domain:add {domain} {--force}'; | protected $signature = 'domain:create {domain} {--force}'; | ||||
/** | /** | ||||
* The console command description. | * The console command description. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $description = "Create a domain."; | protected $description = "Create a domain."; | ||||
/** | /** | ||||
* Execute the console command. | * Execute the console command. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function handle() | public function handle() | ||||
{ | { | ||||
$namespace = \strtolower($this->argument('domain')); | $namespace = \strtolower($this->argument('domain')); | ||||
// must use withTrashed(), because unique constraint | // must use withTrashed(), because unique constraint | ||||
$domain = Domain::withTrashed()->where('namespace', $namespace)->first(); | $domain = \App\Domain::withTrashed()->where('namespace', $namespace)->first(); | ||||
if ($domain && !$this->option('force')) { | if ($domain && !$this->option('force')) { | ||||
$this->error("Domain {$namespace} already exists."); | $this->error("Domain {$namespace} already exists."); | ||||
return 1; | return 1; | ||||
} | } | ||||
Queue::fake(); // ignore LDAP for now | |||||
if ($domain) { | if ($domain) { | ||||
if ($domain->deleted_at) { | if ($domain->deleted_at) { | ||||
// revive domain | // set the status back to new | ||||
$domain->deleted_at = null; | $domain->status = \App\Domain::STATUS_NEW; | ||||
$domain->status = 0; | |||||
$domain->save(); | $domain->save(); | ||||
// remove existing entitlement | // remove existing entitlement | ||||
$entitlement = Entitlement::withTrashed()->where( | $entitlement = \App\Entitlement::withTrashed()->where( | ||||
[ | [ | ||||
'entitleable_id' => $domain->id, | 'entitleable_id' => $domain->id, | ||||
'entitleable_type' => \App\Domain::class | 'entitleable_type' => \App\Domain::class | ||||
] | ] | ||||
)->first(); | )->first(); | ||||
if ($entitlement) { | if ($entitlement) { | ||||
$entitlement->forceDelete(); | $entitlement->forceDelete(); | ||||
} | } | ||||
// restore the domain to allow for the observer to handle the create job | |||||
$domain->restore(); | |||||
$this->info( | |||||
sprintf( | |||||
"Domain %s with ID %d revived. Remember to assign it to a wallet with 'domain:set-wallet'", | |||||
$domain->namespace, | |||||
$domain->id | |||||
) | |||||
); | |||||
} else { | } else { | ||||
$this->error("Domain {$namespace} not marked as deleted... examine more closely"); | $this->error("Domain {$namespace} not marked as deleted... examine more closely"); | ||||
return 1; | return 1; | ||||
} | } | ||||
} else { | } else { | ||||
$domain = Domain::create([ | $domain = \App\Domain::create( | ||||
[ | |||||
'namespace' => $namespace, | 'namespace' => $namespace, | ||||
'type' => Domain::TYPE_EXTERNAL, | 'type' => \App\Domain::TYPE_EXTERNAL, | ||||
]); | ] | ||||
} | ); | ||||
$this->info($domain->id); | $this->info( | ||||
sprintf( | |||||
"Domain %s created with ID %d. Remember to assign it to a wallet with 'domain:set-wallet'", | |||||
$domain->namespace, | |||||
$domain->id | |||||
) | |||||
); | |||||
} | |||||
} | } | ||||
} | } |