Completed
Push — master ( b4d909...e3c084 )
by Valentyn
03:37
created

ActorController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 10
dl 0
loc 82
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 13 1
A getActors() 0 6 1
A putActors() 0 33 1
1
<?php
2
3
namespace App\Actors\Controller;
4
5
use App\Actors\Entity\Actor;
6
use App\Actors\Entity\ActorTranslations;
7
use App\Actors\Repository\ActorRepository;
8
use App\Actors\Request\UpdateActorRequest;
9
use App\Controller\BaseController;
10
use App\Pagination\PaginatedCollection;
11
use App\Users\Entity\UserRoles;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
class ActorController extends BaseController
17
{
18
    /**
19
     * @Route("/api/actors", methods={"GET"})
20
     *
21
     * @param Request         $request
22
     * @param ActorRepository $repository
23
     *
24
     * @return JsonResponse
25
     */
26 2
    public function getAll(Request $request, ActorRepository $repository)
27
    {
28 2
        $actors = $repository->findAllWithTranslations();
29
30 2
        $offset = (int) $request->get('offset', 0);
31 2
        $limit = $request->get('limit', null);
32
33 2
        $movies = new PaginatedCollection($actors, $offset, $limit);
34
35 2
        return $this->response($movies, 200, [], [
36 2
            'groups' => ['list'],
37
        ]);
38
    }
39
40
    /**
41
     * @Route("/api/actors/{id}", methods={"GET"}, requirements={"id"="\d+"})
42
     *
43
     * @param Actor $actor
44
     *
45
     * @return JsonResponse
46
     */
47 2
    public function getActors(Actor $actor)
48
    {
49 2
        return $this->response($actor, 200, [], [
50 2
            'groups' => ['view'],
51
        ]);
52
    }
53
54
    /**
55
     * @Route("/api/actors/{id}", methods={"POST", "PUT", "PATCH"}, requirements={"id"="\d+"})
56
     *
57
     * @param Actor              $actor
58
     * @param UpdateActorRequest $request
59
     *
60
     * @throws \ErrorException
61
     *
62
     * @return JsonResponse
63
     */
64 2
    public function putActors(Actor $actor, UpdateActorRequest $request)
65
    {
66 2
        $this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN);
67
68 2
        $actorData = $request->get('actor');
69 2
        $actorTranslationData = $actorData['translations'];
70
71 2
        $actor->setOriginalName($actorData['originalName']);
72 2
        $actor->setImdbId($actorData['imdbId']);
73 2
        $actor->setGender($actorData['gender']);
74 2
        $actor->setBirthday(new \DateTimeImmutable($actorData['birthday']));
75
76
        $addTranslation = function (array $trans) use ($actor) {
77 1
            $actorTranslation = new ActorTranslations($actor, $trans['locale'], $trans['name']);
78 1
            $actorTranslation->setBiography($trans['biography']);
79 1
            $actorTranslation->setPlaceOfBirth($trans['placeOfBirth']);
80 1
            $actor->addTranslation($actorTranslation);
81 2
        };
82
83
        $updateTranslation = function (array $trans, ActorTranslations $oldTranslation) use ($actor) {
84 1
            $oldTranslation->setName($trans['name']);
85 1
            $oldTranslation->setBiography($trans['biography']);
86 1
            $oldTranslation->setPlaceOfBirth($trans['placeOfBirth']);
87 2
        };
88
89 2
        $actor->updateTranslations($actorTranslationData, $addTranslation, $updateTranslation);
90
91 2
        $em = $this->getDoctrine()->getManager();
92 2
        $em->persist($actor); // if there 1+ new translations lets persist movie to be sure that they will be saved
93 2
        $em->flush();
94
95 2
        return new JsonResponse(null, 202);
96
    }
97
}
98