Passed
Push — master ( 637398...2f17a8 )
by Matt
14:04
created

QuizController::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Models\Quiz;
7
use App\Models\User;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\Auth;
11
use Validator;
12
use App\Repositories\Repository;
13
14
/**
15
 * @TODO separate controllers for host and user. 
16
 * implement repositories.
17
 */
18
19
class QuizController extends Controller
20
{
21
22
    protected $model;
23
24
    public function __construct(Quiz $quiz)
25
    {
26
        $this->model = new Repository($quiz);
27
    }
28
29
    public function showIndexWelcome()
30
    {
31
        return view('index.welcome');
32
    }
33
34
    public function showHostDashboard()
35
    {
36
        // Move to trait/facade/logic class @TODO
37
        $quizCount = DB::table('user_quizzes')->where('user_id', Auth::user()->id)->count();
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
38
        return view('quiz_host.dashboard.index')->with('quizCount', $quizCount);
39
    }
40
41
    public function showHostManageQuizzes()
42
    {
43
        $quizzes = Quiz::leftJoin('user_quizzes', 'user_quizzes.quiz_id', 'quizzes.id')
44
                        ->join('users', 'user_quizzes.user_id', '=', 'users.id')
45
                        ->where('users.id', Auth::user()->id)
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
                        ->get();
47
48
        return view('quiz_host.dashboard.manage-quizzes')->with('quizzes', $quizzes);
49
    }
50
51
    public function create()
52
    {
53
        return view('quiz_host.dashboard.quiz.create');
54
    }
55
56
    public function store(Request $request)
57
    {
58
        // @TODO use validator facade over $request->validate();
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
        $validator = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
60
            'quiz_name'        => 'required|max:30',
61
            'quiz_description' => 'required|max:500'
62
        ]);
63
64
        $quiz = new Quiz;
65
        $quiz->quiz_name = $request->get('quiz_name');
66
        $quiz->quiz_description = $request->get('quiz_description');
67
        $quiz->active = '0';
68
        $quiz->quiz_pin = '5555';
69
        $quiz->save();
70
71
        $user = Auth::user();
72
        $quiz->user()->attach($user);
73
74
        return redirect()->route('quiz_host.dashboard.manage-quizzes')->with('quizCreated', 'Whoa ' . Auth::user()->username . ', you have created a quiz! Now it\'s time to add some questions');
0 ignored issues
show
Bug introduced by
Accessing username on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
75
76
    }
77
78
    public function edit($id) 
79
    {
80
        try {
81
            $quiz = Quiz::findOrFail($id);
82
        } catch (ModelNotFoundException $e) {
83
            return redirect()->route('quiz_host.dashboard.manage-quizzes');
84
        }
85
86
        return view('quiz_host.dashboard.quiz.edit')->with('quiz', $quiz);
87
    }
88
89
    public function destroy($id) 
90
    {
91
        try {
92
            $quiz = Quiz::findOrFail($id);
93
        } catch (ModelNotFoundException $e) {
94
            return redirect()->route('quiz_host.dashboard.manage-quizzes');
95
        }
96
97
        $quiz->delete();
98
        $quiz->user()->detach(Auth::user());
99
        return redirect()->route('quiz_host.dashboard.manage-quizzes')->with('quizDeleted', 'Quiz succesfully deleted.');
100
101
    }
102
103
    public function showQuestion()
104
    {
105
        return view('quiz_user.question');
106
    }
107
108
    public function showPin()
109
    {
110
        return view('quiz_user.pin');
111
    }
112
113
    public function leaderboards()
114
    {
115
        return view('quiz_user.leaderboard');
116
    }
117
118
119
    public function showSplash()
120
    {
121
        return view('quiz_user.splash');
122
    }
123
124
    public function enterQuizPin(Request $request)
125
    {
126
        // @TODO - PIN max length needs to be dynamic with trait
127
        // will sort out soon - Matt.
128
        $validatedData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedData is dead and can be removed.
Loading history...
129
            'pin'      => 'required|string'
130
        ]);
131
132
        try {
133
            $quiz = Quiz::where('quiz_pin', $request->get('pin'))
134
                    ->where('active', '1')
135
                    ->firstOrFail();
136
        } catch (ModelNotFoundException $e) {
137
            return back()->with('pin_error', 'Either the quiz doesn\'t exist, or it hasn\'t been activated');
138
        }
139
140
        //Add user to the socket.io stuff!
141
        event(new \App\Events\addUser($request->get('username'))); //Adds the user to the socket stuff
142
        return redirect()->route('quiz_user.showSplash')->with('quizData', $quiz);
143
    }
144
}
145