. */ namespace Poniverse\Ponyfm\Commands; use Gate; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Support\Str; use Poniverse\Ponyfm\Jobs\UpdateTagsForRenamedGenre; use Poniverse\Ponyfm\Models\Genre; use Validator; class RenameGenreCommand extends CommandBase { use DispatchesJobs; /** @var Genre */ private $_genre; private $_newName; public function __construct($genreId, $newName) { $this->_genre = Genre::find($genreId); $this->_newName = $newName; } /** * @return bool */ public function authorize() { return Gate::allows('rename', $this->_genre); } /** * @throws \Exception * @return CommandResponse */ public function execute() { $slug = Str::slug($this->_newName); $rules = [ 'name' => 'required|unique:genres,name,'.$this->_genre->id.',id,deleted_at,NULL|max:50', 'slug' => 'required|unique:genres,slug,'.$this->_genre->id.',id,deleted_at,NULL' ]; $validator = Validator::make([ 'name' => $this->_newName, 'slug' => $slug ], $rules); if ($validator->fails()) { return CommandResponse::fail($validator); } $this->_genre->name = $this->_newName; $this->_genre->slug = $slug; $this->_genre->save(); $this->dispatch(new UpdateTagsForRenamedGenre($this->_genre)); return CommandResponse::succeed(['message' => 'Genre renamed!']); } }