Passed
Pull Request — 1.11.x (#4501)
by Angel Fernando Quiroz
09:49
created

PortfolioItemCommented::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 38
nc 2
nop 0
dl 0
loc 52
rs 9.312
c 0
b 0
f 0

How to fix   Long Method   

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:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement;
6
7
use Chamilo\CoreBundle\Entity\PortfolioAttachment;
8
use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioComment as PortfolioCommentActivity;
9
use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioItem as PortfolioItemActivity;
10
use Chamilo\PluginBundle\XApi\ToolExperience\Actor\User as UserActor;
11
use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Commented as CommentedVerb;
12
use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Replied as RepliedVerb;
13
use Xabbuh\XApi\Model\Result;
14
use Xabbuh\XApi\Model\Statement;
15
16
class PortfolioItemCommented extends PortfolioComment
17
{
18
    use PortfolioAttachmentsTrait;
19
20
    public function generate(): Statement
21
    {
22
        $statementId = $this->generateStatementId('portfolio-comment');
23
24
        $userActor = new UserActor($this->comment->getAuthor());
25
        $statementResult = new Result(null, null, null, $this->comment->getContent());
26
27
        $context = $this->generateContext();
28
29
        $em = \Database::getManager();
30
        $commentAttachments = $em->getRepository(PortfolioAttachment::class)->findFromComment($this->comment);
31
32
        $attachments = $this->generateAttachments(
33
            $commentAttachments,
34
            $this->comment->getAuthor()
35
        );
36
37
        if ($this->parentComment) {
38
            $repliedVerb = new RepliedVerb();
39
40
            $itemActivity = new PortfolioItemActivity($this->item);
41
            $parentCommentActivity = new PortfolioCommentActivity($this->parentComment);
42
43
            $contextActivities = $context
44
                ->getContextActivities()
45
                ->withAddedGroupingActivity($itemActivity->generate());
46
47
            return new Statement(
48
                $statementId,
49
                $userActor->generate(),
50
                $repliedVerb->generate(),
51
                $parentCommentActivity->generate(),
52
                $statementResult,
53
                null,
54
                $this->comment->getDate(),
55
                null,
56
                $context->withContextActivities($contextActivities),
57
                $attachments
58
            );
59
        } else {
60
            $itemShared = new PortfolioItemShared($this->item);
61
62
            $commentedVerb = new CommentedVerb();
63
64
            return $itemShared->generate()
65
                ->withId($statementId)
66
                ->withActor($userActor->generate())
67
                ->withVerb($commentedVerb->generate())
68
                ->withStored($this->comment->getDate())
69
                ->withResult($statementResult)
70
                ->withContext($context)
71
                ->withAttachments($attachments);
72
        }
73
    }
74
}
75