1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 01:14:04 +00:00
tenancy/src/Traits/TenantAwareCommand.php
Samuel Štancl 5dc80473d3
[2.3.1] Fix exit codes in TenantAwareCommand (#336)
* Fix exit codes in TenantAwareCommand

* Cast result to int

* Assert exit code 0
2020-03-30 21:09:58 +02:00

44 lines
1 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Traits;
use Stancl\Tenancy\Tenant;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
trait TenantAwareCommand
{
/** @return int */
protected function execute(InputInterface $input, OutputInterface $output)
{
$tenants = $this->getTenants();
if (count($tenants) === 1) {
return $tenants[0]->run(function () {
return $this->laravel->call([$this, 'handle']);
});
}
$exitCode = 0;
foreach ($tenants as $tenant) {
$result = (int) $tenant->run(function () {
return $this->laravel->call([$this, 'handle']);
});
if ($result !== 0) {
$exitCode = $result;
}
}
return $exitCode;
}
/**
* Get an array of tenants for which the command should be executed.
*
* @return Tenant[]
*/
abstract protected function getTenants(): array;
}