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

CidReqHelper::getDoctrineCourseEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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