Completed
Push — master ( 4201d9...0a29a7 )
by Paweł
19s queued 10s
created

ApiCoursesController::getCourseAttachments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Repository\AttachmentRepositoryInterface;
8
use App\Repository\CourseRepositoryInterface;
9
use SWP\Component\Common\Exception\NotFoundHttpException;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Serializer\SerializerInterface;
13
14
final class ApiCoursesController extends AbstractController
15
{
16
    private $courseRepository;
17
18
    public function __construct(CourseRepositoryInterface $courseRepository)
19
    {
20
        $this->courseRepository = $courseRepository;
21
    }
22
23
    public function getAll(SerializerInterface $serializer): Response
24
    {
25
        $courses = $this->courseRepository->getAll();
26
27
        return new Response($serializer->serialize($courses, 'json', ['groups' => ['list_courses']]));
28
    }
29
30
    public function getCourseAttachments(
31
        SerializerInterface $serializer,
32
        AttachmentRepositoryInterface $attachmentRepository,
33
        int $courseId
34
    ): Response {
35
        $course = $this->courseRepository->getOneById($courseId);
36
        if (null === $course) {
37
            throw new NotFoundHttpException('Course was not found');
38
        }
39
40
        return new Response($serializer->serialize($attachmentRepository->getAllForCourse($course), 'json', ['groups' => ['attachments_list']]));
41
    }
42
}
43