1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 04:44:04 +00:00

Make with-pending VALUE_NONE

This commit is contained in:
lukinovec 2022-10-05 13:35:23 +02:00
parent 72f742e53d
commit af689f816a
2 changed files with 41 additions and 83 deletions

View file

@ -13,37 +13,14 @@ use Symfony\Component\Console\Input\InputOption;
*/
trait HasTenantOptions
{
/** Value indicating an option wasn't passed */
protected $optionNotPassedValue = 'not passed';
protected function getOptions()
{
return array_merge([
['tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, '', null],
['with-pending', null, InputOption::VALUE_OPTIONAL, 'include pending tenants in query', $this->optionNotPassedValue],
['with-pending', null, InputOption::VALUE_NONE, 'include pending tenants in query'],
], parent::getOptions());
}
protected function getWithPendingOption(): bool
{
$optionPassedWithoutArgument = is_null($this->option('with-pending'));
$optionPassedWithArgument = $this->option('with-pending') !== $this->optionNotPassedValue;
// E.g. 'tenants:run --with-pending'
if ($optionPassedWithoutArgument) {
return true;
}
// E.g. 'tenants:run --with-pending=false'
// If the passed value can't get converted to a bool (e.g. --with-pending=foo), default to false
if ($optionPassedWithArgument) {
return filter_var($this->option('with-pending'), FILTER_VALIDATE_BOOLEAN);
}
// Option not passed, e.g. tenants:run
return config('tenancy.pending.include_in_queries');
}
protected function getTenants(): LazyCollection
{
return tenancy()
@ -52,7 +29,7 @@ trait HasTenantOptions
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
})
->when(tenancy()->model()::hasGlobalScope(PendingScope::class), function ($query) {
$query->withPending($this->getWithPendingOption());
$query->withPending(config('tenancy.pending.include_in_queries') ?: $this->option('with-pending'));
})
->cursor();
}