Completed
Pull Request — dev (#40)
by
unknown
11:22
created

FormRequestController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 6
dl 0
loc 74
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A listAction() 0 13 2
B addAction() 0 43 6
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 FormRequestType $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
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
        $em = $this->getDoctrine()->getManager();
60
61
        switch ($type){
62
            case 'sick-day':
63
                $formRequestType = $em->getRepository(FormRequestType::class)
64
                    ->findOneBy(['name' => 'Sick Day']);
65
                break;
66
            case 'personal-day':
67
                $formRequestType = $em->getRepository(FormRequestType::class)
68
                    ->findOneBy(['name' => 'Personal Day']);
69
                break;
70
            case 'overnight-guest':
71
                $formRequestType = $em->getRepository(FormRequestType::class)
72
                    ->findOneBy(['name' => 'Overnight Guest']);
73
                break;
74
        }
75
        $date = new \DateTime($data->date);
76
        $formRequest = new FormRequest();
77
        $formRequest->setDate($date)
78
            ->setType($formRequestType)
0 ignored issues
show
Bug introduced by
The variable $formRequestType does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
79
            ->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...
80
        $em->persist($formRequest);
81
        $em->flush();
82
83
        $this->get('app.email_notification')->sendNotification(
84
            $formRequest->getUser()->getEmail(),
85
            "Form request",
86
            "Hello, ".$formRequest->getUser()->getFirstName().". Your form request was created."
87
        );
88
        return $this->json("Request form created", 200);
89
    }
90
}
91