Passed
Push — master ( 4ca170...784f70 )
by Julito
12:58
created

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