Passed
Push — master ( 26caeb...0e28f7 )
by Thomas
11:18
created

AFSantiagoEnrollmentSheetService::exportToWord()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 47
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 33
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 47
rs 8.0555
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\TemplateProcessor;
11
12
class AFSantiagoEnrollmentSheetService implements EnrollmentSheetInterface
13
{
14
    public function __construct()
15
    {
16
        if (config('enrollment-sheet.style' !== 'afsantiago')) {
17
            abort(403);
18
        }
19
    }
20
21
    private function utf8_for_xml($string)
22
    {
23
        return preg_replace('/^[\p{L}\p{N}_-]+$/u', ' ', $string);
24
    }
25
26
    public function exportToWord(Enrollment $enrollment)
27
    {
28
        App::setLocale(config('app.locale'));
29
        $templateProcessor = new TemplateProcessor(storage_path('afsantiago/enrollment.docx'));
30
31
        $templateProcessor->setValue('enrollment_date', $enrollment->date);
32
        $templateProcessor->setValue('name', $enrollment->student_name);
33
34
        $nif = $enrollment->student->idnumber ?: '';
35
        $phone = $enrollment->student->phone->count() > 0 && $enrollment->student->phone->first()->phone_number ? $enrollment->student->phone->first()->phone_number : '';
0 ignored issues
show
Bug introduced by
The method count() does not exist on Illuminate\Database\Eloquent\Collection. ( Ignorable by Annotation )

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

35
        $phone = $enrollment->student->phone->/** @scrutinizer ignore-call */ count() > 0 && $enrollment->student->phone->first()->phone_number ? $enrollment->student->phone->first()->phone_number : '';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method first() does not exist on Illuminate\Database\Eloquent\Collection. ( Ignorable by Annotation )

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

35
        $phone = $enrollment->student->phone->count() > 0 && $enrollment->student->phone->/** @scrutinizer ignore-call */ first()->phone_number ? $enrollment->student->phone->first()->phone_number : '';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
        $email = $enrollment->student->email ?: '';
37
        $address = $enrollment->student->address ?: '';
38
        $city = $enrollment->student->city ?: '';
39
40
        $templateProcessor->setValue('address', $address);
41
        $templateProcessor->setValue('city', $city);
42
        $templateProcessor->setValue('phone', $phone);
43
        $templateProcessor->setValue('nif', $nif);
44
        $templateProcessor->setValue('email', $email);
45
46
        $templateProcessor->setValue('description', $this->utf8_for_xml($enrollment->course->name ?? ''));
47
        $templateProcessor->setValue('start_date', $enrollment->course->formatted_start_date);
48
        $templateProcessor->setValue('end_date', $enrollment->course->formatted_end_date);
49
        $templateProcessor->setValue('volume', $enrollment->course->volume);
50
51
        $table = new Table(['borderSize' => 8, 'borderColor' => 'black', 'cellMargin' => 80, 'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER, 'cellSpacing' => 50, 'width' => 100 * 50]);
52
53
        $firstRowStyle = ['bgColor' => 'd9d9d9'];
54
55
        $table->addRow(500, $firstRowStyle);
56
        $table->addCell(4000, $firstRowStyle)->addText(Str::upper(__('Due Date')));
0 ignored issues
show
Bug introduced by
The method upper() does not exist on Illuminate\Support\Str. ( Ignorable by Annotation )

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

56
        $table->addCell(4000, $firstRowStyle)->addText(Str::/** @scrutinizer ignore-call */ upper(__('Due Date')));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        $table->addCell(5000, $firstRowStyle)->addText(Str::upper(__('Total')));
58
59
        if ($enrollment->scheduledPayments->count() > 0) {
60
            foreach ($enrollment->scheduledPayments as $payment) {
61
                $table->addRow(500);
62
                $table->addCell(4000)->addText($payment->date_for_humans, [], ['spaceAfter' => 0]);
63
                $table->addCell(5000)->addText($payment->value_with_currency, [], ['spaceAfter' => 0]);
64
            }
65
            $templateProcessor->setComplexBlock('payments', $table);
66
        } else {
67
            $templateProcessor->setValue('payments', '');
68
        }
69
70
        $path = $templateProcessor->save();
71
72
        return response()->download($path)->deleteFileAfterSend(true);
73
    }
74
}
75