. */ namespace Poniverse\Ponyfm\Commands; use Gate; use Illuminate\Support\Str; use Poniverse\Ponyfm\Genre; use Validator; class RenameGenreCommand extends CommandBase { /** @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(); return CommandResponse::succeed(['message' => 'Genre renamed!']); } }