refactor: if / else / elseif statements

- Change If Else Value Assign To Early Return
- Change Nested Ifs To Early Return
- Remove Always Else
This commit is contained in:
HDVinnie
2020-02-12 12:34:34 -05:00
parent 8607387023
commit 611fcd6bc1
57 changed files with 1065 additions and 1341 deletions
+51 -74
View File
@@ -253,9 +253,8 @@ class UserController extends Controller
if (isset($css_url) && filter_var($css_url, FILTER_VALIDATE_URL) === false) {
return redirect()->route('users.show', ['username' => $user->username])
->withErrors('The URL for the external CSS stylesheet is invalid, try it again with a valid URL.');
} else {
$user->custom_css = $css_url;
}
$user->custom_css = $css_url;
$user->nav = $request->input('sidenav');
// Torrent Settings
@@ -329,10 +328,9 @@ class UserController extends Controller
$user->save();
return redirect()->route('home.index')->withSuccess('Your Password Has Been Reset');
} else {
return redirect()->route('user_security', ['username' => $user->username, 'hash' => '#password'])
->withErrors('Your Password Was Incorrect!');
}
return redirect()->route('user_security', ['username' => $user->username, 'hash' => '#password'])
->withErrors('Your Password Was Incorrect!');
} else {
return redirect()->route('user_security', ['username' => $user->username, 'hash' => '#password'])
->withErrors('Your New Password Is To Weak!');
@@ -370,13 +368,11 @@ class UserController extends Controller
if ($v->fails()) {
return redirect()->route('user_security', ['username' => $user->username, 'hash' => '#email'])
->withErrors($v->errors());
} else {
$user->email = $request->input('email');
$user->save();
return redirect()->route('user_security', ['username' => $user->username, 'hash' => '#email'])
->withSuccess('Your Email Was Updated Successfully!');
}
$user->email = $request->input('email');
$user->save();
return redirect()->route('user_security', ['username' => $user->username, 'hash' => '#email'])
->withSuccess('Your Email Was Updated Successfully!');
}
/**
@@ -1231,16 +1227,13 @@ class UserController extends Controller
$user = User::where('username', '=', $username)->firstOrFail();
abort_unless($request->user()->group->is_modo || $request->user()->id == $user->id, 403);
if ($request->has('view') && $request->input('view') == 'seeds') {
$history = Peer::with(['torrent' => function ($query) {
$query->withAnyStatus();
}])->selectRaw('distinct(torrents.info_hash),max(peers.id) as id,max(torrents.name) as name,max(torrents.seeders) as seeders,max(torrents.leechers) as leechers,max(torrents.times_completed) as times_completed,max(torrents.size) as size,max(history.info_hash) as history_info_hash,max(history.created_at) as history_created_at,max(torrents.id) as torrent_id,max(history.seedtime) as seedtime')->leftJoin('torrents', 'torrents.id', '=', 'peers.torrent_id')->leftJoin('history', 'history.info_hash', '=', 'torrents.info_hash')->where('peers.user_id', '=', $user->id)->whereRaw('history.user_id = ? and history.seeder = ?', [$user->id, 1])
->where('peers.seeder', '=', 1)->groupBy('torrents.info_hash');
$order = null;
$sorting = null;
$history->where(function ($query) use ($request) {
if ($request->has('dying') && $request->input('dying') != null) {
$query->orWhereRaw('(torrents.seeders = ? AND torrents.times_completed > ?)', [1, 2]);
@@ -1276,7 +1269,6 @@ class UserController extends Controller
$query->orWhereRaw('(history.active = 1 AND history.seedtime > (2592000 * 12))', []);
}
});
if ($request->has('name') && $request->input('name') != null) {
$history->where('torrents.name', 'like', '%'.$request->input('name').'%');
}
@@ -1296,7 +1288,6 @@ class UserController extends Controller
} else {
$direction = 2;
}
if ($sorting != 'name' && $sorting != 'size' && $sorting != 'times_completed' && $sorting != 'seeders' && $sorting != 'leechers') {
if ($sorting == 'seedtime') {
$table = $history->orderBy($sorting, $order)->paginate(50);
@@ -1308,17 +1299,16 @@ class UserController extends Controller
} else {
$table = $history->orderBy($sorting, $order)->paginate(50);
}
return view('user.filters.seeds', [
'user' => $user,
'seeds' => $table,
])->render();
} elseif ($request->has('view') && $request->input('view') == 'requests') {
$torrentRequests = TorrentRequest::with(['user', 'category']);
}
if ($request->has('view') && $request->input('view') == 'requests') {
$torrentRequests = TorrentRequest::with(['user', 'category']);
$order = null;
$sorting = null;
if ($request->has('name') && $request->input('name') != null) {
$torrentRequests->where('name', 'like', '%'.$request->input('name').'%');
}
@@ -1344,7 +1334,6 @@ class UserController extends Controller
$query->whereNull('filled_by')->orWhereNull('filled_hash')->orWhereNull('approved_by');
});
}
if ($request->has('sorting') && $request->input('sorting') != null) {
$sorting = $request->input('sorting');
}
@@ -1361,13 +1350,11 @@ class UserController extends Controller
} else {
$direction = 2;
}
if ($sorting == 'date') {
$table = $torrentRequests->where('user_id', '=', $user->id)->orderBy('created_at', $order)->paginate(25);
} else {
$table = $torrentRequests->where('user_id', '=', $user->id)->orderBy($sorting, $order)->paginate(25);
}
return view('user.filters.requests', [
'user' => $user,
'torrentRequests' => $table,
@@ -1763,32 +1750,29 @@ class UserController extends Controller
'his_downl' => $his_downl,
'his_downl_cre' => $his_downl_cre,
]);
} else {
$logger = 'user.downloads';
if (config('hitrun.enabled') == true) {
$downloads = History::with(['torrent' => function ($query) {
$query->withAnyStatus();
}])->selectRaw('distinct(history.info_hash), max(torrents.id), max(history.completed_at) as completed_at, max(history.created_at) as created_at, max(history.id) as id, max(history.user_id) as user_id, max(history.seedtime) as seedtime, max(history.seeder) as seeder, max(torrents.size) as size,max(torrents.leechers) as leechers,max(torrents.seeders) as seeders,max(torrents.times_completed) as times_completed')->leftJoin('torrents', 'torrents.info_hash', '=', 'history.info_hash')->where('actual_downloaded', '>', 0)
->whereRaw('history.actual_downloaded > (torrents.size * ('.(config('hitrun.buffer') / 100).'))')
->where('history.user_id', '=', $user->id)
->groupBy('history.info_hash')->orderBy('completed_at', 'desc')
->paginate(50);
} else {
$downloads = History::with(['torrent' => function ($query) {
$query->withAnyStatus();
}])->selectRaw('distinct(history.info_hash), max(torrents.id), max(history.completed_at) as completed_at, max(history.created_at) as created_at, max(history.id) as id, max(history.user_id) as user_id, max(history.seedtime) as seedtime, max(history.seeder) as seeder, max(torrents.size) as size,max(torrents.leechers) as leechers,max(torrents.seeders) as seeders,max(torrents.times_completed) as times_completed')->leftJoin('torrents', 'torrents.info_hash', '=', 'history.info_hash')
->where('history.user_id', '=', $user->id)
->groupBy('history.info_hash')->orderBy('completed_at', 'desc')
->paginate(50);
}
return view($logger, [
'route' => 'downloads',
'user' => $user,
'downloads' => $downloads,
]);
}
$logger = 'user.downloads';
if (config('hitrun.enabled') == true) {
$downloads = History::with(['torrent' => function ($query) {
$query->withAnyStatus();
}])->selectRaw('distinct(history.info_hash), max(torrents.id), max(history.completed_at) as completed_at, max(history.created_at) as created_at, max(history.id) as id, max(history.user_id) as user_id, max(history.seedtime) as seedtime, max(history.seeder) as seeder, max(torrents.size) as size,max(torrents.leechers) as leechers,max(torrents.seeders) as seeders,max(torrents.times_completed) as times_completed')->leftJoin('torrents', 'torrents.info_hash', '=', 'history.info_hash')->where('actual_downloaded', '>', 0)
->whereRaw('history.actual_downloaded > (torrents.size * ('.(config('hitrun.buffer') / 100).'))')
->where('history.user_id', '=', $user->id)
->groupBy('history.info_hash')->orderBy('completed_at', 'desc')
->paginate(50);
} else {
$downloads = History::with(['torrent' => function ($query) {
$query->withAnyStatus();
}])->selectRaw('distinct(history.info_hash), max(torrents.id), max(history.completed_at) as completed_at, max(history.created_at) as created_at, max(history.id) as id, max(history.user_id) as user_id, max(history.seedtime) as seedtime, max(history.seeder) as seeder, max(torrents.size) as size,max(torrents.leechers) as leechers,max(torrents.seeders) as seeders,max(torrents.times_completed) as times_completed')->leftJoin('torrents', 'torrents.info_hash', '=', 'history.info_hash')
->where('history.user_id', '=', $user->id)
->groupBy('history.info_hash')->orderBy('completed_at', 'desc')
->paginate(50);
}
return view($logger, [
'route' => 'downloads',
'user' => $user,
'downloads' => $downloads,
]);
}
/**
@@ -1807,22 +1791,19 @@ class UserController extends Controller
$torrentRequests = TorrentRequest::with(['user', 'category'])->where('user_id', '=', $user->id)->latest()->paginate(25);
return view($logger, [
'route' => 'requests',
'user' => $user,
'torrentRequests' => $torrentRequests,
]);
} else {
$logger = 'user.requests';
$torrentRequests = TorrentRequest::with(['user', 'category'])->where('user_id', '=', $user->id)->where('anon', '!=', 1)->latest()->paginate(25);
return view($logger, [
'route' => 'requests',
'user' => $user,
'torrentRequests' => $torrentRequests,
]);
}
$logger = 'user.requests';
$torrentRequests = TorrentRequest::with(['user', 'category'])->where('user_id', '=', $user->id)->where('anon', '!=', 1)->latest()->paginate(25);
return view($logger, [
'route' => 'requests',
'user' => $user,
'torrentRequests' => $torrentRequests,
]);
}
/**
@@ -1958,16 +1939,14 @@ class UserController extends Controller
'his_downl' => $his_downl,
'his_downl_cre' => $his_downl_cre,
]);
} else {
$logger = 'user.uploads';
$uploads = Torrent::selectRaw('distinct(torrents.id),max(torrents.moderated_at) as moderated_at,max(torrents.slug) as slug,max(torrents.user_id) as user_id,max(torrents.name) as name,max(torrents.category_id) as category_id,max(torrents.size) as size,max(torrents.leechers) as leechers,max(torrents.seeders) as seeders,max(torrents.times_completed) as times_completed,max(torrents.created_at) as created_at,max(torrents.status) as status,count(distinct thanks.id) as thanked_total,sum(bon_transactions.cost) as tipped_total')->where('torrents.user_id', '=', $user->id)->where('torrents.status', '=', 1)->where('torrents.anon', '=', 0)->with(['tips', 'thanks'])->leftJoin('bon_transactions', 'bon_transactions.torrent_id', 'torrents.id')->leftJoin('thanks', 'thanks.torrent_id', 'torrents.id')->groupBy('torrents.id')->orderBy('created_at', 'DESC')->paginate(50);
return view($logger, [
'route' => 'uploads',
'user' => $user,
'uploads' => $uploads,
]);
}
$logger = 'user.uploads';
$uploads = Torrent::selectRaw('distinct(torrents.id),max(torrents.moderated_at) as moderated_at,max(torrents.slug) as slug,max(torrents.user_id) as user_id,max(torrents.name) as name,max(torrents.category_id) as category_id,max(torrents.size) as size,max(torrents.leechers) as leechers,max(torrents.seeders) as seeders,max(torrents.times_completed) as times_completed,max(torrents.created_at) as created_at,max(torrents.status) as status,count(distinct thanks.id) as thanked_total,sum(bon_transactions.cost) as tipped_total')->where('torrents.user_id', '=', $user->id)->where('torrents.status', '=', 1)->where('torrents.anon', '=', 0)->with(['tips', 'thanks'])->leftJoin('bon_transactions', 'bon_transactions.torrent_id', 'torrents.id')->leftJoin('thanks', 'thanks.torrent_id', 'torrents.id')->groupBy('torrents.id')->orderBy('created_at', 'DESC')->paginate(50);
return view($logger, [
'route' => 'uploads',
'user' => $user,
'uploads' => $uploads,
]);
}
/**
@@ -2108,11 +2087,10 @@ class UserController extends Controller
// The Torrent File Exist?
if (!file_exists(getcwd().'/files/torrents/'.$torrent->file_name)) {
return redirect()->back()->withErrors('Torrent File Not Found! Please Report This Torrent!');
} else {
// Delete The Last Torrent Tmp File If Exist
if (file_exists(getcwd().'/files/tmp/'.$tmpFileName)) {
unlink(getcwd().'/files/tmp/'.$tmpFileName);
}
}
// Delete The Last Torrent Tmp File If Exist
if (file_exists(getcwd().'/files/tmp/'.$tmpFileName)) {
unlink(getcwd().'/files/tmp/'.$tmpFileName);
}
// Get The Content Of The Torrent
@@ -2136,9 +2114,8 @@ class UserController extends Controller
if (file_exists($zip_file)) {
return response()->download($zip_file)->deleteFileAfterSend(true);
} else {
return redirect()->back()->withErrors('Something Went Wrong!');
}
return redirect()->back()->withErrors('Something Went Wrong!');
}
/**