Passed
Branch master (a9cf56)
by Francisco
07:28
created

EnrollmentsExport::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Exports;
4
5
use App\Judite\Models\Enrollment;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\DB;
8
use Maatwebsite\Excel\Concerns\WithMapping;
9
use Maatwebsite\Excel\Concerns\WithHeadings;
10
use Maatwebsite\Excel\Concerns\FromCollection;
11
12
class EnrollmentsExport implements FromCollection, WithMapping, WithHeadings
13
{
14
    /**
15
     * The enrollments collection.
16
     *
17
     * @var \Illuminate\Support\Collection
18
     */
19
    private Collection $enrollments;
20
21
    /**
22
     * Construct a new EnrollmentsExport.
23
     *
24
     * @param \Illuminate\Support\Collection $enrollments
25
     */
26
    public function __construct(\Illuminate\Support\Collection $enrollments = null)
27
    {
28
        $this->enrollments = $enrollments;
29
    }
30
31
    /**
32
     * Headings of the exported enrollments.
33
     *
34
     * @return array
35
     */
36
    public function headings(): array
37
    {
38
        return [
39
            'Course ID',
40
            'Course Name',
41
            'Student ID',
42
            'Student Name',
43
            'Student Email',
44
            'Enrollment Date',
45
            'Shift',
46
        ];
47
    }
48
49
    /**
50
     * The enrollment to export.
51
     *
52
     * @param \App\Judite\Models\Enrollment $enrollment
53
     *
54
     * @return array
55
     */
56
    public function map($enrollment): array
57
    {
58
        return [
59
            $enrollment->course->code,
60
            $enrollment->course->name,
61
            $enrollment->student->student_number,
62
            $enrollment->student->user->name,
63
            $enrollment->student->user->email,
64
            $enrollment->student->created_at,
65
            $enrollment->present()->getShiftTag(''),
66
        ];
67
    }
68
69
    /**
70
     * The enrollments collection to export.
71
     *
72
     * @return \Illuminate\Support\Collection
73
     */
74
    public function collection()
75
    {
76
        if ($this->enrollments) {
77
            return $this->enrollments;
78
        }
79
80
        return DB::transaction(fn () => Enrollment::with('course', 'student', 'shift')
81
            ->get()
82
            ->sortByDesc('courses.name')
83
        );
84
    }
85
}
86