Completed
Pull Request — dev (#40)
by
unknown
03:50
created

FormRequestController::addAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
3
namespace AppBundle\Controller\Api;
4
5
use AppBundle\Entity\FormRequest;
6
use AppBundle\Entity\FormRequestType;
7
use Mcfedr\JsonFormBundle\Controller\JsonController;
8
use AppBundle\Exception\JsonHttpException;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * @Route("/form_request", name="form_requests")
17
 */
18
class FormRequestController extends JsonController
19
{
20
    /**
21
     * @Route("/{status}", requirements={"status": "pending|past"})
22
     * @Method("GET")
23
     *
24
     * @return JsonResponse
25
     */
26
    public function listAction(Request $request, $status)
27
    {
28
        $em = $this->getDoctrine()->getManager();
29
        $requestForms = $this->get('knp_paginator')
30
            ->paginate(
31
                $em->getRepository(FormRequest::class)->findBy([
32
                    'user' => $this->getUser(),
33
                    'status' => $status == "pending" ? "pending" : ["approved", "rejected"],
34
                ]),
35
                $request->query->getInt('page', 1),
36
                10
37
            );
38
        return $this->json($requestForms);
39
    }
40
41
    /**
42
     * @Route("/{type}")
43
     * @Method("POST")
44
     *
45
     * @param FormRequestType $type
46
     * @param  Request $request
47
     * @return JsonResponse
48
     */
49
    public function addAction(FormRequestType $type, Request $request)
50
    {
51
        if (!$request->getContent()) {
52
            throw new JsonHttpException(404, 'Request body is empty');
53
        }
54
55
        $data = json_decode($request->getContent());
56
57
        if (!preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/', $data->date)) {
58
            throw new JsonHttpException(404, 'Invalid date format');
59
        }
60
61
        $date = new \DateTime($data->date);
62
        $em = $this->getDoctrine()->getManager();
63
        $formRequest = new FormRequest();
64
        $formRequest->setDate($date)
65
            ->setType($type)
66
            ->setUser($this->getUser());
0 ignored issues
show
Documentation introduced by
$this->getUser() is of type null|object, but the function expects a object<AppBundle\Entity\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
68
        $em->persist($formRequest);
69
        $em->flush();
70
71
        return $this->json("Request form created", 200);
72
    }
73
}
74