Completed
Push — master ( 911538...3e739c )
by Julito
17:05
created

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