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

CreateStudentPublicationCommentAction::__invoke()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 73
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 40
nc 6
nop 8
dl 0
loc 73
rs 8.6577
c 1
b 0
f 0

How to fix   Long Method    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
        $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