Passed
Push — master ( ff47af...4fe9ef )
by Julito
08:21
created

Container::addFlash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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