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

GroupController::destroy()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 2
nop 1
dl 0
loc 22
rs 9.8333
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\Course;
7
use App\Judite\Models\Student;
8
use App\Judite\Models\Invitation;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\Auth;
11
use App\Exceptions\UserHasAlreadyGroupInCourseException;
12
13
class GroupController extends Controller
14
{
15
    /**
16
     * Create a new controller instance.
17
     */
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
        $this->middleware('can.student');
22
        $this->middleware('student.verified');
23
        $this->middleware('can.group');
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\View\View
30
     */
31
    public function index()
32
    {
33
        $enrollments = DB::transaction(function () {
34
            $student = Auth::student();
35
36
            $enrollments = $student
37
                ->enrollments()
38
                ->orderByCourse()
39
                ->get();
40
41
            $this->addStudentGroupInfoToEachEnrollment($student, $enrollments);
42
43
            return $enrollments;
44
        });
45
46
        return view('groups.index', compact('enrollments'));
47
    }
48
49
    /**
50
     * Store a newly created resource in storage.
51
     *
52
     * @param $courseId
53
     *
54
     * @return \Illuminate\Http\RedirectResponse
55
     */
56
    public function store($courseId)
57
    {
58
        $group = DB::transaction(function () use ($courseId) {
59
            $course = Course::findOrFail($courseId);
0 ignored issues
show
Unused Code introduced by
The assignment to $course is dead and can be removed.
Loading history...
60
61
            $group = new Group();
62
            $group->course_id = $courseId;
63
            $group->save();
64
65
            return $group;
66
        });
67
68
        if ($group == null) {
69
            return redirect()->back();
70
        }
71
72
        try {
73
            Auth::student()->join($group);
74
75
            flash('You have successfully joined a group.')->success();
76
        } catch (UserHasAlreadyGroupInCourseException $e) {
77
            flash('You already have a group.')->error();
78
            $group->delete();
79
        }
80
81
        return redirect()->route('groups.show', compact('courseId'));
82
    }
83
84
    /**
85
     * Display the specified resource.
86
     *
87
     * @param int $courseId
88
     *
89
     * @return \Illuminate\View\View
90
     */
91
    public function show($courseId)
92
    {
93
        $student = Auth::student();
94
95
        $course = DB::transaction(function () use ($courseId, $student) {
96
            $course = Course::whereId($courseId)->first();
97
98
            $course->number_invitations = Invitation::ofStudentInCourse(
0 ignored issues
show
Bug introduced by
The property number_invitations does not seem to exist on App\Judite\Models\Course. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
99
                    $student->student_number,
100
                    $courseId
101
                )->count();
102
103
            return $course;
104
        });
105
106
        $membership = DB::transaction(function () use ($courseId, $student) {
107
            return $student->memberships()
108
                ->with('group.memberships.student.user')
109
                ->whereCourseId($courseId)
110
                ->first();
111
        });
112
113
        return view('groups.show', compact('course', 'membership'));
114
    }
115
116
    /**
117
     * Remove the specified resource from storage.
118
     *
119
     * @param int $courseId
120
     *
121
     * @return \Illuminate\Http\RedirectResponse
122
     */
123
    public function destroy($courseId)
124
    {
125
        $group = Auth::student()->memberships()->get()
126
            ->where('course_id', '=', $courseId)->first()
127
            ->group()->first();
128
129
        Auth::student()->leave($group);
130
131
        if (! $group->memberships()->count()) {
132
            $invitations = Invitation::whereGroupId($group->id)->get();
133
134
            foreach ($invitations as $invitation) {
135
                $invitation->delete();
136
            }
137
138
            $group->delete();
139
            flash('Group deleted.')->success();
140
        } else {
141
            flash('You have successfully left the group.')->success();
142
        }
143
144
        return redirect()->route('groups.show', compact('courseId'));
145
    }
146
147
    /**
148
     * Foreach student enrollment adds groups and invites information about the respective course.
149
     *
150
     * @param Student    $student
151
     * @param Collection $enrollments
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Collection 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...
152
     */
153
    private function addStudentGroupInfoToEachEnrollment($student, $enrollments)
154
    {
155
        foreach ($enrollments as $enrollmentKey => $enrollment) {
156
            $course = Course::whereId($enrollment->course_id)->first();
157
158
            if ($course->group_max <= 0) {
159
                unset($enrollments[$enrollmentKey]);
160
                continue;
161
            }
162
163
            $membership = $student->findMembershipByCourse($course->id);
164
165
            if (is_null($membership)) {
166
                $enrollment->group_status = 0;
167
            } else {
168
                $group = Group::with('memberships')
169
                    ->whereId($membership->group_id)
170
                    ->first();
171
172
                $enrollment->group_status = $group->memberships->count();
173
            }
174
175
            $enrollment->name = $course->name;
176
            $enrollment->group_min = $course->group_min;
177
            $enrollment->group_max = $course->group_max;
178
179
            $enrollment->number_invitations = Invitation::ofStudentInCourse(
180
                    $student->student_number,
181
                    $enrollment->course_id
182
                )->count();
183
        }
184
    }
185
}
186