Passed
Pull Request — master (#6396)
by Angel Fernando Quiroz
13:45 queued 05:26
created

EventLoggerHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A serializeEventValue() 0 3 1
A addEvent() 0 34 2
A __construct() 0 5 1
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