Completed
Push — master ( 0b4911...4851fa )
by Valentyn
04:19 queued 11s
created

WatchedMovieController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 43.48%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 12
dl 0
loc 66
ccs 10
cts 23
cp 0.4348
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A postWatchedMovies() 0 23 3
A getAll() 0 19 2
1
<?php
2
3
namespace App\Guests\Controller;
4
5
use App\Controller\BaseController;
6
use App\Guests\Entity\GuestSession;
7
use App\Guests\Entity\GuestWatchedMovie;
8
use App\Guests\Repository\WatchedMovieRepository;
9
use App\Movies\Request\AddWatchedMovieRequest;
10
use App\Movies\Service\WatchedMovieService;
11
use App\Pagination\PaginatedCollection;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
class WatchedMovieController extends BaseController
18
{
19
    /**
20
     * @Route("/api/guests/{token}/watchedMovies", methods={"POST"});
21
     *
22
     * @param $token
23
     * @param AddWatchedMovieRequest $addWatchedMovieRequest
24
     * @param Request                $request
25
     * @param WatchedMovieService    $watchedMovieService
26
     *
27
     * @throws \Exception
28
     *
29
     * @return JsonResponse
30
     */
31 3
    public function postWatchedMovies($token, AddWatchedMovieRequest $addWatchedMovieRequest, Request $request, WatchedMovieService $watchedMovieService)
32
    {
33 3
        $em = $this->getDoctrine()->getManager();
34 3
        $guestSessionRepository = $em->getRepository(GuestSession::class);
35
36
        /** @var $guestSession GuestSession|null */
37 3
        $guestSession = $guestSessionRepository->findOneBy([
38 3
            'token' => $token,
39
        ]);
40
41 3
        if ($guestSession === null) {
42
            throw new NotFoundHttpException('Guest session not found by provided token');
43
        }
44
45 3
        $watchedMovieDTO = $addWatchedMovieRequest->getWatchedMovieDTO();
46 3
        $isMovieAdded = $watchedMovieService->addGuestWatchedMovie($guestSession, $watchedMovieDTO, $request->getLocale());
47
48 3
        if ($isMovieAdded === false) {
49
            throw new NotFoundHttpException('Movie not found by provided ID / TMDB ID');
50
        }
51
52 3
        return new JsonResponse(null, 202);
53
    }
54
55
    /**
56
     * @Route("/api/guests/{id<\d+>}/watchedMovies", methods={"GET"});
57
     *
58
     * @param Request      $request
59
     * @param GuestSession $guestSession
60
     *
61
     * @return JsonResponse
62
     */
63
    public function getAll(Request $request, GuestSession $guestSession)
64
    {
65
        $em = $this->getDoctrine()->getManager();
66
        /** @var $watchedMovieRepository WatchedMovieRepository */
67
        $watchedMovieRepository = $em->getRepository(GuestWatchedMovie::class);
68
69
        $offset = (int) $request->get('offset', 0);
70
        $limit = $request->get('limit', null);
71
72
        $watchedMovies = new PaginatedCollection(
73
            $watchedMovieRepository->getAllWatchedMoviesByGuestSessionId($guestSession->getId()),
74
            $offset,
75
            $limit ? (int) $limit : null
76
        );
77
78
        return $this->response($watchedMovies, 200, [], [
79
            'groups' => ['list'],
80
        ]);
81
    }
82
}
83