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) |
|
|
|
|
79
|
|
|
->setUser($this->getUser()); |
|
|
|
|
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
|
|
|
|
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: