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

PortfolioItemCommented   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 57
c 1
b 0
f 0
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 53 2
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