Passed
Push — master ( bcba07...4f2fea )
by Julito
08:02
created

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