VariableController::getAction()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 11

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2.0811

Importance

Changes 0
Metric Value
dl 25
loc 25
ccs 8
cts 11
cp 0.7272
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 2.0811
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Variable;
6
use AppBundle\Variable\Service;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
use AppBundle\Variable\Parser;
12
13
class VariableController extends Controller
14
{
15
16
    /**
17
     * @Route("/var", name="var_index")
18
     */
19 1
    public function indexAction()
20
    {
21 1
        $vars = $this->getDoctrine()->getRepository('AppBundle:Variable');
22
23 1
        $vars = $vars->findAll();
24
25 1
        $list = [];
26
        /** @var Variable $var */
27 1
        foreach ($vars as $var) {
28 1
            $list[$var->getName()] = $var->getValue();
29
        }
30
31 1
        return $this->sendResponse(true, $list);
32
    }
33
34
    /**
35
     * @Route("/var/{name}", methods={"GET"})
36
     * @param $name
37
     * @return Response
38
     */
39
40 3 View Code Duplication
    public function getAction($name)
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...
41
    {
42
        /** @var Service $vars */
43 3
        $vars = $this->get('vars');
44
45
        /** @var Variable $var */
46 3
        $var = $vars->get($name);
47
48 2
        if (!$var) {
49
            return $this->sendResponse(
50
                false,
51
                [
52
                    'message'=>'Variable not found',
53
                ]
54
            );
55
        }
56
57 2
        return $this->sendResponse(
58 2
            true,
59
            [
60 2
                    'name'=>$var->getName(),
61 2
                    'value'=>$var->getValue(),
62
            ]
63
        );
64
    }
65
66
    /**
67
     * @Route("/set/{name}", methods={"GET"})
68
     * @param Request $request
69
     * @param $name
70
     * @return Response
71
     * @throws \Exception
72
     */
73 2 View Code Duplication
    public function putAction(Request $request, $name)
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...
74
    {
75
76 2
        $varService = $this->get('vars');
77
78 2
        $value = $varService->set($name, $request);
79
80 2
        if (!$value) {
81 2
            return $this->sendResponse(
82 2
                true,
83
                [
84 2
                    'message'=>'Cannot set variable, maybe value param is missing?',
85
                ]
86
            );
87
        }
88
89 2
        return $this->sendResponse(
90 2
            true,
91
            [
92 2
                'message'=>'Set: '.$value,
93
            ]
94
        );
95
    }
96
97 3 View Code Duplication
    public function sendResponse($success, array $resp)
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...
98
    {
99 3
        $response = new Response();
100 3
        $response->setContent(json_encode(array(
101 3
            'result' => $success ? 'ok' : 'fail',
102 3
            'response'=> $resp,
103
        )));
104 3
        $response->headers->set('Content-Type', 'application/json');
105 3
        return $response;
106
    }
107
}
108