Passed
Pull Request — master (#5629)
by Angel Fernando Quiroz
10:06 queued 01:32
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
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\PluginBundle\XApi\Lrs\Util;
8
9
use Chamilo\CoreBundle\Entity\XApiInternalLog;
10
use Database;
11
use UserManager;
12
use Xabbuh\XApi\Model\Activity;
13
use Xabbuh\XApi\Model\Actor;
14
use Xabbuh\XApi\Model\Agent;
15
use Xabbuh\XApi\Model\IRL;
16
use Xabbuh\XApi\Model\Statement;
17
use XApiPlugin;
18
19
class InternalLogUtil
20
{
21
    public static function saveStatementForInternalLog(Statement $statement): void
22
    {
23
        if (null === $user = self::getUserFromActor($statement->getActor())) {
24
            return;
25
        }
26
27
        $statementObject = $statement->getObject();
28
29
        if (!$statementObject instanceof Activity) {
30
            return;
31
        }
32
33
        $languageIso = api_get_language_isocode();
34
        $statementVerbString = XApiPlugin::extractVerbInLanguage($statement->getVerb()->getDisplay(), $languageIso);
35
36
        $internalLog = new XApiInternalLog();
37
        $internalLog
38
            ->setUser($user)
39
            ->setVerb($statementVerbString)
40
            ->setObjectId($statementObject->getId()->getValue())
41
        ;
42
43
        if (null !== $statementId = $statement->getId()) {
44
            $internalLog->setStatementId($statementId->getValue());
45
        }
46
47
        if (null !== $definition = $statementObject->getDefinition()) {
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
80
        if (null !== $created = $statement->getCreated()) {
81
            $internalLog->setCreatedAt($created);
82
        }
83
84
        $em = Database::getManager();
85
        $em->persist($internalLog);
86
        $em->flush();
87
    }
88
89
    private static function getUserFromActor(Actor $actor): ?object
90
    {
91
        if (!$actor instanceof Agent) {
92
            return null;
93
        }
94
95
        $actorIri = $actor->getInverseFunctionalIdentifier();
96
97
        if (null === $actorIri) {
98
            return null;
99
        }
100
101
        $userRepo = UserManager::getRepository();
102
103
        $user = null;
104
105
        if (null !== $mbox = $actorIri->getMbox()) {
106
            if ((null !== $parts = explode(':', $mbox->getValue(), 2)) && !empty($parts[1])) {
107
                $user = $userRepo->findOneBy(['email' => $parts[1]]);
108
            }
109
        } elseif (null !== $account = $actorIri->getAccount()) {
110
            $chamiloIrl = IRL::fromString(api_get_path(WEB_PATH));
111
112
            if ($account->getHomePage()->equals($chamiloIrl)) {
113
                $user = $userRepo->findOneBy(['username' => $account->getName()]);
114
            }
115
        }
116
117
        return $user;
118
    }
119
}
120