Completed
Pull Request — dev (#40)
by
unknown
05:22
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 Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
/**
15
 * @Route("/form_request", name="form_requests")
16
 */
17
class FormRequestController extends JsonController
18
{
19
    /**
20
     * @Route("/{status}", requirements={"status": "pending|past"})
21
     * @Method("GET")
22
     *
23
     * @return JsonResponse
24
     */
25
    public function listAction(Request $request, $status)
26
    {
27
        $requestForms = $this->get('knp_paginator')
28
            ->paginate(
29
                $this->getDoctrine()->getManager()->getRepository(FormRequest::class)->findBy([
30
                    'user' => $this->getUser(),
31
                    'status' => $status == "pending" ? "pending" : ["approved", "rejected"],
32
                ]),
33
                $request->query->getInt('page', 1),
34
                10
35
            );
36
        return $this->json($requestForms);
37
    }
38
39
    /**
40
     * @Route("/{type}")
41
     * @Method("POST")
42
     *
43
     * @param FormRequestType $type
44
     * @param  Request $request
45
     * @return JsonResponse
46
     */
47
    public function addAction(FormRequestType $type, Request $request)
48
    {
49
        if (!$request->getContent()) {
50
            throw new JsonHttpException(404, 'Request body is empty');
51
        }
52
53
        $data = json_decode($request->getContent());
54
55
        if (!preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/', $data->date)) {
56
            throw new JsonHttpException(404, 'Invalid date format');
57
        }
58
59
        $date = new \DateTime($data->date);
60
        $em = $this->getDoctrine()->getManager();
61
        $formRequest = new FormRequest();
62
        $formRequest->setDate($date)
63
            ->setType($type)
64
            ->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...
65
66
        $em->persist($formRequest);
67
        $em->flush();
68
69
        return $this->json("Request form created", 200);
70
    }
71
}
72