Passed
Push — master ( 95b92f...37aede )
by Yannick
10:09 queued 01:17
created

PortfolioItemCommented::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
nc 2
nop 0
dl 0
loc 53
c 1
b 0
f 0
cc 2
rs 9.328

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