Passed
Pull Request — develop (#11)
by
unknown
02:56
created

InvitationController::update()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 6
nop 1
dl 0
loc 39
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Judite\Models\Group;
6
use App\Judite\Models\Invitation;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Auth;
9
use App\Http\Requests\Invitation\UpdateRequest;
10
use App\Exceptions\UserHasAlreadyAnInviteInGroupException;
11
12
class InvitationController extends Controller
13
{
14
    /**
15
     * Create a new controller instance.
16
     */
17
    public function __construct()
18
    {
19
        $this->middleware('auth');
20
        $this->middleware('can.student');
21
        $this->middleware('student.verified');
22
        $this->middleware('can.group');
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\View\View
29
     */
30
    public function index($courseId)
31
    {
32
        $invitations = DB::transaction(function () use ($courseId) {
33
            return Invitation::with('group.memberships.student.user')
34
                ->where([
35
                    ['student_number', Auth::student()->student_number],
36
                    ['course_id', $courseId],
37
                ])
38
                ->get();
39
        });
40
41
        return view('invitations.index', compact('invitations'));
42
    }
43
44
    /**
45
     * Store a newly created resource in storage.
46
     *
47
     * @param \App\Http\Requests\Invitation\UpdateRequest $request
48
     * @param $groupId
49
     * @param $courseId
50
     *
51
     * @return \Illuminate\Http\RedirectResponse
52
     */
53
    public function store(UpdateRequest $request, $groupId, $courseId)
54
    {
55
        $studentNumber = $request->input('student_number');
56
57
        if ($studentNumber == Auth::student()->student_number) {
58
            flash('You can not invite yourself.')->error();
59
60
            return redirect()->back();
61
        }
62
63
        try {
64
            Invitation::create($studentNumber, $groupId, $courseId);
65
            flash('Invitation successfully sent.')->success();
66
        } catch (UserHasAlreadyAnInviteInGroupException $e) {
67
            flash('This student was already invited.')->error();
68
        }
69
70
        return redirect()->back();
71
    }
72
73
    /**
74
     * Update the specified resource in storage.
75
     *
76
     * @param int $inviteId
77
     *
78
     * @return \Illuminate\Http\RedirectResponse
79
     */
80
    public function update($inviteId)
81
    {
82
        $invitation = DB::transaction(function () use ($inviteId) {
83
            return Invitation::with(['group.memberships', 'group.course'])
84
                ->whereId($inviteId)
85
                ->whereStudentNumber(Auth::student()->student_number)
86
                ->first();
87
        });
88
89
        if ($invitation == null) {
90
            flash('Invalid invitation.')->error();
91
92
            return redirect()->back();
93
        }
94
95
        $numberOfGroupMembers = $invitation->group->memberships->count();
96
        $groupMaxSize = $invitation->group->course->group_max;
97
98
        if ($numberOfGroupMembers >= $groupMaxSize) {
99
            flash('Course group limit exceeded')->error();
100
101
            return redirect()->back();
102
        }
103
104
        $courseId = $invitation->course_id;
105
106
        try {
107
            Auth::student()->join($invitation->group);
108
109
            flash('You have successfully joined the group.')
110
                ->success();
111
112
            $invitation->delete();
113
        } catch (UserHasAlreadyGroupInCourseException $e) {
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Use...yGroupInCourseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
114
            flash('You already have a group.')
115
                ->error();
116
        }
117
118
        return redirect()->route('groups.show', compact('courseId'));
119
    }
120
121
    /**
122
     * Remove the specified resource from storage.
123
     *
124
     * @param int $id
125
     *
126
     * @return \Illuminate\Http\RedirectResponse
127
     */
128
    public function destroy($id)
129
    {
130
        $courseId = 0;
131
132
        $remainingInvitations = DB::transaction(function () use ($id, &$courseId) {
133
            $invitation = Invitation::find($id)
134
                ->whereStudentNumber(Auth::student()->student_number)
135
                ->first();
136
137
            $courseId = $invitation->course_id;
138
            $studentNumber = $invitation->student_number;
139
140
            $invitation->delete();
141
142
            return Invitation::with('group.memberships.student.user')
143
                ->where([
144
                    ['student_number', $studentNumber],
145
                    ['course_id', $courseId],
146
                ])
147
                ->count();
148
        });
149
150
        flash('Invitation destroyed.')->success();
151
152
        if ($remainingInvitations > 0) {
153
            return redirect()->back();
154
        } else {
155
            return redirect()->route('groups.show', compact('courseId'));
156
        }
157
    }
158
}
159