AFSantiagoEnrollmentSheetService::exportToWord()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 52
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 38
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 52
rs 7.7564

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Services;
4
5
use App\Interfaces\EnrollmentSheetInterface;
6
use App\Models\Enrollment;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Str;
9
use PhpOffice\PhpWord\Element\Table;
10
use PhpOffice\PhpWord\SimpleType\JcTable;
11
use PhpOffice\PhpWord\TemplateProcessor;
12
13
class AFSantiagoEnrollmentSheetService implements EnrollmentSheetInterface
14
{
15
    public function __construct()
16
    {
17
        if (config('enrollment-sheet.style' !== 'afsantiago')) {
18
            abort(403);
19
        }
20
    }
21
22
    private function utf8_for_xml($string)
23
    {
24
        return preg_replace('/^[\p{L}\p{N}_-]+$/u', ' ', $string);
25
    }
26
27
    public function exportToWord(Enrollment $enrollment)
28
    {
29
        App::setLocale(config('app.locale'));
30
        $templateProcessor = new TemplateProcessor(storage_path('afsantiago/enrollment.docx'));
31
32
        $templateProcessor->setValue('enrollment_date', $enrollment->date);
33
        $templateProcessor->setValue('name', $enrollment->student_name);
34
35
        $nif = $enrollment->student->idnumber ?: '';
36
        $phone = $enrollment->student->phone->count() > 0 && $enrollment->student->phone->first()->phone_number ? $enrollment->student->phone->first()->phone_number : '';
37
        $email = $enrollment->student->email ?: '';
38
        $address = $enrollment->student->address ?: '';
39
        $city = $enrollment->student->city ?: '';
40
41
        $templateProcessor->setValue('address', $address);
42
        $templateProcessor->setValue('city', $city);
43
        $templateProcessor->setValue('phone', $phone);
44
        $templateProcessor->setValue('nif', $nif);
45
        $templateProcessor->setValue('email', $email);
46
47
        $templateProcessor->setValue('description', $this->utf8_for_xml($enrollment->course->name ?? ''));
48
        $templateProcessor->setValue('start_date', $enrollment->course->formatted_start_date);
49
        $templateProcessor->setValue('end_date', $enrollment->course->formatted_end_date);
50
        $templateProcessor->setValue('volume', $enrollment->course->volume);
51
52
        $table = new Table(['borderSize' => 8,
53
            'borderColor' => 'black',
54
            'cellMargin' => 80,
55
            'alignment' => JcTable::CENTER,
56
            'cellSpacing' => 50,
57
            'width' => 100 * 50, ]);
58
59
        $firstRowStyle = ['bgColor' => 'd9d9d9'];
60
61
        $table->addRow(500, $firstRowStyle);
62
        $table->addCell(4000, $firstRowStyle)->addText(Str::upper(__('Due Date')));
0 ignored issues
show
Bug introduced by
It seems like __('Due Date') can also be of type array and array; however, parameter $value of Illuminate\Support\Str::upper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $table->addCell(4000, $firstRowStyle)->addText(Str::upper(/** @scrutinizer ignore-type */ __('Due Date')));
Loading history...
63
        $table->addCell(5000, $firstRowStyle)->addText(Str::upper(__('Total')));
64
65
        if ($enrollment->scheduledPayments->count() > 0) {
66
            foreach ($enrollment->scheduledPayments as $payment) {
67
                $table->addRow(500);
68
                $table->addCell(4000)->addText($payment->date_for_humans, [], ['spaceAfter' => 0]);
69
                $table->addCell(5000)->addText($payment->value_with_currency, [], ['spaceAfter' => 0]);
70
            }
71
            $templateProcessor->setComplexBlock('payments', $table);
72
        } else {
73
            $templateProcessor->setValue('payments', '');
74
        }
75
76
        $path = $templateProcessor->save();
77
78
        return response()->download($path)->deleteFileAfterSend(true);
79
    }
80
}
81