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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.