Completed
Push — master ( dff8d7...0614a5 )
by Julito
12:08
created

BaseController::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Session;
8
use Knp\Menu\FactoryInterface as MenuFactoryInterface;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13
/**
14
 * Each entity controller must extends this class.
15
 *
16
 * @abstract
17
 */
18
abstract class BaseController extends AbstractController
19
{
20
    /**
21
     * @param string $message
22
     *
23
     * @return NotFoundHttpException
24
     */
25
    public function abort($message = '')
26
    {
27
        return new NotFoundHttpException($message);
28
    }
29
30
    /**
31
     * Translator shortcut.
32
     *
33
     * @param string $variable
34
     *
35
     * @return string
36
     */
37
    public function trans($variable)
38
    {
39
        return $this->container->get('translator')->trans($variable);
40
    }
41
42
    /**
43
     * @return MenuFactoryInterface
44
     */
45
    public function getMenuFactory()
46
    {
47
        return $this->container->get('knp_menu.factory');
48
    }
49
50
    /**
51
     * Gets the current Chamilo course based in the "_real_cid" session variable.
52
     *
53
     * @return Course
54
     */
55
    public function getCourse()
56
    {
57
        $request = $this->getRequest();
58
        if ($request) {
59
            $courseId = $request->getSession()->get('_real_cid', 0);
60
        }
61
62
        if (empty($courseId)) {
63
            return null;
64
        }
65
66
        return $this->getDoctrine()->getManager()->find('ChamiloCoreBundle:Course', $courseId);
67
    }
68
69
    /**
70
     * Gets the current Chamilo session based in the "sid" $_SESSION variable.
71
     *
72
     * @return Session|null
73
     */
74
    public function getCourseSession()
75
    {
76
        $request = $this->getRequest();
77
78
        if ($request) {
79
            $sessionId = $request->getSession()->get('sid', 0);
80
        }
81
82
        if (empty($sessionId)) {
83
            return null;
84
        }
85
86
        return $this->getDoctrine()->getManager()->find('ChamiloCoreBundle:Session', $sessionId);
87
    }
88
89
    public function getCourseUrlQuery(): string
90
    {
91
        $url = '';
92
        $course = $this->getCourse();
93
        if ($course) {
0 ignored issues
show
introduced by
$course is of type Chamilo\CoreBundle\Entity\Course, thus it always evaluated to true.
Loading history...
94
            $url = 'cid='.$course->getId();
95
        }
96
        $session = $this->getCourseSession();
97
98
        if ($session) {
99
            $url .= '&sid='.$session->getId();
100
        } else {
101
            $url .= '&sid=0';
102
        }
103
104
        return $url;
105
    }
106
107
    public function getCourseParams(): array
108
    {
109
        $routeParams = ['cid' => $this->getCourse()->getId()];
110
        $session = $this->getCourseSession();
111
        $sessionId = $session ? $session->getId() : 0;
112
        $routeParams['sid'] = $sessionId;
113
114
        return $routeParams;
115
    }
116
117
    /**
118
     * @return Request|null
119
     */
120
    public function getRequest()
121
    {
122
        $request = $this->container->get('request_stack')->getCurrentRequest();
123
124
        return $request;
125
    }
126
}
127