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

CreateStudentPublicationCommentAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 75
rs 10
c 1
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 73 6
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\Controller\Api\BaseResourceFileAction;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\ServiceHelper\MessageHelper;
12
use Chamilo\CourseBundle\Entity\CStudentPublicationComment;
13
use Chamilo\CourseBundle\Entity\CStudentPublication;
14
use Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository;
15
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
16
use Doctrine\ORM\EntityManager;
17
use Symfony\Bundle\SecurityBundle\Security;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\KernelInterface;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
use Symfony\Contracts\Translation\TranslatorInterface;
22
23
class CreateStudentPublicationCommentAction extends BaseResourceFileAction
24
{
25
    public function __invoke(
26
        Request $request,
27
        CStudentPublicationCommentRepository $commentRepo,
28
        CStudentPublicationRepository $publicationRepo,
29
        EntityManager $em,
30
        KernelInterface $kernel,
31
        TranslatorInterface $translator,
32
        MessageHelper $messageHelper,
33
        Security $security
34
    ): CStudentPublicationComment {
35
        $fileExistsOption = $request->get('fileExistsOption', 'rename');
36
37
        $commentEntity = new CStudentPublicationComment();
38
39
        $result = $this->handleCreateCommentRequest(
40
            $commentEntity,
41
            $commentRepo,
42
            $request,
43
            $em,
44
            $fileExistsOption,
45
            $translator
46
        );
47
48
        $commentText = $request->get('comment');
49
        $submissionId = (int) $request->get('submissionId');
50
        $sendMail = $request->get('sendMail', false);
51
52
        if (!$submissionId) {
53
            throw new NotFoundHttpException('submissionId is required');
54
        }
55
56
        $submission = $publicationRepo->find($submissionId);
57
58
        if (!$submission instanceof CStudentPublication) {
59
            throw new NotFoundHttpException('Student publication not found');
60
        }
61
62
        /** @var User $user */
63
        $user = $security->getUser();
64
65
        $commentEntity->setUser($user);
66
        $commentEntity->setPublication($submission);
67
        $commentEntity->setComment($commentText ?? '');
68
69
        $filename = $result['filename'] ?? null;
70
        if (!empty($filename)) {
71
            $commentEntity->setFile($filename);
72
        }
73
74
        $em->persist($commentEntity);
75
        $em->flush();
76
77
        if ($sendMail && $submission->getUser() instanceof User) {
78
            /** @var User $receiverUser */
79
            $receiverUser = $submission->getUser();
80
            $senderUserId = $user?->getId() ?? 0;
81
82
            $subject = sprintf('New feedback for your submission "%s"', $submission->getTitle());
83
            $content = sprintf(
84
                'Hello %s, there is a new comment on your assignment submission "%s". Please review it in the platform.',
85
                $receiverUser->getFullname(),
86
                $submission->getTitle()
87
            );
88
89
            $messageHelper->sendMessageSimple(
90
                $receiverUser->getId(),
91
                $subject,
92
                $content,
93
                $senderUserId
94
            );
95
        }
96
97
        return $commentEntity;
98
    }
99
}
100