. */ namespace App\Commands; use Gate; use App\Models\Track; class DeleteTrackCommand extends CommandBase { /** @var int */ private $_trackId; /** @var Track */ private $_track; public function __construct($trackId) { $this->_trackId = $trackId; $this->_track = Track::find($trackId); } /** * @return bool */ public function authorize() { return Gate::allows('delete', $this->_track); } /** * @throws \Exception * @return CommandResponse */ public function execute() { if ($this->_track->album_id != null) { $album = $this->_track->album; $this->_track->album_id = null; $this->_track->track_number = null; $this->_track->delete(); $album->updateTrackNumbers(); } else { $this->_track->delete(); } return CommandResponse::succeed(); } }