Passed
Push — master ( 3fd861...81aaab )
by
unknown
15:54 queued 07:53
created

CreateStudentPublicationCommentAction::__invoke()   C

Complexity

Conditions 12
Paths 42

Size

Total Lines 96
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 53
nc 42
nop 8
dl 0
loc 96
rs 6.9666
c 1
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
        $hasFile = $request->files->get('uploadFile');
40
        $hasComment = trim((string) $request->get('comment')) !== '';
41
42
        if ($hasFile || $hasComment) {
43
            $result = $this->handleCreateCommentRequest(
44
                $commentEntity,
45
                $commentRepo,
46
                $request,
47
                $em,
48
                $fileExistsOption,
49
                $translator
50
            );
51
52
            $filename = $result['filename'] ?? null;
53
            if (!empty($filename)) {
54
                $commentEntity->setFile($filename);
55
            }
56
        }
57
58
        $commentText = $request->get('comment');
59
        $submissionId = (int) $request->get('submissionId');
60
        $sendMail = $request->get('sendMail', false);
61
62
        if (!$submissionId) {
63
            throw new NotFoundHttpException('submissionId is required');
64
        }
65
66
        $submission = $publicationRepo->find($submissionId);
67
68
        if (!$submission instanceof CStudentPublication) {
69
            throw new NotFoundHttpException('Student publication not found');
70
        }
71
72
        /** @var User $user */
73
        $user = $security->getUser();
74
75
        $qualification = $request->get('qualification', null);
76
        $hasQualification = $qualification !== null;
77
78
        if ($hasFile || $hasComment) {
79
            $commentEntity->setUser($user);
80
            $commentEntity->setPublication($submission);
81
            $commentEntity->setComment($commentText ?? '');
82
83
            if (!empty($filename)) {
84
                $commentEntity->setFile($filename);
85
            }
86
87
            $em->persist($commentEntity);
88
        }
89
90
        if ($hasQualification) {
91
            $submission->setQualification((float) $qualification);
92
            $submission->setQualificatorId($user->getId());
93
            $submission->setDateOfQualification(new \DateTime());
94
95
            $em->persist($submission);
96
        }
97
98
        $em->flush();
99
100
        if ($sendMail && $submission->getUser() instanceof User) {
101
            /** @var User $receiverUser */
102
            $receiverUser = $submission->getUser();
103
            $senderUserId = $user?->getId() ?? 0;
104
105
            $subject = sprintf('New feedback for your submission "%s"', $submission->getTitle());
106
            $content = sprintf(
107
                'Hello %s, there is a new comment on your assignment submission "%s". Please review it in the platform.',
108
                $receiverUser->getFullname(),
109
                $submission->getTitle()
110
            );
111
112
            $messageHelper->sendMessageSimple(
113
                $receiverUser->getId(),
114
                $subject,
115
                $content,
116
                $senderUserId
117
            );
118
        }
119
120
        return $commentEntity;
121
    }
122
}
123