. */ namespace App\Commands; use App\Jobs\UpdateTagsForRenamedShowSong; use App\Models\ShowSong; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; class RenameShowSongCommand extends CommandBase { use DispatchesJobs; /** @var Song */ private $_song; private $_newName; public function __construct($genreId, $newName) { $this->_song = ShowSong::find($genreId); $this->_newName = $newName; } /** * @return bool */ public function authorize() { return Gate::allows('rename', $this->_song); } /** * @throws \Exception * @return CommandResponse */ public function execute() { $slug = Str::slug($this->_newName); $rules = [ 'title' => 'required|unique:show_songs,title,'.$this->_song->id.',id|max:250', 'slug' => 'required|unique:show_songs,slug,'.$this->_song->id.',id', ]; $validator = Validator::make([ 'title' => $this->_newName, 'slug' => $slug, ], $rules); if ($validator->fails()) { return CommandResponse::fail($validator); } $this->_song->title = $this->_newName; $this->_song->slug = $slug; $this->_song->save(); $this->dispatch(new UpdateTagsForRenamedShowSong($this->_song)); return CommandResponse::succeed(['message' => 'Show song renamed!']); } }