Completed
Push — master ( 7171ca...80a1b3 )
by Tomáš
06:18
created

PageController::putPageSectionOrderAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 24
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
nop 1
crap 12
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
     * Update Page.
32
     *
33
     * @param int $id Id of the desired Page.
34
     *
35
     * @ApiDoc(
36
     *  description="Update order of page section.",
37
     *  input="Webcook\Cms\CoreBundle\Form\Type\PageSectionType"
38
     * )
39
     * @Put("/page-section/order/{id}", options={"i18n"=false})
40
     */
41
    public function putPageSectionOrderAction($id)
42
    {
43
        $this->checkPermission(WebcookCmsVoter::ACTION_EDIT);
44
45
        $pageSection = $this->getEntityManager()->getRepository('Webcook\Cms\CoreBundle\Entity\PageSection')->find($id);
46
47
        if (!is_null($pageSection)) {
48
            $form = $this->createForm(PageSectionType::class);
49
            $form = $this->formSubmit($form, 'PUT');
50
            if ($form->isValid()) {
51
                $data = $form->getData();
52
                $pageSection->setOrder($data['order']);
53
                $this->getEntityManager()->flush();
54
55
                $statusCode = 204;
56
                $message = 'Page section has been updated.';
57
            } else {
58
                $statusCode = 400;
59
                $message = 'Given data are not valid.';
60
            }
61
        } else {
62
            $statusCode = 404;
63
            $message = 'Page section not found.';
64
        }
65
66
        $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...
67
68
        return $this->handleView($view);
69
    }
70
71
    /**
72
     * Get Page by id.
73
     *
74
     * @param int $id [description]
75
     *
76
     * @return Page
77
     *
78
     * @throws NotFoundHttpException If Page doesn't exist
79
     */
80 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...
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
81
    {
82
        if ($expectedVersion) {
83
            $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...
84
        } else {
85
            $page = $this->getEntityManager()->getRepository('Webcook\Cms\CoreBundle\Entity\Page')->find($id);
86
        }
87
88
        if (!$page instanceof Page) {
89
            throw new NotFoundHttpException('Page not found.');
90
        }
91
92
        $this->saveLockVersion($page);
93
94
        return $page;
95
    }
96
}
97