Completed
Push — master ( c811c3...8d5df0 )
by Tomáš
05:43
created

PageController::putPageSectionOrderAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 15
cts 15
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * This file is part of Webcook common bundle.
5
 *
6
 * See LICENSE file in the root of the bundle. Webcook
7
 */
8
9
namespace Webcook\Cms\CoreBundle\Controller;
10
11
use Webcook\Cms\CoreBundle\Base\BaseRestController;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
use Webcook\Cms\CoreBundle\Entity\Page;
14
use Webcook\Cms\CoreBundle\Entity\PageSection;
15
use Webcook\Cms\CoreBundle\Form\Type\PageType;
16
use Webcook\Cms\CoreBundle\Form\Type\PageSectionType;
17
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
18
use Webcook\Cms\SecurityBundle\Authorization\Voter\WebcookCmsVoter;
19
use FOS\RestBundle\Controller\Annotations\Get;
20
use FOS\RestBundle\Controller\Annotations\Post;
21
use FOS\RestBundle\Controller\Annotations\Put;
22
use FOS\RestBundle\Controller\Annotations\Delete;
23
use Doctrine\DBAL\LockMode;
24
25
/**
26
 * Page controller.
27
 */
28
class PageController extends BaseRestController
29
{
30
    /**
31
     * Get all Pages.
32
     *
33
     * @ApiDoc(
34
     *  resource=true,
35
     *  description="Return collection of Pages.",
36
     * )
37
     * @Get(options={"i18n"=false})
38
     */
39 1
    public function getPagesAction()
40
    {
41 1
        $this->checkPermission(WebcookCmsVoter::ACTION_VIEW);
42
43 1
        $pages = $this->getEntityManager()->getRepository('Webcook\Cms\CoreBundle\Entity\Page')->findAll();
44 1
        $view = $this->view($pages, 200);
45
46 1
        return $this->handleView($view);
47
    }
48
49
    /**
50
     * Get single Page.
51
     *
52
     * @param int $id Id of the desired Page.
53
     *
54
     * @ApiDoc(
55
     *  description="Return single Page.",
56
     *  parameters={
57
     *      {"name"="PageId", "dataType"="integer", "required"=true, "description"="Page id."}
58
     *  }
59
     * )
60
     * @Get(options={"i18n"=false})
61
     */
62 2
    public function getPageAction($id)
63
    {
64 2
        $this->checkPermission(WebcookCmsVoter::ACTION_VIEW);
65
66 2
        $page = $this->getPageById($id);
67 2
        $view = $this->view($page, 200);
68
69 2
        return $this->handleView($view);
70
    }
71
72
    /**
73
     * Save new Page.
74
     *
75
     * @ApiDoc(
76
     *  description="Create a new Page.",
77
     *  input="Webcook\Cms\CoreBundle\Form\Type\PageType",
78
     *  output="Webcook\Cms\CoreBundle\Entity\Page",
79
     * )
80
     * @Post(options={"i18n"=false})
81
     */
82 2 View Code Duplication
    public function postPagesAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 2
        $this->checkPermission(WebcookCmsVoter::ACTION_INSERT);
85
86 2
        $response = $this->processPageForm(new Page(), 'POST');
87
88 2
        if ($response instanceof Page) {
89 1
            $statusCode = 200;
90 1
            $message = 'Page has been added.';
91
        } else {
92 1
            $statusCode = 400;
93 1
            $message = 'Error while adding new Page.';
94
        }
95
96 2
        $view = $this->getViewWithMessage($response, $statusCode, $message);
0 ignored issues
show
Documentation introduced by
$response is of type object<Symfony\Component\Form\Form>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
98 2
        return $this->handleView($view);
99
    }
100
101
    /**
102
     * Update Page.
103
     *
104
     * @param int $id Id of the desired Page.
105
     *
106
     * @ApiDoc(
107
     *  description="Update existing Page.",
108
     *  input="Webcook\Cms\CoreBundle\Form\Type\PageType",
109
     *  output="Webcook\Cms\CoreBundle\Entity\Page"
110
     * )
111
     * @Put(options={"i18n"=false})
112
     */
113 3 View Code Duplication
    public function putPageAction($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115 3
        $this->checkPermission(WebcookCmsVoter::ACTION_EDIT);
116
117
        try {
118 3
            $page = $this->getPageById($id, $this->getLockVersion((string) new Page()));
119 1
        } catch (NotFoundHttpException $e) {
120 1
            $page = new Page();
121
        }
122
123 3
        $response = $this->processPageForm($page, 'PUT');
124
125 3
        if ($response instanceof Page) {
126 2
            $statusCode = 204;
127 2
            $message = 'Page has been updated.';
128
        } else {
129 1
            $statusCode = 400;
130 1
            $message = 'Error while updating Page.';
131
        }
132
133 3
        $view = $this->getViewWithMessage($response, $statusCode, $message);
0 ignored issues
show
Documentation introduced by
$response is of type object<Symfony\Component\Form\Form>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
135 3
        return $this->handleView($view);
136
    }
