Total Complexity | 107 |
Total Lines | 558 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Container often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
111 | class Container |
||
112 | { |
||
113 | public static ?ContainerInterface $container = null; |
||
114 | public static ?Request $request = null; |
||
115 | // For legacy, to get the translator service is necessary get it by Container::$container->get('translator') |
||
116 | public static ?Translator $translator = null; |
||
117 | public static Environment $twig; |
||
118 | public static ?Session $session = null; |
||
119 | public static string $legacyTemplate = '@ChamiloCore/Layout/layout_one_col.html.twig'; |
||
120 | |||
121 | public static function setContainer(ContainerInterface $container): void |
||
122 | { |
||
123 | self::$container = $container; |
||
124 | } |
||
125 | |||
126 | public static function getParameter(string $parameter): array|bool|float|int|string|UnitEnum|null |
||
127 | { |
||
128 | if (self::$container->hasParameter($parameter)) { |
||
129 | return self::$container->getParameter($parameter); |
||
130 | } |
||
131 | |||
132 | return false; |
||
133 | } |
||
134 | |||
135 | public static function getLegacyHelper(): ContainerHelper |
||
136 | { |
||
137 | return self::$container->get(ContainerHelper::class); |
||
138 | } |
||
139 | |||
140 | public static function getEnvironment(): string |
||
141 | { |
||
142 | return self::getLegacyHelper()->getKernel()->getEnvironment(); |
||
143 | } |
||
144 | |||
145 | public static function getLogDir(): string |
||
146 | { |
||
147 | return self::getLegacyHelper()->getKernel()->getLogDir(); |
||
148 | } |
||
149 | |||
150 | public static function getCacheDir(): string |
||
151 | { |
||
152 | return self::getLegacyHelper()->getKernel()->getCacheDir().'/'; |
||
153 | } |
||
154 | |||
155 | public static function getProjectDir(): string |
||
156 | { |
||
157 | if (null !== self::$container) { |
||
158 | return self::getLegacyHelper()->getKernel()->getProjectDir().'/'; |
||
159 | } |
||
160 | |||
161 | return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/'; |
||
162 | } |
||
163 | |||
164 | public static function isInstalled(): bool |
||
165 | { |
||
166 | return self::getLegacyHelper()->getKernel()->isInstalled(); |
||
167 | } |
||
168 | |||
169 | public static function getMessengerBus(): MessageBusInterface |
||
170 | { |
||
171 | return self::getLegacyHelper()->getMessengerBus(); |
||
172 | } |
||
173 | |||
174 | public static function getTwig(): Environment |
||
175 | { |
||
176 | return self::$twig; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return Editor |
||
181 | */ |
||
182 | public static function getHtmlEditor() |
||
183 | { |
||
184 | return self::$container->get(CkEditor::class); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return null|Request |
||
189 | */ |
||
190 | public static function getRequest() |
||
191 | { |
||
192 | if (null === self::$container) { |
||
193 | return null; |
||
194 | } |
||
195 | |||
196 | if (!empty(self::$request)) { |
||
197 | return self::$request; |
||
198 | } |
||
199 | |||
200 | return self::$container->get('request_stack')->getCurrentRequest(); |
||
201 | } |
||
202 | |||
203 | public static function setRequest(Request $request): void |
||
204 | { |
||
205 | self::$request = $request; |
||
206 | } |
||
207 | |||
208 | public static function getSession(): bool|HttpSessionInterface|Session|null |
||
209 | { |
||
210 | if (null !== self::$session) { |
||
211 | return self::$session; |
||
212 | } |
||
213 | |||
214 | if (null !== self::$container) { |
||
215 | return self::$container->get('request_stack')->getSession(); |
||
216 | } |
||
217 | |||
218 | return false; |
||
219 | } |
||
220 | |||
221 | public static function setSession(Session $session): void |
||
222 | { |
||
223 | self::$session = $session; |
||
224 | } |
||
225 | |||
226 | public static function getAuthorizationChecker(): AuthorizationCheckerInterface |
||
227 | { |
||
228 | return self::getLegacyHelper()->getAuthorizationChecker(); |
||
229 | } |
||
230 | |||
231 | public static function getTokenStorage(): TokenStorageInterface|TokenStorage |
||
234 | } |
||
235 | |||
236 | public static function getMailer(): Mailer |
||
237 | { |
||
238 | return self::$container->get(Mailer::class); |
||
239 | } |
||
240 | |||
241 | public static function getSettingsManager(): SettingsManager |
||
242 | { |
||
243 | return self::$container->get(SettingsManager::class); |
||
244 | } |
||
245 | |||
246 | public static function getCourseSettingsManager(): SettingsCourseManager |
||
247 | { |
||
248 | return self::$container->get(SettingsCourseManager::class); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @return EntityManager |
||
253 | */ |
||
254 | public static function getEntityManager() |
||
255 | { |
||
256 | return Database::getManager(); |
||
257 | } |
||
258 | |||
259 | public static function getAssetRepository(): AssetRepository |
||
260 | { |
||
261 | return self::$container->get(AssetRepository::class); |
||
262 | } |
||
263 | |||
264 | public static function getResourceNodeRepository(): ResourceNodeRepository |
||
265 | { |
||
266 | return self::$container->get(ResourceNodeRepository::class); |
||
267 | } |
||
268 | |||
269 | public static function getAttendanceRepository(): CAttendanceRepository |
||
270 | { |
||
271 | return self::$container->get(CAttendanceRepository::class); |
||
272 | } |
||
273 | |||
274 | public static function getAnnouncementRepository(): CAnnouncementRepository |
||
275 | { |
||
276 | return self::$container->get(CAnnouncementRepository::class); |
||
277 | } |
||
278 | |||
279 | public static function getAccessUrlRepository(): AccessUrlRepository |
||
280 | { |
||
281 | return self::$container->get(AccessUrlRepository::class); |
||
282 | } |
||
283 | |||
284 | public static function getAnnouncementAttachmentRepository(): CAnnouncementAttachmentRepository |
||
285 | { |
||
286 | return self::$container->get(CAnnouncementAttachmentRepository::class); |
||
287 | } |
||
288 | |||
289 | public static function getTicketMessageAttachmentRepository(): TicketMessageAttachmentRepository |
||
290 | { |
||
291 | return self::$container->get(TicketMessageAttachmentRepository::class); |
||
292 | } |
||
293 | |||
294 | public static function getSocialPostAttachmentRepository(): SocialPostAttachmentRepository |
||
295 | { |
||
296 | return self::$container->get(SocialPostAttachmentRepository::class); |
||
297 | } |
||
298 | |||
299 | public static function getCourseRepository(): CourseRepository |
||
300 | { |
||
301 | return self::$container->get(CourseRepository::class); |
||
302 | } |
||
303 | |||
304 | public static function getCareerRepository(): CareerRepository |
||
305 | { |
||
306 | return self::$container->get(CareerRepository::class); |
||
307 | } |
||
308 | |||
309 | public static function getCourseCategoryRepository(): CourseCategoryRepository |
||
310 | { |
||
311 | return self::$container->get(CourseCategoryRepository::class); |
||
312 | } |
||
313 | |||
314 | public static function getCourseDescriptionRepository(): CCourseDescriptionRepository |
||
315 | { |
||
316 | return self::$container->get(CCourseDescriptionRepository::class); |
||
317 | } |
||
318 | |||
319 | public static function getCalendarEventRepository(): CCalendarEventRepository |
||
320 | { |
||
321 | return self::$container->get(CCalendarEventRepository::class); |
||
322 | } |
||
323 | |||
324 | public static function getCalendarEventAttachmentRepository(): CCalendarEventAttachmentRepository |
||
325 | { |
||
326 | return self::$container->get(CCalendarEventAttachmentRepository::class); |
||
327 | } |
||
328 | |||
329 | public static function getDocumentRepository(): CDocumentRepository |
||
332 | } |
||
333 | |||
334 | public static function getQuizCategoryRepository(): CQuizCategoryRepository |
||
335 | { |
||
336 | return self::$container->get(CQuizCategoryRepository::class); |
||
337 | } |
||
338 | |||
339 | public static function getExternalToolRepository(): ExternalToolRepository |
||
340 | { |
||
341 | return self::$container->get(ExternalToolRepository::class); |
||
342 | } |
||
343 | |||
344 | public static function getExtraFieldRepository(): ExtraFieldRepository |
||
345 | { |
||
346 | return self::$container->get(ExtraFieldRepository::class); |
||
347 | } |
||
348 | |||
349 | public static function getExtraFieldOptionsRepository(): ExtraFieldOptionsRepository |
||
350 | { |
||
351 | return self::$container->get(ExtraFieldOptionsRepository::class); |
||
352 | } |
||
353 | |||
354 | public static function getGlossaryRepository(): CGlossaryRepository |
||
355 | { |
||
356 | return self::$container->get(CGlossaryRepository::class); |
||
357 | } |
||
358 | |||
359 | public static function getGradeBookCategoryRepository(): GradeBookCategoryRepository |
||
360 | { |
||
361 | return self::$container->get(GradeBookCategoryRepository::class); |
||
362 | } |
||
363 | |||
364 | public static function getGradeBookCertificateRepository(): GradebookCertificateRepository |
||
365 | { |
||
366 | return self::$container->get(GradebookCertificateRepository::class); |
||
367 | } |
||
368 | |||
369 | public static function getGroupRepository(): CGroupRepository |
||
370 | { |
||
371 | return self::$container->get(CGroupRepository::class); |
||
372 | } |
||
373 | |||
374 | public static function getGroupCategoryRepository(): CGroupCategoryRepository |
||
375 | { |
||
376 | return self::$container->get(CGroupCategoryRepository::class); |
||
377 | } |
||
378 | |||
379 | public static function getForumRepository(): CForumRepository |
||
380 | { |
||
381 | return self::$container->get(CForumRepository::class); |
||
382 | } |
||
383 | |||
384 | public static function getForumCategoryRepository(): CForumCategoryRepository |
||
385 | { |
||
386 | return self::$container->get(CForumCategoryRepository::class); |
||
387 | } |
||
388 | |||
389 | public static function getForumPostRepository(): CForumPostRepository |
||
390 | { |
||
391 | return self::$container->get(CForumPostRepository::class); |
||
392 | } |
||
393 | |||
394 | public static function getForumAttachmentRepository(): CForumAttachmentRepository |
||
395 | { |
||
396 | return self::$container->get(CForumAttachmentRepository::class); |
||
397 | } |
||
398 | |||
399 | public static function getForumThreadRepository(): CForumThreadRepository |
||
400 | { |
||
401 | return self::$container->get(CForumThreadRepository::class); |
||
402 | } |
||
403 | |||
404 | public static function getIllustrationRepository(): IllustrationRepository |
||
405 | { |
||
406 | return self::$container->get(IllustrationRepository::class); |
||
407 | } |
||
408 | |||
409 | public static function getQuizRepository(): CQuizRepository |
||
410 | { |
||
411 | return self::$container->get(CQuizRepository::class); |
||
412 | } |
||
413 | |||
414 | public static function getQuestionRepository(): CQuizQuestionRepository |
||
415 | { |
||
416 | return self::$container->get(CQuizQuestionRepository::class); |
||
417 | } |
||
418 | |||
419 | public static function getQuestionCategoryRepository(): CQuizQuestionCategoryRepository |
||
420 | { |
||
421 | return self::$container->get(CQuizQuestionCategoryRepository::class); |
||
422 | } |
||
423 | |||
424 | public static function getLanguageRepository(): LanguageRepository |
||
425 | { |
||
426 | return self::$container->get(LanguageRepository::class); |
||
427 | } |
||
428 | |||
429 | public static function getLinkRepository(): CLinkRepository |
||
430 | { |
||
431 | return self::$container->get(CLinkRepository::class); |
||
432 | } |
||
433 | |||
434 | public static function getLinkCategoryRepository(): CLinkCategoryRepository |
||
435 | { |
||
436 | return self::$container->get(CLinkCategoryRepository::class); |
||
437 | } |
||
438 | |||
439 | public static function getLpRepository(): CLpRepository |
||
440 | { |
||
441 | return self::$container->get(CLpRepository::class); |
||
442 | } |
||
443 | |||
444 | public static function getLpItemRepository(): CLpItemRepository |
||
445 | { |
||
446 | return self::$container->get(CLpItemRepository::class); |
||
447 | } |
||
448 | |||
449 | public static function getLpCategoryRepository(): CLpCategoryRepository |
||
450 | { |
||
451 | return self::$container->get(CLpCategoryRepository::class); |
||
452 | } |
||
453 | |||
454 | public static function getMessageRepository(): MessageRepository |
||
455 | { |
||
456 | return self::$container->get(MessageRepository::class); |
||
457 | } |
||
458 | |||
459 | public static function getMessageAttachmentRepository(): MessageAttachmentRepository |
||
460 | { |
||
461 | return self::$container->get(MessageAttachmentRepository::class); |
||
462 | } |
||
463 | |||
464 | public static function getNotebookRepository(): CNotebookRepository |
||
465 | { |
||
466 | return self::$container->get(CNotebookRepository::class); |
||
467 | } |
||
468 | |||
469 | public static function getPersonalFileRepository(): PersonalFileRepository |
||
470 | { |
||
471 | return self::$container->get(PersonalFileRepository::class); |
||
472 | } |
||
473 | |||
474 | public static function getPromotionRepository(): PromotionRepository |
||
475 | { |
||
476 | return self::$container->get(PromotionRepository::class); |
||
477 | } |
||
478 | |||
479 | public static function getUserRepository(): UserRepository |
||
480 | { |
||
481 | return self::$container->get(UserRepository::class); |
||
482 | } |
||
483 | |||
484 | public static function getUsergroupRepository(): UsergroupRepository |
||
485 | { |
||
486 | return self::$container->get(UsergroupRepository::class); |
||
487 | } |
||
488 | |||
489 | public static function getUserToJsonNormalizer(): UserToJsonNormalizer |
||
490 | { |
||
491 | return self::$container->get(UserToJsonNormalizer::class); |
||
492 | } |
||
493 | |||
494 | public static function getShortcutRepository(): CShortcutRepository |
||
497 | } |
||
498 | |||
499 | public static function getStudentPublicationRepository(): CStudentPublicationRepository |
||
502 | } |
||
503 | |||
504 | public static function getStudentPublicationAssignmentRepository(): CStudentPublicationAssignmentRepository |
||
505 | { |
||
506 | return self::$container->get(CStudentPublicationAssignmentRepository::class); |
||
507 | } |
||
508 | |||
509 | public static function getStudentPublicationCommentRepository(): CStudentPublicationCommentRepository |
||
510 | { |
||
511 | return self::$container->get(CStudentPublicationCommentRepository::class); |
||
512 | } |
||
513 | |||
514 | public static function getStudentPublicationCorrectionRepository(): CStudentPublicationCorrectionRepository |
||
515 | { |
||
516 | return self::$container->get(CStudentPublicationCorrectionRepository::class); |
||
517 | } |
||
518 | |||
519 | public static function getSequenceResourceRepository(): SequenceResourceRepository |
||
520 | { |
||
521 | return self::$container->get(SequenceResourceRepository::class); |
||
522 | } |
||
523 | |||
524 | public static function getSequenceRepository(): SequenceRepository |
||
525 | { |
||
526 | return self::$container->get(SequenceRepository::class); |
||
527 | } |
||
528 | |||
529 | public static function getSessionRepository(): SessionRepository |
||
530 | { |
||
531 | return self::$container->get(SessionRepository::class); |
||
532 | } |
||
533 | |||
534 | public static function getSkillRepository(): SkillRepository |
||
535 | { |
||
536 | return self::$container->get(SkillRepository::class); |
||
537 | } |
||
538 | |||
539 | public static function getSurveyRepository(): CSurveyRepository |
||
540 | { |
||
541 | return self::$container->get(CSurveyRepository::class); |
||
542 | } |
||
543 | |||
544 | public static function getSurveyInvitationRepository(): CSurveyInvitationRepository |
||
545 | { |
||
546 | return self::$container->get(CSurveyInvitationRepository::class); |
||
547 | } |
||
548 | |||
549 | public static function getSurveyQuestionRepository(): CSurveyQuestionRepository |
||
550 | { |
||
551 | return self::$container->get(CSurveyQuestionRepository::class); |
||
552 | } |
||
553 | |||
554 | public static function getSysAnnouncementRepository(): SysAnnouncementRepository |
||
555 | { |
||
556 | return self::$container->get(SysAnnouncementRepository::class); |
||
557 | } |
||
558 | |||
559 | public static function getTagRepository(): TagRepository |
||
560 | { |
||
561 | return self::$container->get(TagRepository::class); |
||
562 | } |
||
563 | |||
564 | public static function getThematicRepository(): CThematicRepository |
||
565 | { |
||
566 | return self::$container->get(CThematicRepository::class); |
||
567 | } |
||
568 | |||
569 | public static function getThematicPlanRepository(): CThematicPlanRepository |
||
570 | { |
||
571 | return self::$container->get(CThematicPlanRepository::class); |
||
572 | } |
||
573 | |||
574 | public static function getThematicAdvanceRepository(): CThematicAdvanceRepository |
||
575 | { |
||
576 | return self::$container->get(CThematicAdvanceRepository::class); |
||
577 | } |
||
578 | |||
579 | public static function getTrackEExerciseRepository(): TrackEExerciseRepository |
||
580 | { |
||
581 | return self::$container->get(TrackEExerciseRepository::class); |
||
582 | } |
||
583 | |||
584 | public static function getTrackEDownloadsRepository(): TrackEDownloadsRepository |
||
585 | { |
||
586 | return self::$container->get(TrackEDownloadsRepository::class); |
||
587 | } |
||
588 | |||
589 | public static function getWikiRepository(): CWikiRepository |
||
590 | { |
||
591 | return self::$container->get(CWikiRepository::class); |
||
592 | } |
||
593 | |||
594 | public static function getToolIntroRepository(): CToolIntroRepository |
||
595 | { |
||
596 | return self::$container->get(CToolIntroRepository::class); |
||
597 | } |
||
598 | |||
599 | public static function getLegalRepository(): LegalRepository |
||
600 | { |
||
601 | return self::$container->get(LegalRepository::class); |
||
602 | } |
||
603 | |||
604 | public static function getFormFactory(): FormFactory |
||
605 | { |
||
606 | return self::$container->get('form.factory'); |
||
607 | } |
||
608 | |||
609 | public static function addFlash(string $message, string $type = 'success'): void |
||
610 | { |
||
611 | $type = match ($type) { |
||
612 | 'confirmation', 'confirm' => 'success', |
||
613 | default => 'info', |
||
614 | }; |
||
615 | |||
616 | $session = self::getSession(); |
||
617 | |||
618 | if ($session instanceof Session) { |
||
619 | $session->getFlashBag()->add($type, $message); |
||
620 | } |
||
621 | } |
||
622 | |||
623 | public static function getRouter(): Router |
||
624 | { |
||
625 | return self::$container->get('router'); |
||
626 | } |
||
627 | |||
628 | public static function getToolChain(): ToolChain |
||
629 | { |
||
630 | return self::$container->get(ToolChain::class); |
||
631 | } |
||
632 | |||
633 | public static function setLegacyServices(ContainerInterface $container): void |
||
641 | } |
||
642 | |||
643 | public static function getSocialPostRepository(): SocialPostRepository |
||
646 | } |
||
647 | |||
648 | public static function getTrackELoginRepository(): TrackELoginRepository |
||
649 | { |
||
650 | return self::$container->get(TrackELoginRepository::class); |
||
651 | } |
||
652 | |||
653 | public static function getTrackELoginRecordRepository(): TrackELoginRecordRepository |
||
654 | { |
||
655 | return self::$container->get(TrackELoginRecordRepository::class); |
||
656 | } |
||
657 | |||
658 | public static function getThemeHelper(): ThemeHelper |
||
659 | { |
||
660 | return self::$container->get(ThemeHelper::class); |
||
661 | } |
||
662 | |||
663 | public static function getAccessUrlHelper(): AccessUrlHelper |
||
664 | { |
||
665 | /** @var AccessUrlHelper $helper */ |
||
666 | $helper = self::$container->get(AccessUrlHelper::class); |
||
667 | |||
668 | return $helper; |
||
669 | } |
||
670 | } |
||
671 |