Passed
Pull Request — develop (#11)
by
unknown
03:46 queued 56s
created

InvitationController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 0
loc 28
rs 9.6666
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
        $inputStudentNumber = $request->input('student_number');
56
57
        $authStudentNumber = DB::transaction(function () {
58
            return Auth::student()->student_number;
59
        });
60
61
        if ($inputStudentNumber == $authStudentNumber) {
62
            flash('You can not invite yourself.')->error();
63
64
            return redirect()->back();
65
        }
66
67
        try {
68
            Invitation::create($inputStudentNumber, $groupId, $courseId);
69
            flash('Invitation successfully sent.')->success();
70
        } catch (UserHasAlreadyAnInviteInGroupException $e) {
71
            flash('This student was already invited.')->error();
72
        }
73
74
        return redirect()->back();
75
    }
76
77
    /**
78
     * Update the specified resource in storage.
79
     *
80
     * @param int $inviteId
81
     *
82
     * @return \Illuminate\Http\RedirectResponse
83
     */
84
    public function update($inviteId)
85
    {
86
        $invitation = DB::transaction(function () use ($inviteId) {
87
            return Invitation::with(['group.memberships', 'group.course'])
88
                ->whereId($inviteId)
89
                ->whereStudentNumber(Auth::student()->student_number)
90
                ->first();
91
        });
92
93
        if ($invitation == null) {
94
            flash('Invalid invitation.')->error();
95
96
            return redirect()->back();
97
        }
98
99
        $numberOfGroupMembers = $invitation->group->memberships->count();
100
        $groupMaxSize = $invitation->group->course->group_max;
101
102
        if ($numberOfGroupMembers >= $groupMaxSize) {
103
            flash('Course group limit exceeded')->error();
104
105
            return redirect()->back();
106
        }
107
108
        $courseId = $invitation->course_id;
109
110
        try {
111
            Auth::student()->join($invitation->group);
112
113
            flash('You have successfully joined the group.')
114
                ->success();
115
116
            $invitation->delete();
117
        } 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...
118
            flash('You already have a group.')
119
                ->error();
120
        }
121
122
        return redirect()->route('groups.show', compact('courseId'));
123
    }
124
125
    /**
126
     * Remove the specified resource from storage.
127
     *
128
     * @param int $id
129
     *
130
     * @return \Illuminate\Http\RedirectResponse
131
     */
132
    public function destroy($id)
133
    {
134
        $courseId = 0;
135
136
        $remainingInvitations = DB::transaction(function () use ($id, &$courseId) {
137
            $invitation = Invitation::find($id)
138
                ->whereStudentNumber(Auth::student()->student_number)
139
                ->first();
140
141
            $courseId = $invitation->course_id;
142
            $studentNumber = $invitation->student_number;
143
144
            $invitation->delete();
145
146
            return Invitation::with('group.memberships.student.user')
147
                ->where([
148
                    ['student_number', $studentNumber],
149
                    ['course_id', $courseId],
150
                ])
151
                ->count();
152
        });
153
154
        flash('Invitation destroyed.')->success();
155
156
        if ($remainingInvitations > 0) {
157
            return redirect()->back();
158
        } else {
159
            return redirect()->route('groups.show', compact('courseId'));
160
        }
161
    }
162
}
163