Completed
Push — master ( 57a1ef...d5f3e2 )
by Ivo
02:52
created

ContentController::getDataFromJsonRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivoaz ContentEditable bundle.
5
 *
6
 * (c) Ivo Azirjans <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Ivoaz\Bundle\ContentEditableBundle\Controller;
13
14
use Ivoaz\Bundle\ContentEditableBundle\Form\Type\BatchType;
15
use Ivoaz\Bundle\ContentEditableBundle\Form\Type\ContentBatchType;
16
use Ivoaz\Bundle\ContentEditableBundle\Form\Type\ContentType;
17
use Ivoaz\Bundle\ContentEditableBundle\Manager\ContentManagerInterface;
18
use Symfony\Component\Form\FormFactoryInterface;
19
use Symfony\Component\Form\FormInterface;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
23
use Symfony\Component\Validator\Constraints;
24
25
/**
26
 * @author Ivo Azirjans <[email protected]>
27
 */
28
class ContentController
29
{
30
    /**
31
     * @var ContentManagerInterface
32
     */
33
    private $manager;
34
35
    /**
36
     * @var FormFactoryInterface
37
     */
38
    private $formFactory;
39
40
    /**
41
     * @var AuthorizationCheckerInterface
42
     */
43
    private $authorizationChecker;
44
45
    /**
46
     * @param ContentManagerInterface       $manager
47
     * @param FormFactoryInterface          $formFactory
48
     * @param AuthorizationCheckerInterface $authorizationChecker
49
     */
50 8
    public function __construct(
51
        ContentManagerInterface $manager,
52
        FormFactoryInterface $formFactory,
53
        AuthorizationCheckerInterface $authorizationChecker
54
    ) {
55 8
        $this->manager = $manager;
56 8
        $this->formFactory = $formFactory;
57 8
        $this->authorizationChecker = $authorizationChecker;
58 8
    }
59
60
    /**
61
     * @param Request $request
62
     *
63
     * @return JsonResponse
64
     */
65 4
    public function updateAction(Request $request)
66
    {
67 4
        if (!$this->isAuthorized()) {
68 1
            return new JsonResponse(null, JsonResponse::HTTP_FORBIDDEN);
69
        }
70
71 3
        $id = $request->get('id');
72 3
        $content = $this->manager->find($id);
73
74 3
        if (null === $content) {
75 1
            return $this->createErrorResponse([$this->createContentNotFoundError($id)]);
76
        }
77
78 2
        $form = $this->formFactory->create(ContentType::class);
79 2
        $form->submit($this->getDataFromJsonRequest($request));
80
81 2
        if (!$form->isValid()) {
82 1
            $errors = $this->getErrors($form);
83
84 1
            return $this->createErrorResponse($errors);
85
        }
86
87 1
        $form->getData()->update($content);
88 1
        $this->manager->update($content);
89
90 1
        return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
91
    }
92
93
    /**
94
     * @param Request $request
95
     *
96
     * @return JsonResponse
97
     */
98 4
    public function batchUpdateAction(Request $request)
99
    {
100 4
        if (!$this->isAuthorized()) {
101 1
            return new JsonResponse(null, JsonResponse::HTTP_FORBIDDEN);
102
        }
103
104 3
        $form = $this->formFactory->create(BatchType::class);
105 3
        $form->submit($this->getDataFromJsonRequest($request));
106
107 3
        if (!$form->isValid()) {
108 1
            $errors = $this->getErrors($form);
109
110 1
            return $this->createErrorResponse($errors);
111
        }
112
113 2
        $errors = [];
114 2
        $contents = [];
115
116 2
        foreach ($form->getData()->contents as $data) {
117 2
            $content = $this->manager->find($data->id);
118
119 2
            if (null === $content) {
120 1
                $errors[] = $this->createContentNotFoundError($data->id);
121
122 1
                continue;
123
            }
124
125 1
            $data->update($content);
126 1
            $contents[] = $content;
127 2
        }
128
129 2
        if (0 !== count($errors)) {
130 1
            return $this->createErrorResponse($errors);
131
        }
132
133 1
        foreach ($contents as $content) {
134 1
            $this->manager->update($content);
135 1
        }
136
137 1
        return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
138
    }
139
140
    /**
141
     * @param FormInterface $form
142
     *
143
     * @return array
144
     */
145 2
    private function getErrors(FormInterface $form)
146
    {
147 2
        $errors = [];
148
149 2
        foreach ($form->getErrors(true, true) as $error) {
150 2
            $errors[] = ['field' => $error->getCause()->getPropertyPath(), 'title' => $error->getMessage()];
151 2
        }
152
153 2
        return $errors;
154
    }
155
156
    /**
157
     * @param array $errors
158
     * @param int   $status
159
     *
160
     * @return JsonResponse
161
     */
162 4
    private function createErrorResponse(array $errors, $status = 400)
163
    {
164 4
        return new JsonResponse(['errors' => $errors], $status);
165
    }
166
167
    /**
168
     * @return bool
169
     */
170 8
    private function isAuthorized()
171
    {
172 8
        return $this->authorizationChecker->isGranted('ROLE_ADMIN');
173
    }
174
175
    /**
176
     * @param $id
177
     *
178
     * @return array
179
     */
180 2
    private function createContentNotFoundError($id)
181
    {
182 2
        return ['title' => sprintf('Content with id "%s" was not found.', $id)];
183
    }
184
185
    /**
186
     * @param Request $request
187
     *
188
     * @return array|null
189
     */
190 5
    private function getDataFromJsonRequest(Request $request)
191
    {
192 5
        $data = json_decode($request->getContent(), true);
193
194 5
        return $data;
195
    }
196
}
197