Passed
Push — 1.11.x ( 1b80ca...1b83a3 )
by Angel Fernando Quiroz
13:55 queued 11s
created

InternalLogUtil::getUserFromActor()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 15
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 29
rs 8.4444
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\XApi\Lrs\Util;
6
7
use Chamilo\PluginBundle\Entity\XApi\InternalLog;
8
use Database;
9
use Doctrine\ORM\ORMException;
10
use UserManager;
11
use Xabbuh\XApi\Model\Activity;
12
use Xabbuh\XApi\Model\Actor;
13
use Xabbuh\XApi\Model\Agent;
14
use Xabbuh\XApi\Model\IRL;
15
use Xabbuh\XApi\Model\Statement;
16
use XApiPlugin;
17
18
class InternalLogUtil
19
{
20
    public static function saveStatementForInternalLog(Statement $statement)
21
    {
22
        if (null === $user = self::getUserFromActor($statement->getActor())) {
23
            return;
24
        }
25
26
        $internalLog = new InternalLog();
27
        $internalLog->setUser($user);
28
29
        $languageIso = api_get_language_isocode();
30
31
        $internalLog->setVerb(
32
            XApiPlugin::extractVerbInLanguage($statement->getVerb()->getDisplay(), $languageIso)
33
        );
34
35
        $statementObject = $statement->getObject();
36
37
        if (!$statementObject instanceof Activity) {
38
            return;
39
        }
40
41
        $internalLog->setStatementId($statement->getId()->getValue());
42
43
        if (null !== $definition = $statementObject->getDefinition()) {
44
            $internalLog->setActivityId(
45
                $statementObject->getId()->getValue()
46
            );
47
48
            if (null !== $nameInLanguages = $definition->getName()) {
49
                $internalLog->setActivityName(
50
                    XApiPlugin::extractVerbInLanguage($nameInLanguages, $languageIso)
51
                );
52
            }
53
54
            if (null !== $descriptionInLanguage = $definition->getDescription()) {
55
                $internalLog->setActivityDescription(
56
                    XApiPlugin::extractVerbInLanguage($descriptionInLanguage, $languageIso)
57
                );
58
            }
59
        }
60
61
        if (null !== $statementResult = $statement->getResult()) {
62
            if (null !== $score = $statementResult->getScore()) {
63
                $internalLog
64
                    ->setScoreScaled(
65
                        $score->getScaled()
66
                    )
67
                    ->setScoreRaw(
68
                        $score->getRaw()
69
                    )
70
                    ->setScoreMin(
71
                        $score->getMin()
72
                    )
73
                    ->setScoreMax(
74
                        $score->getMax()
75
                    );
76
            }
77
        }
78
79
        if (null !== $created = $statement->getCreated()) {
80
            $internalLog->setCreatedAt($created);
81
        }
82
83
        $em = Database::getManager();
84
        $em->persist($internalLog);
85
        $em->flush();
86
    }
87
88
    private static function getUserFromActor(Actor $actor)
89
    {
90
        if (!$actor instanceof Agent) {
91
            return null;
92
        }
93
94
        $actorIri = $actor->getInverseFunctionalIdentifier();
95
96
        if (null === $actorIri) {
97
            return null;
98
        }
99
100
        $userRepo = UserManager::getRepository();
101
102
        $user = null;
103
104
        if (null !== $mbox = $actorIri->getMbox()) {
105
            if ((null !== $parts = explode(':', $mbox->getValue(), 2)) && !empty($parts[1])) {
106
                $user = $userRepo->findOneBy(['email' => $parts[1]]);
107
            }
108
        } elseif (null !== $account = $actorIri->getAccount()) {
109
            $chamiloIrl = IRL::fromString(api_get_path(WEB_PATH));
110
111
            if ($account->getHomePage()->equals($chamiloIrl)) {
112
                $user = $userRepo->findOneBy(['username' => $account->getName()]);
113
            }
114
        }
115
116
        return $user;
117
    }
118
}
119