Passed
Pull Request — master (#6257)
by
unknown
08:47
created

__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 24
nc 3
nop 6
dl 0
loc 46
rs 9.536
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller\Api;
8
9
use Chamilo\CourseBundle\Entity\CStudentPublication;
10
use Chamilo\CourseBundle\Entity\CStudentPublicationCorrection;
11
use Chamilo\CourseBundle\Repository\CStudentPublicationCorrectionRepository;
12
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Contracts\Translation\TranslatorInterface;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
19
class CreateStudentPublicationCorrectionFileAction extends BaseResourceFileAction
20
{
21
    public function __invoke(
22
        Request $request,
23
        CStudentPublicationCorrectionRepository $correctionRepo,
24
        CStudentPublicationRepository $publicationRepo,
25
        EntityManagerInterface $em,
26
        KernelInterface $kernel,
27
        TranslatorInterface $translator
28
    ): CStudentPublicationCorrection {
29
        $fileExistsOption = $request->get('fileExistsOption', 'rename');
30
31
        $correction = new CStudentPublicationCorrection();
32
33
        $result = $this->handleCreateFileRequest(
34
            $correction,
35
            $correctionRepo,
36
            $request,
37
            $em,
38
            $fileExistsOption,
39
            $translator
40
        );
41
42
        $correction->setTitle($result['title']);
43
44
        $submissionId = (int) $request->get('submissionId');
45
46
        if (!$submissionId) {
47
            throw new NotFoundHttpException('submissionId is required');
48
        }
49
50
        $submission = $publicationRepo->find($submissionId);
51
52
        if (!$submission instanceof CStudentPublication) {
53
            throw new NotFoundHttpException('Student publication not found');
54
        }
55
56
        $submission->setDescription('Correction uploaded');
57
        $submission->setQualification(0);
58
        $submission->setDateOfQualification(new \DateTime());
59
        $submission->setAccepted(true);
60
61
        $submission->setExtensions($correction->getTitle());
62
63
        $em->persist($submission);
64
        $em->flush();
65
66
        return $correction;
67
    }
68
}
69