Passed
Push — develop ( dfa246...179c36 )
by Francisco
02:27
created

DashboardController::studentDashboard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 23
rs 9.0856
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
9
class DashboardController extends Controller
10
{
11
    /**
12
     * Create a new controller instance.
13
     */
14
    public function __construct()
15
    {
16
        $this->middleware('auth');
17
    }
18
19
    /**
20
     * Show the dashboard.
21
     *
22
     * @return \Illuminate\Http\Response
23
     */
24
    public function index()
25
    {
26
        return auth()->user()->isAdmin()
27
            ? $this->adminDashboard()
28
            : $this->studentDashboard();
29
    }
30
31
    /**
32
     * Get the admin's dashboard.
33
     *
34
     * @return \Illuminate\Http\Response
35
     */
36 View Code Duplication
    protected function adminDashboard()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $courses = DB::transaction(function () {
39
            return Course::withCount('enrollments')
40
                ->orderedList()
41
                ->get();
42
        });
43
44
        $courses = $courses->groupBy(function ($course) {
45
            return $course->present()->getOrdinalYear();
46
        });
47
48
        return view('admin.dashboard', compact('courses'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('admin.dashb...d', compact('courses')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
49
    }
50
51
    /**
52
     * Get the student's dashboard.
53
     *
54
     * @return \Illuminate\Http\Response
55
     */
56
    protected function studentDashboard()
57
    {
58
        $data = DB::transaction(function () {
59
            $student = Auth::user()->student;
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...
60
            $data['enrollments'] = $student
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
61
                ->enrollments()
62
                ->orderByCourse()
63
                ->withCount('exchangesAsSource')
64
                ->get();
65
            $data['requestedExchanges'] = $student->requestedExchanges()->get();
66
            $data['proposedExchanges'] = $student->proposedExchanges()->get();
67
68
            return $data;
69
        });
70
71
        // Group all enrollments by the year of their associated course, so
72
        // the enrollments listing is organized by year. This will allow
73
        // a better experience, since it matches the official order.
74
        $data['enrollments'] = $data['enrollments']->groupBy(function ($enrollment) {
75
            return $enrollment->course->present()->getOrdinalYear();
76
        });
77
78
        return view('dashboard', $data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('dashboard', $data) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
79
    }
80
}
81