Passed
Push — master ( 8f17a5...faab0f )
by Julito
09:35
created

Container::getMailer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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