Passed
Pull Request — develop (#1)
by
unknown
04:23
created

StudentController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 2
dl 0
loc 26
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Judite\Models\Student;
6
use Illuminate\Support\Facades\DB;
7
use App\Http\Controllers\Controller;
8
use App\Judite\Contracts\Registry\ExchangeRegistry;
9
10
class StudentController extends Controller
11
{
12
    /**
13
     * Display a resource of given id.
14
     *
15
     * @param int $id
16
     *
17
     * @return \Illuminate\View\View
18
     */
19
    public function show($id, ExchangeRegistry $exchangeLogger)
20
    {
21
        $data = DB::transaction(function () use ($id, $exchangeLogger) {
22
            $data = [];
23
            $student = Student::findOrFail($id);
24
            $data['student'] = $student;
25
            $data['enrollments'] = $student
26
                ->enrollments()
27
                ->orderByCourse()
28
                ->withCount('exchangesAsSource')
29
                ->get();
30
            $data['requestedExchanges'] = $student->requestedExchanges()->get();
31
            $data['proposedExchanges'] = $student->proposedExchanges()->get();
32
            $data['historyExchanges'] = $exchangeLogger->historyOfStudent($student);
33
34
            return $data;
35
        });
36
37
        // Group all enrollments by the year of their associated course, so
38
        // the enrollments listing is organized by year. This will allow
39
        // a better experience, since it matches the official order.
40
        $data['enrollments'] = $data['enrollments']->groupBy(function ($enrollment) {
41
            return $enrollment->course->present()->getOrdinalYear();
42
        });
43
44
        return view('students.show', $data);
45
    }
46
}
47