Passed
Pull Request — master (#6396)
by Angel Fernando Quiroz
08:39
created

EventLoggerHelper::serializeEventValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Helpers;
8
9
use Chamilo\CoreBundle\Entity\TrackEDefault;
10
use DateTime;
11
use DateTimeZone;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Symfony\Bundle\SecurityBundle\Security;
14
15
class EventLoggerHelper
16
{
17
    public function __construct(
18
        private readonly EntityManagerInterface $entityManager,
19
        private readonly CidReqHelper $cidReqHelper,
20
        private readonly Security $security,
21
    ) {}
22
23
    public function addEvent(
24
        string $eventType,
25
        string $valueType,
26
        $value,
27
        ?DateTime $dateTime = null,
28
        ?int $userId = null,
29
        ?int $courseId = null,
30
        ?int $sessionId = null
31
    ): bool {
32
        if (empty($eventType)) {
33
            return false;
34
        }
35
36
        $courseId = $courseId ?? $this->cidReqHelper->getCourseId();
37
        $sessionId = $sessionId ?? $this->cidReqHelper->getSessionId();
38
        $userId = $userId ?? $this->security->getUser()->getId();
39
40
        $value = $this->serializeEventValue($value);
41
42
        $trackEvent = new TrackEDefault();
43
        $trackEvent
44
            ->setDefaultUserId($userId)
45
            ->setCId($courseId)
46
            ->setDefaultDate($dateTime ?? new DateTime('now', new DateTimeZone('UTC')))
47
            ->setDefaultEventType($eventType)
48
            ->setDefaultValueType($valueType)
49
            ->setDefaultValue($value)
50
            ->setSessionId((int) $sessionId)
51
        ;
52
53
        $this->entityManager->persist($trackEvent);
54
        $this->entityManager->flush();
55
56
        return true;
57
    }
58
59
    private function serializeEventValue($value): string
60
    {
61
        return serialize($value);
62
    }
63
}
64