ApiCourseModulesController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Model\CourseInterface;
8
use App\Model\DemoLessonInterface;
9
use App\Model\LessonInterface;
10
use App\Model\ModuleInterface;
11
use App\Repository\CourseRepositoryInterface;
12
use App\Repository\DemoLessonRepositoryInterface;
13
use App\Repository\ModuleRepositoryInterface;
14
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17
use Symfony\Component\Serializer\SerializerInterface;
18
19
final class ApiCourseModulesController extends AbstractController
20
{
21
    private CourseRepositoryInterface $courseRepository;
22
23
    private ModuleRepositoryInterface $moduleRepository;
24
25
    private DemoLessonRepositoryInterface $demoLessonRepository;
26
27
    public function __construct(
28
        CourseRepositoryInterface $courseRepository,
29
        ModuleRepositoryInterface $moduleRepository,
30
    DemoLessonRepositoryInterface $demoLessonRepository
31
    ) {
32
        $this->courseRepository = $courseRepository;
33
        $this->moduleRepository = $moduleRepository;
34
        $this->demoLessonRepository = $demoLessonRepository;
35
    }
36
37
    public function getAllForCourse(SerializerInterface $serializer, int $courseId): Response
38
    {
39
        $course = $this->courseRepository->getOneById($courseId);
40
        if (null === $course) {
41
            throw new NotFoundHttpException('Course was not found');
42
        }
43
44
        if (CourseInterface::COURSE_TYPE_DEMO === $course->getType() && null !== $course->getParent()) {
45
            $modules = $this->moduleRepository->getAllForCourse($course->getParent());
46
            $demoLessons = $this->demoLessonRepository->findAllForCourse($course->getParent());
47
            $enabledLessons = [];
48
            /** @var DemoLessonInterface $demoLesson */
49
            foreach ($demoLessons as $demoLesson) {
50
                $enabledLessons[] = $demoLesson->getLesson()->getId();
51
            }
52
53
            /** @var ModuleInterface $module */
54
            foreach ($modules as $module) {
55
                $module->setCourse($course);
56
57
                /** @var LessonInterface $lesson */
58
                foreach ($module->getLessons() as $lesson) {
59
                    if (!in_array($lesson->getId(), $enabledLessons)) {
60
                        $lesson->setBlocked(true);
61
                    }
62
                }
63
            }
64
        } else {
65
            $modules = $this->moduleRepository->getAllForCourse($course);
66
        }
67
68
        return new Response($serializer->serialize($modules, 'json', ['groups' => ['course_modules']]));
69
    }
70
}
71