Passed
Push — master ( 323135...9f183a )
by Julito
13:17
created

Container::getRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nop 0
dl 0
loc 11
rs 10
nc 3
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Framework;
5
6
use Chamilo\CoreBundle\Component\Editor\Editor;
7
use Chamilo\CoreBundle\Hook\Interfaces\HookEventInterface;
8
use Chamilo\CoreBundle\Repository\AccessUrlRepository;
9
use Chamilo\CoreBundle\Repository\CourseRepository;
10
use Chamilo\CoreBundle\ToolChain;
11
use Chamilo\CourseBundle\Repository\CDocumentRepository;
12
use Chamilo\CourseBundle\Repository\CExerciseCategoryRepository;
13
use Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository;
14
use Chamilo\CourseBundle\Repository\CQuizRepository;
15
use Chamilo\PageBundle\Entity\Page;
16
use Chamilo\SettingsBundle\Manager\SettingsManager;
17
use Sonata\PageBundle\Entity\SiteManager;
18
use Sonata\UserBundle\Entity\UserManager;
19
use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Session\Session;
23
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
24
use Symfony\Component\Security\Core\Role\RoleHierarchy;
25
use Symfony\Contracts\Translation\TranslatorInterface;
26
27
/**
28
 * Class Container
29
 * This class is a way to access Symfony2 services in legacy Chamilo code.
30
 */
