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
+44 -48
View File
@@ -172,66 +172,62 @@ class TopicController extends Controller
'forum_id' => 'required',
]);
if ($v->fails()) {
return redirect()->route('forums.index')
->withErrors($v->errors());
}
$topic->save();
$post = new Post();
$post->content = $request->input('content');
$post->user_id = $user->id;
$post->topic_id = $topic->id;
$v = validator($post->toArray(), [
'content' => 'required',
'user_id' => 'required',
'topic_id' => 'required',
]);
if ($v->fails()) {
return redirect()->route('forums.index')
->withErrors($v->errors());
} else {
$post->save();
$topic->num_post = 1;
$topic->last_reply_at = $post->created_at;
$topic->save();
$forum->num_topic = $forum->getTopicCount($forum->id);
$forum->num_post = $forum->getPostCount($forum->id);
$forum->last_topic_id = $topic->id;
$forum->last_topic_name = $topic->name;
$forum->last_topic_slug = $topic->slug;
$forum->last_post_user_id = $user->id;
$forum->last_post_user_username = $user->username;
$forum->save();
$post = new Post();
$post->content = $request->input('content');
$post->user_id = $user->id;
$post->topic_id = $topic->id;
$forum->notifySubscribers($user, $topic);
$v = validator($post->toArray(), [
'content' => 'required',
'user_id' => 'required',
'topic_id' => 'required',
]);
if ($v->fails()) {
return redirect()->route('forums.index')
->withErrors($v->errors());
} else {
$post->save();
$topic->num_post = 1;
$topic->last_reply_at = $post->created_at;
$topic->save();
$forum->num_topic = $forum->getTopicCount($forum->id);
$forum->num_post = $forum->getPostCount($forum->id);
$forum->last_topic_id = $topic->id;
$forum->last_topic_name = $topic->name;
$forum->last_topic_slug = $topic->slug;
$forum->last_post_user_id = $user->id;
$forum->last_post_user_username = $user->username;
$forum->save();
$forum->notifySubscribers($user, $topic);
// Post To ShoutBox
// Post To ShoutBox
$appurl = config('app.url');
$topicUrl = "{$appurl}/forums/topics/{$topic->id}";
$profileUrl = "{$appurl}/users/{$user->username}";
$topicUrl = "{$appurl}/forums/topics/{$topic->id}";
$profileUrl = "{$appurl}/users/{$user->username}";
$this->chat->systemMessage("[url={$profileUrl}]{$user->username}[/url] has created a new topic [url={$topicUrl}]{$topic->name}[/url]");
$this->chat->systemMessage("[url={$profileUrl}]{$user->username}[/url] has created a new topic [url={$topicUrl}]{$topic->name}[/url]");
//Achievements
//Achievements
$user->unlock(new UserMadeFirstPost(), 1);
$user->addProgress(new UserMade25Posts(), 1);
$user->addProgress(new UserMade50Posts(), 1);
$user->addProgress(new UserMade100Posts(), 1);
$user->addProgress(new UserMade200Posts(), 1);
$user->addProgress(new UserMade300Posts(), 1);
$user->addProgress(new UserMade400Posts(), 1);
$user->addProgress(new UserMade500Posts(), 1);
$user->addProgress(new UserMade600Posts(), 1);
$user->addProgress(new UserMade700Posts(), 1);
$user->addProgress(new UserMade800Posts(), 1);
$user->addProgress(new UserMade900Posts(), 1);
$user->addProgress(new UserMade25Posts(), 1);
$user->addProgress(new UserMade50Posts(), 1);
$user->addProgress(new UserMade100Posts(), 1);
$user->addProgress(new UserMade200Posts(), 1);
$user->addProgress(new UserMade300Posts(), 1);
$user->addProgress(new UserMade400Posts(), 1);
$user->addProgress(new UserMade500Posts(), 1);
$user->addProgress(new UserMade600Posts(), 1);
$user->addProgress(new UserMade700Posts(), 1);
$user->addProgress(new UserMade800Posts(), 1);
$user->addProgress(new UserMade900Posts(), 1);
return redirect()->route('forum_topic', ['id' => $topic->id])
->withSuccess('Topic Created Successfully!');
}
return redirect()->route('forum_topic', ['id' => $topic->id])
->withSuccess('Topic Created Successfully!');
}
}