Passed
Push — master ( f5aa3d...244e6c )
by Paweł
03:13 queued 11s
created

ApiLessonsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Form\LessonProgressType;
8
use App\Model\LessonInterface;
9
use App\Repository\LessonRepositoryInterface;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\Form\FormFactoryInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Serializer\SerializerInterface;
17
18
final class ApiLessonsController extends AbstractController
19
{
20
    private $lessonsRepository;
21
22
    private $serializer;
23
24
    public function __construct(LessonRepositoryInterface $lessonsRepository, SerializerInterface $serializer)
25
    {
26
        $this->lessonsRepository = $lessonsRepository;
27
        $this->serializer = $serializer;
28
    }
29
30
    public function getSingleModule(string $lessonId): Response
31
    {
32
        $lesson = $this->getLessonById($lessonId);
33
34
        return new Response($this->serializer->serialize($lesson, 'json', ['groups' => ['lesson_details']]));
35
    }
36
37
    public function setProgress(
38
        Request $request,
39
        EntityManagerInterface $entityManager,
40
        FormFactoryInterface $formFactory,
41
        string $lessonId
42
    ): Response {
43
        $lesson = $this->getLessonById($lessonId);
44
        $form = $formFactory->createNamed('', LessonProgressType::class, $lesson, ['method' => Request::METHOD_PATCH]);
45
        $form->handleRequest($request);
46
        if ($form->isSubmitted() && $form->isValid()) {
47
            $entityManager->flush();
48
49
            return new Response($this->serializer->serialize($lesson, 'json', ['groups' => ['lesson_details']]), Response::HTTP_OK);
50
        }
51
52
        return new Response($this->serializer->serialize($form, 'json'), Response::HTTP_BAD_REQUEST);
53
    }
54
55
    private function getLessonById(string $lessonId): LessonInterface
56
    {
57
        $lesson = $this->lessonsRepository->findOneBy(['id' => $lessonId]);
0 ignored issues
show
Bug introduced by
The method findOneBy() does not exist on App\Repository\LessonRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Repository\LessonRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        $lesson = $this->lessonsRepository->findOneBy(['id' => $lessonId]);
Loading history...
58
        if (null === $lesson) {
59
            throw new NotFoundHttpException('Lesson was not found');
60
        }
61
62
        return $lesson;
63
    }
64
}
65