Passed
Push — master ( e66a3e...15c60f )
by Paweł
04:21
created

ApiBookmarksController::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 16
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Entity\Bookmark;
8
use App\Form\BookmarkType;
9
use App\Form\ErrorHandler;
10
use App\Model\BookmarkInterface;
11
use App\Repository\BookmarkRepositoryInterface;
12
use App\Repository\LessonRepositoryInterface;
13
use Doctrine\ORM\EntityManagerInterface;
14
use SWP\Component\Common\Exception\NotFoundHttpException;
15
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16
use Symfony\Component\Form\FormFactoryInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Serializer\SerializerInterface;
20
21
final class ApiBookmarksController extends AbstractController
22
{
23
    private $bookmarkRepository;
24
25
    private $lessonRepository;
26
27
    private $serializer;
28
29
    public function __construct(
30
        BookmarkRepositoryInterface $bookmarkRepository,
31
        LessonRepositoryInterface $lessonRepository,
32
        SerializerInterface $serializer
33
    ) {
34
        $this->bookmarkRepository = $bookmarkRepository;
35
        $this->lessonRepository = $lessonRepository;
36
        $this->serializer = $serializer;
37
    }
38
39
    public function getAllForLesson(SerializerInterface $serializer, string $lessonId): Response
40
    {
41
        $lesson = $this->lessonRepository->getOneById($lessonId);
42
        if (null === $lesson) {
43
            throw new NotFoundHttpException('Lesson was not found');
44
        }
45
46
        $bookmarks = $this->bookmarkRepository->getAllForLesson($lesson);
47
48
        return new Response($serializer->serialize($bookmarks, 'json', ['groups' => ['bookmarks_list']]));
49
    }
50
51
    public function create(
52
        Request $request,
53
        EntityManagerInterface $entityManager,
54
        FormFactoryInterface $formFactory,
55
        string $lessonId
56
    ): Response {
57
        $lesson = $this->lessonRepository->getOneById($lessonId);
58
        if (null === $lesson) {
59
            throw new NotFoundHttpException('Lesson was not found');
60
        }
61
62
        $form = $formFactory->createNamed('', BookmarkType::class, new Bookmark());
63
        $form->handleRequest($request);
64
        if ($form->isSubmitted() && $form->isValid()) {
65
            /** @var BookmarkInterface $bookmark */
66
            $bookmark = $form->getData();
67
            $bookmark->setLesson($lesson);
68
            $bookmark->setUser($this->getUser());
69
70
            $entityManager->persist($bookmark);
71
            $entityManager->flush();
72
73
            return new Response($this->serializer->serialize($bookmark, 'json', ['groups' => ['bookmark_details']]), Response::HTTP_CREATED);
74
        }
75
76
        return new Response($this->serializer->serialize(ErrorHandler::getErrorsFromForm($form), 'json'), Response::HTTP_BAD_REQUEST);
77
    }
78
79
    public function delete(
80
        EntityManagerInterface $entityManager,
81
        string $bookmarkId
82
    ): Response {
83
        $bookmark = $this->bookmarkRepository->getOneById($bookmarkId);
84
        if (null === $bookmark) {
85
            throw new NotFoundHttpException('Bookmark was not found');
86
        }
87
88
        if ($bookmark->getUser() !== $this->getUser()) {
89
            throw $this->createAccessDeniedException();
90
        }
91
92
        $entityManager->remove($bookmark);
93
94
        return new Response(null, Response::HTTP_NO_CONTENT);
95
    }
96
}
97