|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Starkerxp\CampagneBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Starkerxp\CampagneBundle\Entity\Template; |
|
6
|
|
|
use Starkerxp\CampagneBundle\Form\Type\TemplateType; |
|
7
|
|
|
use Starkerxp\StructureBundle\Controller\StructureController; |
|
8
|
|
|
use Starkerxp\StructureBundle\Manager\Exception\ObjectClassNotAllowedException; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class TemplateController extends StructureController |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
public function cgetAction(Request $request) |
|
17
|
|
|
{ |
|
18
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
19
|
|
|
try { |
|
20
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
|
21
|
|
|
$orderBy = $this->getOrderBy($options['sort']); |
|
22
|
|
|
$resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']); |
|
23
|
|
|
} catch (\Exception $e) { |
|
24
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400);//400 |
|
25
|
|
|
} |
|
26
|
|
|
if (empty($resultSets)) { |
|
27
|
|
|
return new JsonResponse([]); |
|
28
|
|
|
} |
|
29
|
|
|
$retour = array_map( |
|
30
|
|
|
function ($element) use ($manager, $options) { |
|
31
|
|
|
return $manager->toArray($element, $this->getFields($options['fields'])); |
|
32
|
|
|
}, |
|
33
|
|
|
$resultSets |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
return new JsonResponse($retour); //400 |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getAction(Request $request) |
|
40
|
|
|
{ |
|
41
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
42
|
|
|
try { |
|
43
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
|
44
|
|
|
$template = $manager->findOneBy(['id' => $request->get('id')]); |
|
45
|
|
|
} catch (\Exception $e) { |
|
46
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400);//400 |
|
47
|
|
|
} |
|
48
|
|
|
if (!$template instanceof Template) { |
|
49
|
|
|
return new JsonResponse([], 404); |
|
50
|
|
|
} |
|
51
|
|
|
$retour = $manager->toArray($template, $this->getFields($options['fields'])); |
|
52
|
|
|
|
|
53
|
|
|
return new JsonResponse($retour);//400, 404 |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function postAction(Request $request) |
|
57
|
|
|
{ |
|
58
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
59
|
|
|
try { |
|
60
|
|
|
$template = new Template(); |
|
61
|
|
|
$form = $this->createForm(TemplateType::class, $template); |
|
62
|
|
|
$form->submit($request->request->all()); |
|
63
|
|
|
if ($form->isValid()) { |
|
64
|
|
|
$template = $form->getData(); |
|
65
|
|
|
$template->setUuid((\Ramsey\Uuid\Uuid::uuid4())->toString()); |
|
66
|
|
|
$manager->insert($template); |
|
67
|
|
|
|
|
68
|
|
|
return new JsonResponse([], 201);//400 |
|
69
|
|
|
} |
|
70
|
|
|
} catch (\Exception $e) { |
|
71
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function putAction(Request $request) |
|
78
|
|
|
{ |
|
79
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
80
|
|
|
$template = $manager->find($request->get('id')); |
|
81
|
|
|
if (empty($template)) { |
|
82
|
|
|
return new JsonResponse(["payload" => ""], 404); |
|
83
|
|
|
} |
|
84
|
|
|
try { |
|
85
|
|
|
$options = $manager->getPutOptionResolver()->resolve($request->request->all()); |
|
|
|
|
|
|
86
|
|
|
} catch (\Exception $e) { |
|
87
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return new JsonResponse(["payload" => ""], 303); //400 |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function deleteAction(Request $request) |
|
94
|
|
|
{ |
|
95
|
|
|
$manager = $this->get("starkerxp_campagne.manager.template"); |
|
96
|
|
|
$template = $manager->find($request->get('id')); |
|
97
|
|
|
if (empty($template)) { |
|
98
|
|
|
return new JsonResponse([], 404); |
|
99
|
|
|
} |
|
100
|
|
|
try { |
|
101
|
|
|
$manager->delete($template); |
|
102
|
|
|
} catch (ObjectClassNotAllowedException $e) { |
|
103
|
|
|
return new JsonResponse([], 400); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return new JsonResponse([], 204); //404 /400 |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.