Passed
Pull Request — master (#6257)
by
unknown
09:37
created

CreateStudentPublicationFileAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 46
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 44 4
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\CoreBundle\Entity\User;
10
use Chamilo\CourseBundle\Entity\CStudentPublication;
11
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
12
use Doctrine\ORM\EntityManager;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\KernelInterface;
15
use Symfony\Contracts\Translation\TranslatorInterface;
16
use Symfony\Bundle\SecurityBundle\Security;
17
18
class CreateStudentPublicationFileAction extends BaseResourceFileAction
19
{
20
    public function __invoke(
21
        Request $request,
22
        CStudentPublicationRepository $repo,
23
        EntityManager $em,
24
        KernelInterface $kernel,
25
        TranslatorInterface $translator,
26
        Security $security
27
    ): CStudentPublication {
28
        $fileExistsOption = $request->get('fileExistsOption', 'rename');
29
30
        $studentPublication = new CStudentPublication();
31
32
        $result = $this->handleCreateFileRequest(
33
            $studentPublication,
34
            $repo,
35
            $request,
36
            $em,
37
            $fileExistsOption,
38
            $translator
39
        );
40
41
        $studentPublication->setTitle($result['title']);
42
        $studentPublication->setFiletype($result['filetype']);
43
        $studentPublication->setDescription($result['comment'] ?? '');
44
        $studentPublication->setContainsFile(1);
45
        $studentPublication->setAccepted(true);
46
        $studentPublication->setActive(1);
47
        $studentPublication->setSentDate(new \DateTime());
48
49
        /** @var User $user */
50
        $user = $security->getUser();
51
        if ($user instanceof User) {
0 ignored issues
show
introduced by
$user is always a sub-type of Chamilo\CoreBundle\Entity\User.
Loading history...
52
            $studentPublication->setUser($user);
53
        }
54
55
        $parentId = (int) $request->get('parentId');
56
        if ($parentId > 0) {
57
            $parentEntity = $repo->find($parentId);
58
            if ($parentEntity) {
59
                $studentPublication->setPublicationParent($parentEntity);
60
            }
61
        }
62
63
        return $studentPublication;
64
    }
65
}
66