Passed
Push — master ( 0c5975...84e769 )
by Julito
21:43
created

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