Passed
Push — develop ( faa3f8...82f9ab )
by Francisco
03:19
created

EnrollmentController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 12
rs 9.4285
c 1
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 EnrollmentController extends Controller
11
{
12
    /**
13
     * Create a new controller instance.
14
     */
15
    public function __construct()
16
    {
17
        $this->middleware('can.enroll');
18
    }
19
20
    /**
21
     * Store a newly created resource in storage.
22
     *
23
     * @param int $courseId
24
     *
25
     * @return \Illuminate\Http\Response
26
     */
27
    public function store($courseId)
28
    {
29
        try {
30
            $course = DB::transaction(function () use ($courseId) {
31
                $course = Course::findOrFail($courseId);
32
                Auth::user()->student->enroll($course);
0 ignored issues
show
Bug introduced by
Accessing student on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
34
                return $course;
35
            });
36
37
            flash("You have successfully enrolled in {$course->name}.")->success();
38
        } catch (UserIsAlreadyEnrolledInCourseException $e) {
39
            $course = $e->getCourse();
40
            flash("You are already enrolled in {$course->name}.")->error();
41
        }
42
43
        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...
44
    }
45
46
    /**
47
     * Remove the specified resource from storage.
48
     *
49
     * @param int $courseId
50
     *
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function destroy($courseId)
54
    {
55
        $course = DB::transaction(function () use ($courseId) {
56
            $course = Course::findOrFail($courseId);
57
            student()->removeEnrollmentInCourse($course);
58
59
            return $course;
60
        });
61
62
        flash("You have successfully deleted the enrollment in {$course->name}.")->success();
63
64
        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...
65
    }
66
}
67