AFLojaCertificatesService::exportCertificate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 24
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 32
rs 9.536
1
<?php
2
3
namespace App\Services;
4
5
use App\Interfaces\CertificatesInterface;
6
use App\Models\Course;
7
use App\Models\Enrollment;
8
use Illuminate\Support\Facades\App;
9
use Illuminate\Support\Facades\Gate;
10
use Mpdf\Config\ConfigVariables;
11
use Mpdf\Config\FontVariables;
12
use Mpdf\Mpdf;
13
14
class AFLojaCertificatesService implements CertificatesInterface
15
{
16
    public function __construct()
17
    {
18
        if (config('certificates-generation.style' !== 'afloja')) {
19
            abort(403);
20
        }
21
    }
22
23
    public function exportCertificate(Enrollment $enrollment)
24
    {
25
        if (Gate::forUser(backpack_user())->denies('view-enrollment', $enrollment)) {
26
            abort(403);
27
        }
28
29
        App::setLocale('es');
30
31
        $defaultConfig = (new ConfigVariables())->getDefaults();
32
        $fontDirs = $defaultConfig['fontDir'];
33
34
        $defaultFontConfig = (new FontVariables())->getDefaults();
35
        $fontData = $defaultFontConfig['fontdata'];
36
37
        $mpdf = new Mpdf(['mode' => 'utf-8',
38
            'format' => 'a4',
39
            'orientation' => 'L',
40
            'margin_left' => 25,
41
            'margin_right' => 25,
42
            'margin_top' => 25,
43
            'margin_bottom' => 25,
44
            'margin_header' => 0,
45
            'margin_footer' => 0,
46
            'tempDir' => storage_path('temp'),
47
            'fontDir' => array_merge($fontDirs, [storage_path('afloja/fonts')]),
48
            'fontdata' => $fontData + ['bodoni' => ['R' => 'BOD_R.TTF',
49
                'B' => 'BOD_B.TTF', ],
50
                'frenchscript' => ['R' => 'FRSCRIPT.TTF'], ],
51
            'default_font' => 'bodoni', ]);
52
53
        $mpdf->WriteHTML(view('results.certificate', ['enrollment' => $enrollment])->render());
54
        $mpdf->Output();
55
    }
56
57
    public function exportCourseResults(Course $course)
58
    {
59
        if (Gate::forUser(backpack_user())->denies('view-course', $course)) {
60
            abort(403);
61
        }
62
63
        App::setLocale('es');
64
65
        $defaultConfig = (new ConfigVariables())->getDefaults();
66
        $fontDirs = $defaultConfig['fontDir'];
67
68
        $defaultFontConfig = (new FontVariables())->getDefaults();
69
        $fontData = $defaultFontConfig['fontdata'];
70
71
        $mpdf = new Mpdf(['mode' => 'utf-8',
72
            'format' => 'a4',
73
            'orientation' => 'L',
74
            'margin_left' => 25,
75
            'margin_right' => 25,
76
            'margin_top' => 25,
77
            'margin_bottom' => 25,
78
            'margin_header' => 0,
79
            'margin_footer' => 0,
80
            'tempDir' => storage_path('temp'),
81
            'fontDir' => array_merge($fontDirs, [storage_path('afloja/fonts')]),
82
            'fontdata' => $fontData + ['calibri' => ['R' => 'calibri.ttf',
83
                'B' => 'calibrib.ttf', ]],
84
            'default_font' => 'calibri', ]);
85
86
        $mpdf->WriteHTML(view('results.course-export', ['enrollments' => $course->enrollments()->with('grades')->get(),
87
            'course' => $course, ])->render());
88
        $mpdf->Output();
89
    }
90
91
    public function exportResult(Enrollment $enrollment)
92
    {
93
        if (Gate::forUser(backpack_user())->denies('view-enrollment', $enrollment)) {
94
            abort(403);
95
        }
96
97
        App::setLocale('es');
98
99
        $defaultConfig = (new ConfigVariables())->getDefaults();
100
        $fontDirs = $defaultConfig['fontDir'];
101
102
        $defaultFontConfig = (new FontVariables())->getDefaults();
103
        $fontData = $defaultFontConfig['fontdata'];
104
105
        $mpdf = new Mpdf(['mode' => 'utf-8',
106
            'format' => 'a4',
107
            'orientation' => 'P',
108
            'margin_left' => 25,
109
            'margin_right' => 25,
110
            'margin_top' => 25,
111
            'margin_bottom' => 25,
112
            'margin_header' => 0,
113
            'margin_footer' => 0,
114
            'tempDir' => storage_path('temp'),
115
            'fontDir' => array_merge($fontDirs, [storage_path('afloja/fonts')]),
116
            'fontdata' => $fontData + ['calibri' => ['R' => 'calibri.ttf',
117
                'B' => 'calibrib.ttf', ]],
118
            'default_font' => 'calibri', ]);
119
120
        $mpdf->WriteHTML(view('results.export', ['grades' => $enrollment->grades,
121
            'enrollment' => $enrollment, ])->render());
122
        $mpdf->Output();
123
    }
124
}
125