CourseEnrollmentController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A store() 0 15 2
A destroy() 0 17 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Judite\Models\Course;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\Auth;
8
use App\Exceptions\EnrollmentCannotBeDeleted;
9
use App\Exceptions\StudentIsNotEnrolledInCourseException;
10
use App\Exceptions\UserIsAlreadyEnrolledInCourseException;
11
12
class CourseEnrollmentController 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.enroll');
23
    }
24
25
    /**
26
     * Store a newly created resource in storage.
27
     *
28
     * @param int $id
29
     *
30
     * @return \Illuminate\Http\RedirectResponse
31
     */
32
    public function store($id)
33
    {
34
        try {
35
            $course = DB::transaction(function () use ($id) {
36
                $course = Course::findOrFail($id);
37
                Auth::student()->enroll($course);
38
39
                return $course;
40
            });
41
            flash("You have successfully enrolled in {$course->name}.")->success();
42
        } catch (UserIsAlreadyEnrolledInCourseException $e) {
43
            flash("You are already enrolled in {$e->getCourse()->name}.")->error();
44
        }
45
46
        return redirect()->route('courses.index');
47
    }
48
49
    /**
50
     * Remove the specified resource from storage.
51
     *
52
     * @param int $id
53
     *
54
     * @return \Illuminate\Http\RedirectResponse
55
     */
56
    public function destroy($id)
57
    {
58
        try {
59
            $course = DB::transaction(function () use ($id) {
60
                $course = Course::findOrFail($id);
61
                Auth::student()->unenroll($course);
62
63
                return $course;
64
            });
65
            flash("You have successfully deleted the enrollment in {$course->name}.")->success();
66
        } catch (StudentIsNotEnrolledInCourseException $e) {
67
            flash('You cannot only delete enrollments that you have enrolled.')->error();
68
        } catch (EnrollmentCannotBeDeleted $e) {
69
            flash('You cannot delete an enrollment that already has a shift.')->error();
70
        }
71
72
        return redirect()->route('courses.index');
73
    }
74
}
75