Passed
Push — master ( 20f36e...862b31 )
by Marcel
10:03
created

StudentAbsenceExportController::index()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 2
nop 2
dl 0
loc 14
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\StudentAbsence;
6
use App\Entity\StudentAbsenceAttachment;
7
use App\Http\FlysystemFileResponse;
8
use App\Repository\StudentAbsenceRepositoryInterface;
9
use App\Section\SectionResolverInterface;
10
use App\Settings\StudentAbsenceSettings;
11
use DateTime;
12
use Exception;
13
use League\Flysystem\FilesystemOperator;
14
use Mimey\MimeTypes;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
use Symfony\Component\Routing\Annotation\Route;
19
use Twig\Environment;
20
21
#[Route('/absence/export')]
22
class StudentAbsenceExportController extends AbstractController {
23
24
    #[Route('', name: 'export_student_absences')]
25
    public function index(StudentAbsenceRepositoryInterface $repository, Request $request): Response {
26
        $start = $this->getDateTimeFromRequest($request, 'start');
27
        $end = $this->getDateTimeFromRequest($request, 'end');
28
        $uuids = [ ];
29
30
        if($start !== null && $end !== null && $start < $end) {
31
            $uuids = $repository->findAllUuids($start, $end);
32
        }
33
34
        return $this->render('absences/students/export/index.html.twig', [
35
            'start' => $start,
36
            'end' => $end,
37
            'uuids' => $uuids
38
        ]);
39
    }
40
41
    private function getDateTimeFromRequest(Request $request, string $name): ?DateTime {
42
        $dateAsString = $request->query->get($name);
43
44
        if($dateAsString === null) {
45
            return null;
46
        }
47
48
        try {
49
            return new DateTime($dateAsString);
50
        } catch(Exception $e) {
51
            return null;
52
        }
53
    }
54
55
    #[Route('/{uuid}', name: 'export_student_absence')]
56
    public function absence(StudentAbsence $absence, SectionResolverInterface $sectionResolver, Environment $twig): Response {
57
        $section = $sectionResolver->getSectionForDate($absence->getFrom()->getDate());
0 ignored issues
show
Bug introduced by
It seems like $absence->getFrom()->getDate() can also be of type null; however, parameter $dateTime of App\Section\SectionResol...ce::getSectionForDate() does only seem to accept DateTime, 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

57
        $section = $sectionResolver->getSectionForDate(/** @scrutinizer ignore-type */ $absence->getFrom()->getDate());
Loading history...
58
59
        $json = [
60
            'uuid' => $absence->getUuid()->toString(),
61
            'from' => [
62
                'date' => $absence->getFrom()->getDate()->format('Y-m-d'),
63
                'lesson' => $absence->getFrom()->getLesson()
64
            ],
65
            'until' => [
66
                'date' => $absence->getUntil()->getDate()->format('Y-m-d'),
67
                'lesson' => $absence->getUntil()->getLesson()
68
            ],
69
            'student' => [
70
                'uuid' => $absence->getStudent()->getUuid()->toString(),
71
                'external_id' => $absence->getStudent()->getExternalId(),
72
                'firstname' => $absence->getStudent()->getFirstname(),
73
                'lastname' => $absence->getStudent()->getLastname(),
74
                'grade' => $section !== null ? $absence->getStudent()->getGrade($section)?->getName() : null
75
            ],
76
            'metadata' => $twig->render('absences/students/export/metadata.txt.twig', [
77
                'absence' => $absence
78
            ]),
79
            'attachments' => [ ]
80
        ];
81
82
        foreach($absence->getAttachments() as $attachment) {
83
            $json['attachments'][] = [
84
                'uuid' => $attachment->getUuid()->toString(),
85
                'filename' => $attachment->getFilename()
86
            ];
87
        }
88
89
        return $this->json($json);
90
    }
91
92
    #[Route('/attachment/{uuid}', name: 'export_student_absence_attachment')]
93
    public function attachment(StudentAbsenceAttachment $attachment, FilesystemOperator $studentAbsenceFilesystem, MimeTypes $mimeTypes, StudentAbsenceSettings $settings): Response {
0 ignored issues
show
Unused Code introduced by
The parameter $settings is not used and could be removed. ( Ignorable by Annotation )

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

93
    public function attachment(StudentAbsenceAttachment $attachment, FilesystemOperator $studentAbsenceFilesystem, MimeTypes $mimeTypes, /** @scrutinizer ignore-unused */ StudentAbsenceSettings $settings): Response {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
        if($studentAbsenceFilesystem->fileExists($attachment->getPath()) !== true) {
95
            throw new NotFoundHttpException();
96
        }
97
98
        $extension = pathinfo($attachment->getFilename(), PATHINFO_EXTENSION);
99
100
        return new FlysystemFileResponse(
101
            $studentAbsenceFilesystem,
102
            $attachment->getPath(),
103
            $attachment->getFilename(),
104
            $mimeTypes->getMimeType($extension)
105
        );
106
    }
107
}