Passed
Push — master ( 023819...5e8c06 )
by Julito
10:32 queued 25s
created

Container::setUserManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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