ModuleController::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.9332
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Controller;
4
5
use PiouPiou\RibsAdminBundle\Entity\Module;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
12
class ModuleController extends AbstractController
13
{
14
    /**
15
     * @Route("/modules/", name="ribsadmin_modules")
16
     * @return Response
17
     */
18
    public function list(): Response
19
    {
20
        $em = $this->getDoctrine()->getManager();
21
22
        $modules = $em->getRepository(Module::class)->findBy([], ['titleTag' => 'ASC']);
23
24
        return $this->render('@RibsAdmin/modules/list.html.twig', [
25
            "modules" => $modules,
26
        ]);
27
    }
28
29
    /**
30
     * @Route("/modules/create/", name="ribsadmin_modules_create")
31
     * @Route("/modules/edit/{id}", name="ribsadmin_modules_edit")
32
     * @param Request $request
33
     * @param int|null $id
34
     * @return Response
35
     */
36
    public function edit(Request $request, int $id = null): Response
37
    {
38
        $em = $this->getDoctrine()->getManager();
39
40
        if (!$id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
41
            $module = new Module();
42
            $text = "created";
43
        } else {
44
            $module = $em->getRepository(Module::class)->findOneBy(["id" => $id]);
45
            $text = "edited";
46
        }
47
48
        $form = $this->createForm(\PiouPiou\RibsAdminBundle\Form\Module::class, $module);
49
        $form->handleRequest($request);
50
51
        if ($form->isSubmitted() && $form->isValid()) {
52
            /** @var Module $data */
53
            $data = $form->getData();
54
            $em->persist($data);
55
            $em->flush();
56
            $this->addFlash("success-flash", "Module " . $data->getTitleTag() . " was " . $text);
57
58
            return $this->redirectToRoute("ribsadmin_modules");
59
        }
60
61
        return $this->render("@RibsAdmin/modules/edit.html.twig", [
62
            "form" => $form->createView(),
63
            "form_errors" => $form->getErrors(),
64
            "module" => $module
65
        ]);
66
    }
67
68
    /**
69
     * @Route("/modules/delete/{id}", name="ribsadmin_modules_delete")
70
     * @param int $id
71
     * @return RedirectResponse
72
     */
73
    public function delete(int $id): RedirectResponse
74
    {
75
        $em = $this->getDoctrine()->getManager();
76
        $module = $em->getRepository(Module::class)->findOneBy(["id" => $id]);
77
78
        if ($module) {
79
            $name = $module->getTitle();
80
            $em->remove($module);
81
            $em->flush();
82
            $this->addFlash("success-flash", "Module " . $name . " was deleted");
83
        } else {
84
            $this->addFlash("error-flash", "An error occured, module doesn't found");
85
        }
86
87
        return $this->redirectToRoute("ribsadmin_modules");
88
    }
89
}
90