src/Controller/Front/Scholar/Module/ShowController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front\Scholar\Module;
  3. use App\Enum\ScholarStatusEnum;
  4. use App\Repository\Exercise\ExerciseRepository;
  5. use App\Repository\Scholar\ChapterRepository;
  6. use App\Repository\Scholar\LessonRepository;
  7. use App\Repository\Scholar\ModuleRepository;
  8. use App\Repository\Scholar\TrainingRepository;
  9. use App\Service\Achievement\AchievementManager;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class ShowController extends AbstractController
  16. {
  17.     public function __construct(
  18.         private TrainingRepository $trainingRepository,
  19.         private ModuleRepository $moduleRepository,
  20.         private LessonRepository $lessonRepository,
  21.         private ExerciseRepository $exerciseRepository,
  22.         private AchievementManager $achievementManager,
  23.         private ChapterRepository $chapterRepository,
  24.     ) {}
  25.     #[Route(path'/training/{trainingId}/module/{moduleId}'name'front_channel_module_show_redirection'methods: ['GET'])]
  26.     public function __invoke(Request $requeststring $trainingIdstring $moduleId): Response
  27.     {
  28.         /**
  29.          * Redirect to first lesson of selected module
  30.          * If module doesn't exist in the training, redirect to training
  31.          * Where user will be redirected
  32.          * path: '/training/{trainingId}/module/{moduleId}/lesson/{lessonId}', name: 'front_channel_lesson_show'
  33.          */
  34.         $training $this->trainingRepository->find($trainingId);
  35.         if ($training === null || $training->getStatus() !== ScholarStatusEnum::PUBLISHED) {
  36.             throw new NotFoundHttpException('Training not found.');
  37.         }
  38.         $module $this->moduleRepository->findOneByTrainingAndId($training$moduleId);
  39.         if ($module === null || !in_array($module->getStatus(), [ScholarStatusEnum::PUBLISHEDnull])) {
  40.             throw new NotFoundHttpException('Module not found.');
  41.         }
  42.         $lessons $module->getLessons();
  43.         if ($lessons !== null && count($lessons) > 0) {
  44.             return $this->redirectToRoute('front_channel_lesson_show', [
  45.                 'trainingId' => $trainingId,
  46.                 'moduleId' => $moduleId,
  47.                 'lessonId' => $lessons[0]->getLesson()->getId()
  48.             ]);
  49.         }
  50.         return $this->redirectToRoute('front_channel_training_show', ['id' => $trainingId]);
  51.     }
  52. }