. */ namespace App\Commands; use App\Jobs\DeleteGenre; use App\Models\Genre; use Illuminate\Support\Facades\Gate; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Support\Facades\Validator; class DeleteGenreCommand extends CommandBase { use DispatchesJobs; /** @var Genre */ private $_genreToDelete; private $_destinationGenre; public function __construct($genreId, $destinationGenreId) { $this->_genreToDelete = Genre::find($genreId); $this->_destinationGenre = Genre::find($destinationGenreId); } /** * @return bool */ public function authorize() { return Gate::allows('delete', $this->_genreToDelete); } /** * @throws \Exception * @return CommandResponse */ public function execute() { $rules = [ 'genre_to_delete' => 'required', 'destination_genre' => 'required', ]; // The validation will fail if the genres don't exist // because they'll be null. $validator = Validator::make([ 'genre_to_delete' => $this->_genreToDelete, 'destination_genre' => $this->_destinationGenre, ], $rules); if ($validator->fails()) { return CommandResponse::fail($validator); } $this->dispatch(new DeleteGenre($this->_genreToDelete, $this->_destinationGenre)); return CommandResponse::succeed(['message' => 'Genre deleted!']); } }