Passed
Push — master ( 262c23...1cc54c )
by Angel Fernando Quiroz
09:24
created

CidReqHelper::getGroupEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\Course;
10
use Chamilo\CoreBundle\Entity\Session;
11
use Chamilo\CoreBundle\EventListener\CidReqListener;
12
use Chamilo\CourseBundle\Entity\CGroup;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Component\HttpFoundation\Session\SessionInterface;
17
18
/**
19
 * @see CidReqListener::onKernelRequest()
20
 */
21
class CidReqHelper
22
{
23
    public function __construct(
24
        private readonly RequestStack $requestStack,
25
        private readonly EntityManagerInterface $em,
26
    ) {}
27
28
    private function getRequest(): ?Request
29
    {
30
        return $this->requestStack->getCurrentRequest();
31
    }
32
33
    private function getSessionHandler(): ?SessionInterface
34
    {
35
        $request = $this->getRequest();
36
37
        return $request?->getSession();
38
    }
39
40
    public function getSessionId(): ?int
41
    {
42
        $session = $this->getSessionHandler();
43
44
        return $session?->get('sid');
45
    }
46
47
    public function getSessionEntity(): ?Session
48
    {
49
        $session = $this->getSessionHandler();
50
51
        return $session?->get('session');
52
    }
53
54
    public function getCourseId(): mixed
55
    {
56
        $session = $this->getSessionHandler();
57
58
        return $session?->get('cid');
59
    }
60
61
    public function getCourseEntity(): ?Course
62
    {
63
        $session = $this->getSessionHandler();
64
65
        return $session?->get('course');
66
    }
67
68
    public function getGroupId(): ?int
69
    {
70
        $session = $this->getSessionHandler();
71
72
        return $session?->get('gid');
73
    }
74
75
    public function getGroupEntity(): ?CGroup
76
    {
77
        return $this->getSessionHandler()->get('group');
78
    }
79
80
    public function getDoctrineCourseEntity(): ?Course
81
    {
82
        $courseId = $this->getCourseId();
83
        if (empty($courseId)) {
84
            return null;
85
        }
86
87
        return $this->em->getRepository(Course::class)->find((int) $courseId);
88
    }
89
90
    public function getDoctrineSessionEntity(): ?Session
91
    {
92
        $sessionId = $this->getSessionId();
93
        if (empty($sessionId)) {
94
            return null;
95
        }
96
97
        return $this->em->getRepository(Session::class)->find((int) $sessionId);
98
    }
99
}
100