Passed
Push — develop ( a18085...de8258 )
by Francisco
14:47
created

CourseEnrollmentController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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\UserIsAlreadyEnrolledInCourseException;
9
10
class CourseEnrollmentController extends Controller
11
{
12
    /**
13
     * Create a new controller instance.
14
     */
15
    public function __construct()
16
    {
17
        $this->middleware('auth');
18
        $this->middleware('can.student');
19
        $this->middleware('student.verified');
20
        $this->middleware('can.enroll');
21
    }
22
23
    /**
24
     * Store a newly created resource in storage.
25
     *
26
     * @param int $id
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function store($id)
31
    {
32
        try {
33
            $course = DB::transaction(function () use ($id) {
34
                $course = Course::findOrFail($id);
35
                student()->enroll($course);
36
37
                return $course;
38
            });
39
40
            flash("You have successfully enrolled in {$course->name}.")->success();
41
        } catch (UserIsAlreadyEnrolledInCourseException $e) {
42
            $course = $e->getCourse();
43
            flash("You are already enrolled in {$course->name}.")->error();
44
        }
45
46
        return redirect()->route('courses.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('courses.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
47
    }
48
49
    /**
50
     * Remove the specified resource from storage.
51
     *
52
     * @param int $id
53
     *
54
     * @return \Illuminate\Http\Response
55
     */
56
    public function destroy($id)
57
    {
58
        $course = DB::transaction(function () use ($id) {
59
            $course = Course::findOrFail($id);
60
            student()->unenroll($course);
61
62
            return $course;
63
        });
64
65
        flash("You have successfully deleted the enrollment in {$course->name}.")->success();
66
67
        return redirect()->route('courses.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('courses.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
68
    }
69
}
70