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
|
|
|
|