Completed
Push — master ( 3a5b5f...667dde )
by Julito
15:02
created

BaseController::getCourseSession()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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