137
138
    /**
139
     * Update Page.
140
     *
141
     * @param int $id Id of the desired Page.
142
     *
143
     * @ApiDoc(
144
     *  description="Update order of page section.",
145
     *  input="Webcook\Cms\CoreBundle\Form\Type\PageSectionType"
146
     * )
147
     * @Put("/page-section/order/{id}", options={"i18n"=false})
148
     */
149 1
    public function putPageSectionOrderAction($id)
150
    {
151 1
        $this->checkPermission(WebcookCmsVoter::ACTION_EDIT);
152
153 1
        $pageSection = $this->getEntityManager()->getRepository('Webcook\Cms\CoreBundle\Entity\PageSection')->find($id);
154
155 1
        if (!is_null($pageSection)) {
156 1
            $form = $this->createForm(PageSectionType::class);
157 1
            $form = $this->formSubmit($form, 'PUT');
158 1
            if ($form->isValid()) {
159 1
                $data = $form->getData();
160 1
                $pageSection->setOrder($data['order']);
161 1
                $this->getEntityManager()->flush();
162
163 1
                $statusCode = 204;
164 1
                $message = 'Page section has been updated.';
165
            } else {
166
                $statusCode = 400;
167 1
                $message = 'Given data are not valid.';
168
            }
169
        } else {
170
            $statusCode = 404;
171
            $message = 'Page section not found.';
172
        }
173
174 1
        $view = $this->getViewWithMessage(null, $statusCode, $message);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
175
176 1
        return $this->handleView($view);
177
    }
178
179
    /**
180
     * Delete Page.
181
     *
182
     * @param int $id Id of the desired Page.
183
     *
184
     * @ApiDoc(
185
     *  description="Delete Page.",
186
     *  parameters={
187
     *     {"name"="PageId", "dataType"="integer", "required"=true, "description"="Page id."}
188
     *  }
189
     * )
190
     * @Delete(options={"i18n"=false})
191
     */
192 1 View Code Duplication
    public function deletePageAction($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
    {
194 1
        $this->checkPermission(WebcookCmsVoter::ACTION_DELETE);
195
196 1
        $page = $this->getPageById($id);
197
198 1
        $this->getEntityManager()->remove($page);
199 1
        $this->getEntityManager()->flush();
200
201 1
        $view = $this->getViewWithMessage(array(), 200, 'Page has been deleted.');
202
203 1
        return $this->handleView($view);
204
    }
205
206
    /**
207
     * Return form if is not valid, otherwise process form and return Page object.
208
     *
209
     * @param Page   $page
210
     * @param string $method Method of request
211
     *
212
     * @return \Symfony\Component\Form\Form [description]
213
     */
214 5
    private function processPageForm(Page $page, String $method = 'POST')
215
    {
216 5
        $form = $this->createForm(PageType::class, $page);
217 5
        $form = $this->formSubmit($form, $method);
218 5
        if ($form->isValid()) {
219 3
            $page = $form->getData();
220
221 3
            if ($page instanceof Page) {
222 3
                $sections = $page->getSections();
223
224 3
                foreach ($sections as $section) {
225
                    $section->setPage($page);
226
                }
227
228 3
                $this->getEntityManager()->persist($page);
229
            }
230
231 3
            $this->getEntityManager()->flush();
232
233 3
            return $page;
234
        }
235
236 2
        return $form;
237
    }
238
239
    /**
240
     * Get Page by id.
241
     *
242
     * @param int $id [description]
243
     *
244
     * @return Page
245
     *
246
     * @throws NotFoundHttpException If Page doesn't exist
247
     */
248 5 View Code Duplication
    private function getPageById($id, $expectedVersion = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
    {
250 5
        if ($expectedVersion) {
251 1
            $page = $this->getEntityManager()->getRepository('Webcook\Cms\CoreBundle\Entity\Page')->find($id, LockMode::OPTIMISTIC, $expectedVersion);
0 ignored issues
show
Unused Code introduced by
The call to ObjectRepository::find() has too many arguments starting with \Doctrine\DBAL\LockMode::OPTIMISTIC.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
252
        } else {
253 5
            $page = $this->getEntityManager()->getRepository('Webcook\Cms\CoreBundle\Entity\Page')->find($id);
254
        }
255
256 5
        if (!$page instanceof Page) {
257 1
            throw new NotFoundHttpException('Page not found.');
258
        }
259
260 4
        $this->saveLockVersion($page);
261
262 4
        return $page;
263
    }
264
}
265