Completed
Push — master ( db4111...70a8a6 )
by Guillaume
02:27
created

LeadController::putAction()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 22
Code Lines 17

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 17
nc 10
nop 1
1
<?php
2
3
namespace Starkerxp\LeadBundle\Controller;
4
5
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6
use Starkerxp\LeadBundle\Entity\Lead;
7
use Starkerxp\LeadBundle\Events;
8
use Starkerxp\LeadBundle\Form\Type\LeadType;
9
use Starkerxp\StructureBundle\Controller\StructureController;
10
use Symfony\Component\EventDispatcher\GenericEvent;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
14
15
class LeadController extends StructureController
16
{
17
	/**
18
     * @ApiDoc(
19
     *      resource=true,
20
     *      description="Liste les leads.",
21
     *      section="starkerxp_lead.lead",
22
     *      parameters={
23
     *          {
24
     *              "name"="offset",
25
     *              "dataType"="integer",
26
     *              "requirement"="\d+",
27
     *              "description"="starkerxp_structure.doc.offset.result",
28
     *              "required"="false"
29
     *          },
30
     *          {
31
     *              "name"="limit",
32
     *              "dataType"="integer",
33
     *              "requirement"="\d+",
34
     *              "description"="starkerxp_structure.doc.limit.result",
35
     *              "required"="false"
36
     *          },
37
     *          {
38
     *              "name"="fields",
39
     *              "dataType"="string",
40
     *              "requirement"="\w+",
41
     *              "description"="starkerxp_structure.doc.list_field.entity",
42
     *              "required"="false"
43
     *          },
44
     *          {
45
     *              "name"="sort",
46
     *              "dataType"="string",
47
     *              "requirement"="\w+",
48
     *              "description"="starkerxp_structure.doc.sort.result",
49
     *              "required"="false"
50
     *          }
51
     *      },
52
     *      views = { "default" }
53
     * )
54
     * 
55
     */
56 View Code Duplication
    public function cgetAction(Request $request)
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...
57
    {
58
        $manager = $this->get("starkerxp_lead.manager.lead");
59
        try {
60
            $options = $this->resolveParams()->resolve($request->query->all());
61
            $orderBy = $this->getOrderBy($options['sort']);
62
            $resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']);
63
        } catch (\Exception $e) {
64
            return new JsonResponse(["payload" => $e->getMessage()], 400);
65
        }
66
        if (empty($resultSets)) {
67
            return new JsonResponse([]);
68
        }
69
        $retour = array_map(
70
            function ($element) use ($manager, $options) {
71
                return $manager->toArray($element, $this->getFields($options['fields']));
72
            },
73
            $resultSets
74
        );
75
        return new JsonResponse($retour);
76
    }
77
78
	
79
	/**
80
     * @ApiDoc(
81
     *      resource=true,
82
     *      description="Affiche un lead.",
83
     *      section="starkerxp_lead.lead",
84
	 *      requirements={
85
     *          {
86
     *              "name"="lead_id",
87
     *              "dataType"="integer",
88
     *              "requirement"="\d+",
89
     *              "description"="Permet d'afficher l'élément choisis"
90
     *          }
91
     *      },
92
     *      parameters={
93
     *          {
94
     *              "name"="fields",
95
     *              "dataType"="string",
96
     *              "requirement"="\w+",
97
     *              "description"="starkerxp_structure.doc.list_field.entity",
98
     *              "required"="false"
99
     *          }
100
     *      },
101
     *      views = { "default" }
102
     * )
103
     */
104
    public function getAction(Request $request)
105
    {
106
        $manager = $this->get("starkerxp_lead.manager.lead");
107
        try {
108
            $options = $this->resolveParams()->resolve($request->query->all());
109
			if (!$entite = $manager->findOneBy(['id' => $request->get('lead_id')])) {
110
                return new JsonResponse(["payload" => $this->translate("entity.not_found", "lead")], 404);
111
            }
112
        } catch (\Exception $e) {
113
            return new JsonResponse(["payload" => $e->getMessage()], 400);
114
        }
115
        
116
        $retour = $manager->toArray($entite, $this->getFields($options['fields']));
117
118
        return new JsonResponse($retour);
119
    }
120
121
	/**
122
     * @ApiDoc(
123
     *      resource=true,
124
     *      description="Ajoute un lead.",
125
     *      section="starkerxp_lead.lead",
126
     *      views = { "default" }
127
     * )
128
     */
129
    public function postAction(Request $request)
130
    {
131
        $manager = $this->get("starkerxp_lead.manager.lead");
132
        try {
133
            $entite = new Lead();
134
            $form = $this->createForm(LeadType::class, $entite, ['method' => 'POST']);
135
            $form->submit($this->getRequestData($request));
136
            if ($form->isValid()) {
137
                $lead = $form->getData();
138
                $manager->insert($lead);
139
				$this->dispatch(Events::LEAD_CREATED, new GenericEvent($entite));
140
                return new JsonResponse(["payload" => $this->translate("lead.entity.created", "lead")], 201);
141
            }
142
        } catch (\Exception $e) {
143
            $manager->rollback();
144
            return new JsonResponse(["payload" => $e->getMessage()], 400);
145
        }
146
147
        return new JsonResponse(["payload" => $this->getFormErrors($form)], 400);
148
    }
149
150
} 
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
151