Skip to content

Commit

Permalink
Add artisan command laravel-utils:dispatch-job
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Jun 17, 2024
1 parent 591d3d3 commit 088c5d6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ See [GitHub releases](https://github.com/mll-lab/laravel-utils/releases).

## Unreleased

## v5.4.0

### Added

- Add artisan command `laravel-utils:dispatch-job`

## v5.3.0

### Added
Expand Down
5 changes: 5 additions & 0 deletions src/LaravelUtilsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MLL\LaravelUtils;

use Illuminate\Support\ServiceProvider;
use MLL\LaravelUtils\Queue\DispatchJob;

class LaravelUtilsServiceProvider extends ServiceProvider
{
Expand All @@ -11,5 +12,9 @@ public function boot(): void
$this->publishes([
__DIR__ . '/stubs' => $this->app->basePath('stubs'),
], ['strict-stubs']);

$this->commands([
DispatchJob::class,
]);
}
}
29 changes: 29 additions & 0 deletions src/Queue/DispatchJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace MLL\LaravelUtils\Queue;

use Illuminate\Console\Command;

final class DispatchJob extends Command
{
protected $signature = 'laravel-utils:dispatch-job {class}';

protected $description = 'Dispatch a job with the given class';

public function handle(): void
{
$class = $this->argument('class');
if (! is_string($class)) {
$notString = gettype($class);
throw new \Exception("Expected argument 'class' to be string, got {$notString}.");
}
if (! class_exists($class)) {
throw new \Exception("Job class {$class} does not exist.");
}

if (! method_exists($class, 'dispatch')) {
throw new \Exception("Job class {$class} does not have a method 'dispatch'.");
}
$class::dispatch();
}
}

0 comments on commit 088c5d6

Please sign in to comment.