Completed
Pull Request — master (#4)
by Kirill
04:26
created

DashboardController::saveWidgetsAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 17
cp 0
rs 8.8571
cc 3
eloc 17
nc 3
nop 1
crap 12
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Widget;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class DashboardController extends Controller
12
{
13
    /**
14
     * @Route("/dashboard", name="var_dashboard")
15
     */
16
    public function indexAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
    {
18
19
        // replace this example code with whatever you need
20
        return $this->render('dashboard/index.html.twig', [
21
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
22
        ]);
23
    }
24
25
    /**
26
     * @Route("/dashboard/widgets.json", methods={"GET"}, name="dashboard_widgets")
27
     * @param Request $request
28
     * @return Response
29
     */
30 1
    public function widgetsAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32 1
        $widgets = $this->getDoctrine()->getRepository('AppBundle:Widget');
33
34 1
        $widgets = $widgets->findAll();
35
36 1
        $list = [];
37
        /** @var Widget $widget */
38 1
        foreach ($widgets as $widget) {
39 1
            $list[$widget->getId()] = [
40 1
                'id'=>$widget->getId(),
41 1
                'x'=>$widget->getX(),
42 1
                'y'=>$widget->getY(),
43 1
                'width'=>$widget->getWidth(),
44 1
                'height'=>$widget->getHeight(),
45 1
                'name'=>$widget->getName(),
46 1
                'value'=>$widget->getVariable()->getValue()
47
            ];
48
        }
49 1
        $response = new Response();
50 1
        $response->setContent(json_encode(array(
51 1
            'result' =>'ok',
52 1
            'widgets'=> $list,
53
        )));
54 1
        $response->headers->set('Content-Type', 'application/json');
55 1
        return $response;
56
    }
57
    /**
58
     * @Route("/dashboard/widgets.json", methods={"POST"}, name="dashboard_widgets_save")
59
     * @param Request $request
60
     * @return Response
61
     */
62
    public function saveWidgetsAction(Request $request)
63
    {
64
65
        $widgets = $request->get('widgets');
66
67
        foreach ($widgets as $widget) {
68
            $w = $this->getDoctrine()->getRepository('AppBundle:Widget')->find($widget['id']);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $w is correct as $this->getDoctrine()->ge...')->find($widget['id']) (which targets Doctrine\Common\Persiste...bjectRepository::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
69
            if (!$w) {
70
                continue;
71
            }
72
73
            $w->setWidth($widget['size_x']);
74
            $w->setHeight($widget['size_y']);
75
            $w->setX($widget['col']);
76
            $w->setY($widget['row']);
77
78
            $this->getDoctrine()->getManager()->persist($w);
79
        }
80
81
        $this->getDoctrine()->getManager()->flush();
82
83
        $response = new Response();
84
        $response->setContent(json_encode(array(
85
            'result' =>'ok',
86
        )));
87
        $response->headers->set('Content-Type', 'application/json');
88
        return $response;
89
    }
90
}
91