diff --git a/src/app/Console/Commands/Job/DomainCreate.php b/src/app/Console/Commands/Job/DomainCreate.php deleted file mode 100644 index e03805e3..00000000 --- a/src/app/Console/Commands/Job/DomainCreate.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; - -class DomainCreate extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:domaincreate {domain}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the domain creation job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $domain = $this->getDomain($this->argument('domain')); - - if (!$domain) { - return 1; - } - - $job = new \App\Jobs\Domain\CreateJob($domain->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/DomainUpdate.php b/src/app/Console/Commands/Job/DomainUpdate.php deleted file mode 100644 index 1477725b..00000000 --- a/src/app/Console/Commands/Job/DomainUpdate.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; - -class DomainUpdate extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:domainupdate {domain}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the domain update job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $domain = $this->getDomain($this->argument('domain')); - - if (!$domain) { - return 1; - } - - $job = new \App\Jobs\Domain\UpdateJob($domain->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/ResourceCreate.php b/src/app/Console/Commands/Job/ResourceCreate.php deleted file mode 100644 index fe57b5f2..00000000 --- a/src/app/Console/Commands/Job/ResourceCreate.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; - -class ResourceCreate extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:resourcecreate {resource}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the resource creation job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $resource = $this->getResource($this->argument('resource')); - - if (!$resource) { - return 1; - } - - $job = new \App\Jobs\Resource\CreateJob($resource->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/ResourceUpdate.php b/src/app/Console/Commands/Job/ResourceUpdate.php deleted file mode 100644 index 15a0ffac..00000000 --- a/src/app/Console/Commands/Job/ResourceUpdate.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; - -class ResourceUpdate extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:resourceupdate {resource}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the resource update job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $resource = $this->getResource($this->argument('resource')); - - if (!$resource) { - return 1; - } - - $job = new \App\Jobs\Resource\UpdateJob($resource->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/SharedFolderCreate.php b/src/app/Console/Commands/Job/SharedFolderCreate.php deleted file mode 100644 index 79b63595..00000000 --- a/src/app/Console/Commands/Job/SharedFolderCreate.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; - -class SharedFolderCreate extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:sharedfoldercreate {folder}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the shared folder creation job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $folder = $this->getSharedFolder($this->argument('folder')); - - if (!$folder) { - return 1; - } - - $job = new \App\Jobs\SharedFolder\CreateJob($folder->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/SharedFolderUpdate.php b/src/app/Console/Commands/Job/SharedFolderUpdate.php deleted file mode 100644 index 3b238557..00000000 --- a/src/app/Console/Commands/Job/SharedFolderUpdate.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; - -class SharedFolderUpdate extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:sharedfolderupdate {folder}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the shared folder update job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $folder = $this->getSharedFolder($this->argument('folder')); - - if (!$folder) { - return 1; - } - - $job = new \App\Jobs\SharedFolder\UpdateJob($folder->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/UserCreate.php b/src/app/Console/Commands/Job/UserCreate.php deleted file mode 100644 index 7cd979ec..00000000 --- a/src/app/Console/Commands/Job/UserCreate.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; - -class UserCreate extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:usercreate {user}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the user creation job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $user = $this->getUser($this->argument('user')); - - if (!$user) { - return 1; - } - - $job = new \App\Jobs\User\CreateJob($user->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/UserUpdate.php b/src/app/Console/Commands/Job/UserUpdate.php deleted file mode 100644 index 58367804..00000000 --- a/src/app/Console/Commands/Job/UserUpdate.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; - -class UserUpdate extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:userupdate {user}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the user update job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $user = $this->getUser($this->argument('user')); - - if (!$user) { - return 1; - } - - $job = new \App\Jobs\User\UpdateJob($user->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/WalletCharge.php b/src/app/Console/Commands/Job/WalletCharge.php deleted file mode 100644 index 0a510bbf..00000000 --- a/src/app/Console/Commands/Job/WalletCharge.php +++ /dev/null @@ -1,40 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; -use App\Wallet; - -class WalletCharge extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:walletcharge {wallet}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the WalletCharge job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $wallet = $this->getWallet($this->argument('wallet')); - - if (!$wallet) { - return 1; - } - - $job = new \App\Jobs\WalletCharge($wallet->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/Job/WalletCheck.php b/src/app/Console/Commands/Job/WalletCheck.php deleted file mode 100644 index 9a32920e..00000000 --- a/src/app/Console/Commands/Job/WalletCheck.php +++ /dev/null @@ -1,40 +0,0 @@ -<?php - -namespace App\Console\Commands\Job; - -use App\Console\Command; -use App\Wallet; - -class WalletCheck extends Command -{ - /** - * The name and signature of the console command. - * - * @var string - */ - protected $signature = 'job:walletcheck {wallet}'; - - /** - * The console command description. - * - * @var string - */ - protected $description = "Execute the WalletCheck job (again)."; - - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() - { - $wallet = $this->getWallet($this->argument('wallet')); - - if (!$wallet) { - return 1; - } - - $job = new \App\Jobs\WalletCheck($wallet->id); - $job->handle(); - } -} diff --git a/src/app/Console/Commands/JobExecuteCommand.php b/src/app/Console/Commands/JobExecuteCommand.php new file mode 100644 index 00000000..6587eaa7 --- /dev/null +++ b/src/app/Console/Commands/JobExecuteCommand.php @@ -0,0 +1,54 @@ +<?php + +namespace App\Console\Commands; + +use App\Console\Command; + +class JobExecuteCommand extends Command +{ + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'job:execute {job} {object}'; + + /** + * The console command description. + * + * @var string + */ + protected $description = "Execute a job."; + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $job = $this->argument('job'); + $object = $this->argument('object'); + + $job = str_replace('/', '\\', $job); + + if (preg_match('/^(WalletCheck|WalletCharge)$/', $job)) { + $object = $this->getWallet($object); + } elseif (preg_match('/^(User|Domain|Group|Resource|SharedFolder).[a-zA-Z]+$/', $job, $m)) { + $object = $this->{'get' . $m[1]}($object); + } else { + $this->error("Invalid or unsupported job name."); + return 1; + } + + if (empty($object)) { + $this->error("Object not found."); + return 1; + } + + $job = 'App\\Jobs\\' . $job; + + $job = new $job($object->id); + $job->handle(); + } +} diff --git a/src/tests/Feature/Console/JobExecuteTest.php b/src/tests/Feature/Console/JobExecuteTest.php new file mode 100644 index 00000000..a3cdf9cf --- /dev/null +++ b/src/tests/Feature/Console/JobExecuteTest.php @@ -0,0 +1,35 @@ +<?php + +namespace Tests\Feature\Console; + +use Tests\TestCase; + +class JobExecuteTest extends TestCase +{ + /** + * Test command execution + */ + public function testHandle(): void + { + // Object that does not exist + $code = \Artisan::call("job:execute User/UpdateJob unknown"); + $output = trim(\Artisan::output()); + + $this->assertSame(1, $code); + $this->assertSame("Object not found.", $output); + + // Invalid job name + $code = \Artisan::call("job:execute Job john@kolab.org"); + $output = trim(\Artisan::output()); + + $this->assertSame(1, $code); + $this->assertSame("Invalid or unsupported job name.", $output); + + // Object that exists + $code = \Artisan::call("job:execute User/UpdateJob john@kolab.org"); + $output = trim(\Artisan::output()); + + $this->assertSame(0, $code); + $this->assertSame('', $output); + } +}