Completed
Pull Request — master (#30)
by nonanerz
05:26
created

FormRequestController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 60
rs 10
c 1
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A listAction() 0 13 2
B addAction() 0 29 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}", requirements={"type"= "sick-day|personal-day|overnight-guest"})
41
     * @Method("POST")
42
     *
43
     * @param $type
44
     * @param  Request $request
45
     * @return JsonResponse
46
     */
47
    public function addAction($type, Request $request)
48
    {
49
        if (!$request->getContent()) {
50
            throw new JsonHttpException(404, 'Request body is empty');
51
        }
52
        $em = $this->getDoctrine()->getManager();
53
54
        $formRequest = new FormRequest();
55
        $formRequest
56
            ->setType(str_replace('-', " ", $type))
57
            ->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...
58
59
        $formRequest = $this->get('serializer')->deserialize(
60
            $request->getContent(),
61
            FormRequest::class,
62
            'json',
63
            ['object_to_populate' => $formRequest]
64
        );
65
66
        $em->persist($formRequest);
67
        $em->flush();
68
69
        $this->get('app.email_notification')->sendNotification(
70
            $formRequest->getUser()->getEmail(),
71
            "Form request",
72
            "Hello, ".$formRequest->getUser()->getFirstName().". Your form request was created."
73
        );
74
        return $this->json("Request form created", 200);
75
    }
76
}
77