Skip to content
This repository has been archived by the owner on Oct 14, 2019. It is now read-only.

Commit

Permalink
setup BaseTenantableCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
tomschlick committed Aug 12, 2017
1 parent c7145c8 commit 99c77e6
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/Commands/BaseTenantableCommand.php
@@ -0,0 +1,110 @@
<?php

namespace TomSchlick\Townhouse;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;

abstract class BaseTenantableCommand extends Command
{
/**
* Declare the handle method as final and use it to traverse through tenants.
*/
final public function handle()
{
if ( ! $this->confirmToProceed()) {
return;
}

/** @var Tenant $tenant */
foreach ($this->parseTenantInput() as $tenant) {
$this->info("Starting for tenant: {$tenant->id} - {$tenant->name}");

$this->bootstrapTenant($tenant);

$this->handleTenant($tenant);

$this->info("\nFinished for tenant: " . $tenant->id);
}
}

/**
* @param \TomSchlick\Townhouse\Tenant $tenant
*
* @return void
*/
abstract public function handleTenant(Tenant $tenant) : void;

/**
* Get or ask for the tenant(s) you'd like to run the commands on.
*
* @return \Illuminate\Support\Collection|null
*/
protected function parseTenantInput() : ?Collection
{
if ($this->option('all')) {
return Tenant::all();
}

$input_ids = $this->option('tenant')
? $this->option('tenant')
: $this->ask('Which tenant would you like to use? [tenant_id]');

return Tenant::find(
array_map(
function ($id) {
return trim($id);
},
explode(',', $input_ids)
)
);
}

/**
* Get the console command arguments.
*
* @return array
*/
public function getArguments()
{
return [];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['tenant', null, InputOption::VALUE_REQUIRED, 'The tenant you want to run the command as.'],
['all', null, InputOption::VALUE_NONE, 'Run all tenants instead of just one.'],
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
];
}

/**
* Override the fire method as well.
*/
final public function fire()
{
$this->handle();
}

/**
* Make Laravel ready for the tenant we want to run the command on.
*
* @param \TomSchlick\Townhouse\Tenant $tenant
*/
protected function bootstrapTenant(Tenant $tenant)
{
if ($this->checkTenantDatabaseExists && ! $tenant->databaseExists()) {
$this->error('Tenant ' . $tenant->id . ' does not have a database setup.');
}

if ($tenant->databaseExists()) {
$tenant->setCurrentTenantConfiguration();
}
}
}

0 comments on commit 99c77e6

Please sign in to comment.