31
class Container
32
{
33
    /**
34
     * @var ContainerInterface
35
     */
36
    public static $container;
37
    public static $session;
38
    public static $request;
39
    public static $configuration;
40
    public static $environment;
41
    public static $urlGenerator;
42
    public static $checker;
43
    /** @var TranslatorInterface */
44
    public static $translator;
45
    public static $mailer;
46
    public static $template;
47
48
    public static $rootDir;
49
    public static $logDir;
50
    public static $tempDir;
51
    public static $dataDir;
52
    public static $courseDir;
53
    public static $configDir;
54
    public static $assets;
55
    public static $htmlEditor;
56
    public static $twig;
57
    public static $roles;
58
    /** @var string */
59
    public static $legacyTemplate = '@ChamiloTheme/Layout/layout_one_col.html.twig';
60
    private static $settingsManager;
61
    private static $userManager;
62
    private static $siteManager;
63
64
    /**
65
     * @param ContainerInterface $container
66
     */
67
    public static function setContainer($container)
68
    {
69
        self::$container = $container;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public static function getConfigDir()
76
    {
77
        return self::$configDir;
78
    }
79
80
    /**
81
     * @param string $parameter
82
     *
83
     * @return mixed
84
     */
85
    public static function getParameter($parameter)
86
    {
87
        if (self::$container->hasParameter($parameter)) {
88
            return self::$container->getParameter($parameter);
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public static function getEnvironment()
98
    {
99
        return self::$container->get('kernel')->getEnvironment();
100
    }
101
102
    /**
103
     * @return RoleHierarchy
104
     */
105
    public static function getRoles()
106
    {
107
        return self::$container->get('security.role_hierarchy');
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public static function getLogDir()
114
    {
115
        return self::$container->get('kernel')->getLogDir();
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public static function getTempDir()
122
    {
123
        return self::$container->get('kernel')->getCacheDir().'/';
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public static function getRootDir()
130
    {
131
        if (isset(self::$container)) {
132
            return self::$container->get('kernel')->getRealRootDir();
133
        }
134
135
        return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/';
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    public static function getUrlAppend()
142
    {
143
        return self::$container->get('kernel')->getUrlAppend();
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public static function isInstalled()
150
    {
151
        return self::$container->get('kernel')->isInstalled();
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public static function getDataDir()
158
    {
159
        return self::$dataDir;
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public static function getCourseDir()
166
    {
167
        return self::$courseDir;
168
    }
169
170
    /**
171
     * @return \Twig_Environment
172
     */
173
    public static function getTwig()
174
    {
175
        return self::$container->get('twig');
176
    }
177
178
    /**
179
     * @return \Symfony\Bundle\TwigBundle\TwigEngine
180
     */
181
    public static function getTemplating()
182
    {
183
        return self::$container->get('templating');
184
    }
185
186
    /**
187
     * @return Editor
188
     */
189
    public static function getHtmlEditor()
190
    {
191
        return self::$container->get('chamilo_core.html_editor');
192
    }
193
194
    /**
195
     * @return object|Request
196
     */
197
    public static function getRequest()
198
    {
199
        if (self::$container === null) {
200
            return null;
201
        }
202
203
        if (!empty(self::$request)) {
204
            return self::$request;
205
        }
206
207
        return self::$container->get('request_stack');
208
    }
209
210
    /**
211
     * @param Request $request
212
     */
213
    public static function setRequest($request)
214
    {
215
        self::$request = $request;
216
    }
217
218
    /**
219
     * @return Session|false
220
     */
221
    public static function getSession()
222
    {
223
        if (self::$container && self::$container->has('session')) {
224
            return self::$container->get('session');
225
        }
226
227
        return false;
228
    }
229
230
    /**
231
     * @return AuthorizationChecker
232
     */
233
    public static function getAuthorizationChecker()
234
    {
235
        return self::$container->get('security.authorization_checker');
236
    }
237
238
    /**
239
     * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
240
     */
241
    public static function getTokenStorage()
242
    {
243
        return self::$container->get('security.token_storage');
244
    }
245
246
    /**
247
     * @return TranslatorInterface
248
     */
249
    public static function getTranslator()
250
    {
251
        if (isset(self::$translator)) {
252
            return self::$translator;
253
        }
254
255
        if (self::$container) {
256
            return self::$container->get('translator');
257
        }
258
259
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type Symfony\Contracts\Translation\TranslatorInterface.
Loading history...
260
    }
261
262
    /**
263
     * @return AssetsHelper
264
     */
265
    public static function getAsset()
266
    {
267
        return self::$container->get('templating.helper.assets');
268
    }
269
270
    /**
271
     * @return \Swift_Mailer
272
     */
273
    public static function getMailer()
274
    {
275
        return self::$container->get('mailer');
276
    }
277
278
    /**
279
     * @return \Elao\WebProfilerExtraBundle\TwigProfilerEngine
280
     */
281
    public static function getTemplate()
282
    {
283
        return self::$container->get('templating');
284
    }
285
286
    /**
287
     * @return SettingsManager
288
     */
289
    public static function getSettingsManager()
290
    {
291
        return self::$settingsManager;
292
    }
293
294
    /**
295
     * @param SettingsManager $manager
296
     */
297
    public static function setSettingsManager($manager)
298
    {
299
        self::$settingsManager = $manager;
300
    }
301
302
    /**
303
     * @return \Chamilo\CourseBundle\Manager\SettingsManager
304
     */
305
    public static function getCourseSettingsManager()
306
    {
307
        return self::$container->get('chamilo_course.settings.manager');
308
    }
309
310
    /**
311
     * @return \Doctrine\ORM\EntityManager
312
     */
313
    public static function getEntityManager()
314
    {
315
        return \Database::getManager();
316
    }
317
318
    /**
319
     * @return UserManager
320
     */
321
    public static function getUserManager()
322
    {
323
        return self::$userManager;
324
    }
325
326
    /**
327
     * @return CDocumentRepository
328
     */
329
    public static function getDocumentRepository()
330
    {
331
        return self::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository');
332
    }
333
334
    /**
335
     * @return CQuizRepository
336
     */
337
    public static function getExerciseRepository()
338
    {
339
        return self::$container->get('Chamilo\CourseBundle\Repository\CQuizRepository');
340
    }
341
342
    /**
343
     * @return CExerciseCategoryRepository
344
     */
345
    public static function getExerciseCategoryRepository()
346
    {
347
        return self::$container->get('Chamilo\CourseBundle\Repository\CExerciseCategoryRepository');
348
    }
349
350
    /**
351
     * @return CQuizQuestionCategoryRepository
352
     */
353
    public static function getQuestionCategoryRepository()
354
    {
355
        return self::$container->get('Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository');
356
    }
357
358
    /**
359
     * @return AccessUrlRepository
360
     */
361
    public static function getAccessUrlRepository()
362
    {
363
        return self::$container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository');
364
    }
365
366
    /**
367
     * @return CourseRepository
368
     */
369
    public static function getCourseRepository()
370
    {
371
        return self::$container->get('Chamilo\CoreBundle\Repository\CourseRepository');
372
    }
373
374
    /**
375
     * @param $manager UserManager
376
     */
377
    public static function setUserManager($manager)
378
    {
379
        self::$userManager = $manager;
380
    }
381
382
    /**
383
     * @return SiteManager
384
     */
385
    public static function getSiteManager()
386
    {
387
        return self::$siteManager;
388
    }
389
390
    /**
391
     * @param $manager UserManager
392
     */
393
    public static function setSiteManager($manager)
394
    {
395
        self::$siteManager = $manager;
396
    }
397
398
    /**
399
     * @return \Sonata\UserBundle\Entity\GroupManager
400
     */
401
    public static function getGroupManager()
402
    {
403
        return self::$container->get('fos_user.group_manager');
404
    }
405
406
    /**
407
     * @return \Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher
408
     */
409
    public static function getEventDispatcher()
410
    {
411
        return self::$container->get('event_dispatcher');
412
    }
413
414
    /**
415
     * @return \Symfony\Component\Form\FormFactory
416
     */
417
    public static function getFormFactory()
418
    {
419
        return self::$container->get('form.factory');
420
    }
421
422
    /**
423
     * @param string $message
424
     * @param string $type    error|success|warning|danger
425
     */
426
    public static function addFlash($message, $type = 'success')
427
    {
428
        $session = self::getSession();
429
        $session->getFlashBag()->add($type, $message);
430
    }
431
432
    /**
433
     * @return object|\Symfony\Cmf\Component\Routing\ChainRouter
434
     */
435
    public static function getRouter()
436
    {
437
        return self::$container->get('router');
438
    }
439
440
    /**
441
     * @return ToolChain
442
     */
443
    public static function getToolChain()
444
    {
445
        return self::$container->get('chamilo_core.tool_chain');
446
    }
447
448
    /**
449
     * @param ContainerInterface $container
450
     * @param bool               $setSession
451
     */
452
    public static function setLegacyServices($container, $setSession = true)
453
    {
454
        \Database::setConnection($container->get('doctrine.dbal.default_connection'));
455
        $em = $container->get('doctrine.orm.entity_manager');
456
        \Database::setManager($em);
457
        \CourseManager::setEntityManager($em);
458
459
        self::setSettingsManager($container->get('chamilo.settings.manager'));
460
        self::setUserManager($container->get('fos_user.user_manager'));
461
        self::setSiteManager($container->get('sonata.page.manager.site'));
462
463
        \CourseManager::setCourseSettingsManager($container->get('chamilo_course.settings.manager'));
464
        // Setting course tool chain (in order to create tools to a course)
465
        \CourseManager::setToolList($container->get('chamilo_core.tool_chain'));
466
467
        if ($setSession) {
468
            self::$session = $container->get('session');
469
        }
470
        // Setting legacy properties.
471
        self::$dataDir = $container->get('kernel')->getDataDir();
472
        self::$courseDir = $container->get('kernel')->getDataDir();
473
    }
474
475
    /**
476
     * Gets a sonata page.
477
     *
478
     * @param string $slug
479
     *
480
     * @return Page
481
     */
482
    public static function getPage($slug)
483
    {
484
        $container = self::$container;
485
        /*$siteSelector = $container->get('sonata.page.site.selector');
486
        $site = $siteSelector->retrieve();*/
487
        $siteManager = $container->get('sonata.page.manager.site');
488
        $request = Container::getRequest();
489
        $page = null;
490
        if ($request) {
491
            $host = $request->getHost();
492
            $criteria = [
493
                'locale' => $request->getLocale(),
494
                'host' => $host,
495
            ];
496
            $site = $siteManager->findOneBy($criteria);
497
498
            $pageManager = $container->get('sonata.page.manager.page');
499
            // Parents only of homepage
500
            $criteria = ['site' => $site, 'enabled' => true, 'slug' => $slug];
501
            /** @var Page $page */
502
            $page = $pageManager->findOneBy($criteria);
503
504
            return $page;
505
        }
506
507
        return $page;
508
    }
509
510
    /**
511
     * @param string $class
512
     *
513
     * @throws \Exception
514
     *
515
     * @return HookEventInterface
516
     */
517
    public static function instantiateHook(string $class): HookEventInterface
518
    {
519
        return self::$container->get('chamilo_core.hook_factory')->build($class);
520
    }
521
}
522