Completed
Push — master ( 4851fa...058e40 )
by Valentyn
04:07
created

ActorController::getSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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\Filters\FilterBuilder;
11
use App\Pagination\PaginatedCollection;
12
use App\Users\Entity\UserRoles;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
use App\Filters\Actor as Filter;
17
18
class ActorController extends BaseController
19
{
20
    /**
21
     * @Route("/api/actors", methods={"GET"})
22
     *
23
     * @param Request         $request
24
     * @param ActorRepository $repository
25
     *
26
     * @return JsonResponse
27
     */
28 2
    public function getAll(Request $request, ActorRepository $repository)
29
    {
30 2
        $actors = $repository->findAllWithTranslations();
31
32 2
        $offset = (int) $request->get('offset', 0);
33 2
        $limit = $request->get('limit', null);
34
35 2
        $filter = new FilterBuilder(
36 2
            new Filter\Name()
37
        );
38
39 2
        $filter->process($request->query, $actors);
40
41 2
        $actors = new PaginatedCollection($actors->getQuery(), $offset, $limit);
42
43 2
        return $this->response($actors, 200, [], [
44 2
            'groups' => ['list'],
45
        ]);
46
    }
47
48
    /**
49
     * @Route("/api/actors/search", methods={"GET"})
50
     *
51
     * @param Request         $request
52
     * @param ActorRepository $repository
53
     *
54
     * @return JsonResponse
55
     */
56 3
    public function getSearch(Request $request, ActorRepository $repository): JsonResponse
57
    {
58 3
        $actors = $repository->findAllWithTranslations();
59
60 3
        $offset = (int) $request->get('offset', 0);
61 3
        $limit = $request->get('limit', null);
62
63 3
        $filter = new FilterBuilder(
64 3
            new Filter\Name()
65
        );
66
67 3
        $filter->process($request->query, $actors);
68
69 3
        $actors = new PaginatedCollection($actors->getQuery(), $offset, $limit);
70
71 3
        return $this->response($actors, 200, [], [
72 3
            'groups' => ['list'],
73
        ]);
74
    }
75
76
    /**
77
     * @Route("/api/actors/{id}", methods={"GET"}, requirements={"id"="\d+"})
78
     *
79
     * @param Actor $actor
80
     *
81
     * @return JsonResponse
82
     */
83 2
    public function getActors(Actor $actor)
84
    {
85 2
        return $this->response($actor, 200, [], [
86 2
            'groups' => ['view'],
87
        ]);
88
    }
89
90
    /**
91
     * @Route("/api/actors/{id}", methods={"POST", "PUT", "PATCH"}, requirements={"id"="\d+"})
92
     *
93
     * @param Actor              $actor
94
     * @param UpdateActorRequest $request
95
     *
96
     * @throws \ErrorException
97
     *
98
     * @return JsonResponse
99
     */
100 2
    public function putActors(Actor $actor, UpdateActorRequest $request)
101
    {
102 2
        $this->denyAccessUnlessGranted(UserRoles::ROLE_ADMIN);
103
104 2
        $actorData = $request->get('actor');
105 2
        $actorTranslationData = $actorData['translations'];
106
107 2
        $actor->setOriginalName($actorData['originalName']);
108 2
        $actor->setImdbId($actorData['imdbId']);
109 2
        $actor->setGender($actorData['gender']);
110 2
        $actor->setBirthday(new \DateTimeImmutable($actorData['birthday']));
111
112
        $addTranslation = function (array $trans) use ($actor) {
113 1
            $actorTranslation = new ActorTranslations($actor, $trans['locale'], $trans['name']);
114 1
            $actorTranslation->setBiography($trans['biography']);
115 1
            $actorTranslation->setPlaceOfBirth($trans['placeOfBirth']);
116 1
            $actor->addTranslation($actorTranslation);
117 2
        };
118
119
        $updateTranslation = function (array $trans, ActorTranslations $oldTranslation) use ($actor) {
120 1
            $oldTranslation->setName($trans['name']);
121 1
            $oldTranslation->setBiography($trans['biography']);
122 1
            $oldTranslation->setPlaceOfBirth($trans['placeOfBirth']);
123 2
        };
124
125 2
        $actor->updateTranslations($actorTranslationData, $addTranslation, $updateTranslation);
126
127 2
        $em = $this->getDoctrine()->getManager();
128 2
        $em->persist($actor); // if there 1+ new translations lets persist movie to be sure that they will be saved
129 2
        $em->flush();
130
131 2
        return new JsonResponse(null, 202);
132
    }
133
}
134