IncidentController::deleteAction()   A
last analyzed

Complexity

Conditions 3
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 20
ccs 10
cts 12
cp 0.8333
rs 9.4285
cc 3
eloc 13
nc 6
nop 1
crap 3.0416
1
<?php
2
3
namespace IncidentBundle\Controller;
4
5
use Exception;
6
use IncidentBundle\Entity\Incident;
7
use JMS\Serializer\Serializer;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
14
/**
15
 * Class IncidentController
16
 */
17
class IncidentController extends Controller
18
{
19
    /**
20
     * @var Serializer;
21
     */
22
    private $serializer;
23
24
    /**
25
     * Get an exist incident
26
     *
27
     * @Route("/{ident}")
28
     * @Method({"GET"})
29
     *
30
     * @param int $ident
31
     *
32
     * @return Response
33
     */
34 2
    public function getAction($ident)
35
    {
36 2
        $errors = [];
37 2
        set_error_handler($this->getErrorHandlerCallback($errors));
38
39 2
        $responseData = ['data' => [], 'error' => []];
40
        try {
41 2
            if ($incident = $this->getIncident($ident, $errors)) {
42
                /** @var Serializer $serializer */
43 1
                $serializer = $this->get('jms_serializer');
44 2
                $responseData['data'] = $serializer->toArray($incident);
45
            }
46
        } catch (Exception $e) {
47
            $errors[] = $e;
48
        }
49 2
        $responseData['errors'] = $errors;
50
51 2
        return new Response(json_encode($responseData));
52
    }
53
54
    /**
55
     * Updates an exist incident
56
     *
57
     * @Route("/{ident}")
58
     * @Method({"POST"})
59
     *
60
     * @param int     $ident
61
     * @param Request $request
62
     *
63
     * @return Response
64
     */
65 2
    public function postAction($ident, Request $request)
66
    {
67 2
        $errors = [];
68 2
        set_error_handler($this->getErrorHandlerCallback($errors));
69
70 2
        $responseData = ['data' => [], 'errors' => []];
71
        try {
72 2
            if ($incident = $this->getIncident($ident, $errors)) {
73 1
                $entityManager = $this->getDoctrine()->getManager();
74
                /** @var Serializer $serializer */
75 1
                $serializer = $this->get('jms_serializer');
76 1
                $incidentNew = $serializer->deserialize($request->getContent(), Incident::class, 'json');
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, JMS\Serializer\Serializer::deserialize() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
77 1
                $incidentNew->setId($incident->getId());
78 1
                $entityManager->merge($incidentNew);
79 1
                $entityManager->flush();
80
81 2
                $responseData['data'] = $this->get('jms_serializer')->toArray($incident);
82
            }
83
        } catch (Exception $e) {
84
            $errors[] = $e;
85
        }
86 2
        $responseData['errors'] = $errors;
87
88 2
        return new Response(json_encode($responseData));
89
    }
90
91
    /**
92
     * Creates a new incident
93
     *
94
     * @Route("/")
95
     * @Method({"PUT"})
96
     *
97
     * @param Request $request
98
     *
99
     * @return Response
100
     */
101 2
    public function putAction(Request $request)
102
    {
103 2
        $errors = [];
104 2
        set_error_handler($this->getErrorHandlerCallback($errors));
105
106 2
        $responseData = ['data' => [], 'errors' => []];
107
        try {
108
109 2
            $entityManager = $this->getDoctrine()->getManager();
110
            /** @var Serializer $serializer */
111 2
            $serializer = $this->get('jms_serializer');
112
            /** @var Incident $incident */
113 2
            $incident = $serializer->deserialize($request->getContent(), Incident::class, 'json');
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, JMS\Serializer\Serializer::deserialize() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
114
115 2
            $entityManager->persist($incident);
116
117 2
            $entityManager->flush();
118
119
120 1
            $responseData['data'] = $this->get('jms_serializer')->toArray($incident);
121 1
        } catch (Exception $e) {
122 1
            $errors[] = $e->getMessage();
123
        }
124 2
        $responseData['errors'] = $errors;
125
126 2
        return new Response(json_encode($responseData));
127
    }
128
129
    /**
130
     * Delete an exist incident
131
     *
132
     * @Route("/{ident}")
133
     * @Method({"DELETE"})
134
     *
135
     * @param int $ident
136
     *
137
     * @return Response
138
     */
139 2
    public function deleteAction($ident)
140
    {
141 2
        $errors = [];
142 2
        set_error_handler($this->getErrorHandlerCallback($errors));
143
144 2
        $responseData = ['data' => [], 'errors' => []];
145
        try {
146 2
            if ($incident = $this->getIncident($ident, $errors)) {
147 1
                $entityManager = $this->getDoctrine()->getManager();
148 1
                $entityManager->remove($incident);
149 2
                $entityManager->flush();
150
            }
151
        } catch (Exception $e) {
152
            $errors[] = $e;
153
        }
154
155 2
        $responseData['errors'] = $errors;
156
157 2
        return new Response(json_encode($responseData));
158
    }
159
160
    /**
161
     * @return Serializer
162
     */
163
    public function getSerializer()
164
    {
165
        return $this->serializer;
166
    }
167
168
    /**
169
     * @param Serializer $serializer
170
     */
171
    protected function setSerializer(Serializer $serializer)
172
    {
173
        $this->serializer = $serializer;
174
    }
175
176
    /**
177
     * @param string $ident
178
     * @param array  $errors
179
     *
180
     * @return Incident
181
     */
182 6
    protected function getIncident($ident, &$errors)
183
    {
184 6
        $incident = $this->getDoctrine()
185 6
            ->getRepository('IncidentBundle:Incident')
186 6
            ->findOneBy(['ident' => $ident], ['id' => 'desc']);
0 ignored issues
show
Unused Code introduced by
The call to ObjectRepository::findOneBy() has too many arguments starting with array('id' => 'desc').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
187 6
        if (!$incident) {
188 4
            $errors[] = ['code' => 404, 'text' => 'Incident not find'];
189
        }
190
191 6
        return $incident;
192
    }
193
194
    /**
195
     * @param array $errors
196
     *
197
     * @return \Closure
198
     */
199
    protected function getErrorHandlerCallback(&$errors)
200
    {
201 7
        return function ($errno, $errstr, $errfile, $errline) use (&$errors) {
0 ignored issues
show
Unused Code introduced by
The parameter $errfile 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...
Unused Code introduced by
The parameter $errline 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...
202
            $errors[] = ['code' => $errno, 'text' => $errstr];
203 7
        };
204
    }
205
}
206