Completed
Pull Request — master (#6)
by Kirill
09:33
created

DashboardController::updateAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 9.0856
cc 2
eloc 15
nc 2
nop 1
crap 2
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
        return $this->render('dashboard/index.html.twig', [
19
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
20
        ]);
21
    }
22
23
    /**
24
     * @Route("/dashboard/update.json", methods={"GET"}, name="dashboard_update")
25
     * @param Request $request
26
     * @return Response
27
     */
28
    public function updateAction(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...
29
    {
30 1
        $widgets = $this->getDoctrine()->getRepository('AppBundle:Widget');
31
32 1
        $widgets = $widgets->findAll();
33
34 1
        $list = [];
35
        /** @var Widget $widget */
36 1
        foreach ($widgets as $widget) {
37
            $list[$widget->getId()] = [
38 1
                'id'=>$widget->getId(),
39 1
                'lastchange'=>$widget->getVariable()->getLastupdate(),
40 1
                'value'=>$widget->getVariable()->getValue()
41 1
            ];
42 1
        }
43 1
        $response = new Response();
44 1
        $response->setContent(json_encode(array(
45 1
            'result' =>'ok',
46 1
            'widgets'=> $list,
47
        )));
48
        $response->headers->set('Content-Type', 'application/json');
49 1
        return $response;
50 1
    }
51 1
52 1
    /**
53
     * @Route("/dashboard/widgets.json", methods={"GET"}, name="dashboard_widgets")
54 1
     * @param Request $request
55 1
     * @return Response
56
     */
57
    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...
58
    {
59
        $widgets = $this->getDoctrine()->getRepository('AppBundle:Widget');
60
61
        $widgets = $widgets->findAll();
62
63
        $list = [];
64
        /** @var Widget $widget */
65
        foreach ($widgets as $widget) {
66
            $list[$widget->getId()] = [
67
                'id'=>$widget->getId(),
68
                'x'=>$widget->getX(),
69
                'y'=>$widget->getY(),
70
                'width'=>$widget->getWidth(),
71
                'height'=>$widget->getHeight(),
72
                'name'=>$widget->getName(),
73
                'value'=>$widget->getVariable()->getValue()
74
            ];
75
        }
76
        $response = new Response();
77
        $response->setContent(json_encode(array(
78
            'result' =>'ok',
79
            'widgets'=> $list,
80
        )));
81
        $response->headers->set('Content-Type', 'application/json');
82
        return $response;
83
    }
84
    /**
85
     * @Route("/dashboard/widgets.json", methods={"POST"}, name="dashboard_widgets_save")
86
     * @param Request $request
87
     * @return Response
88
     */
89
    public function saveWidgetsAction(Request $request)
90
    {
91
92
        $widgets = $request->get('widgets');
93
94
        foreach ($widgets as $widget) {
95
            $w = $this->getDoctrine()->getRepository('AppBundle:Widget')->find($widget['id']);
96
            if (!$w) {
97
                continue;
98
            }
99
100
            $w->setWidth($widget['size_x']);
101
            $w->setHeight($widget['size_y']);
102
            $w->setX($widget['col']);
103
            $w->setY($widget['row']);
104
105
            $this->getDoctrine()->getManager()->persist($w);
106
        }
107
108
        $this->getDoctrine()->getManager()->flush();
109
110
        $response = new Response();
111
        $response->setContent(json_encode(array(
112
            'result' =>'ok',
113
        )));
114
        $response->headers->set('Content-Type', 'application/json');
115
        return $response;
116
    }
117
}
118