AccessRightsController::handleEditForm()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 26
rs 9.7666
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Controller;
4
5
use PiouPiou\RibsAdminBundle\Entity\AccessRight;
6
use PiouPiou\RibsAdminBundle\Service\Globals;
7
use PiouPiou\RibsAdminBundle\Service\ModuleService;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
class AccessRightsController extends AbstractController
15
{
16
    /**
17
     * @Route("/access-rights-management/", name="ribsadmin_access_rights")
18
     * @return Response
19
     */
20
    public function list(): Response
21
    {
22
        $em = $this->getDoctrine()->getManager();
23
        $acces_right = $em->getRepository("RibsAdminBundle:AccessRight")->findAll();
24
25
        return $this->render("@RibsAdmin/access-rights/list.html.twig", [
26
            "access_right" => $acces_right
27
        ]);
28
    }
29
30
    /**
31
     * @Route("/access-rights-management/create/", name="ribsadmin_access_rights_create")
32
     * @Route("/access-rights-management/show/{guid}", name="ribsadmin_access_rights_show")
33
     * @Route("/access-rights-management/edit/{guid}", name="ribsadmin_access_rights_edit")
34
     * @param Request $request
35
     * @param Globals $globals
36
     * @param ModuleService $module
37
     * @param string|null $guid
38
     * @return Response
39
     */
40
    public function edit(Request $request, Globals $globals, ModuleService $module, string $guid = null): Response
41
    {
42
        $em = $this->getDoctrine()->getManager();
43
        $list_rights_user = [];
44
        $disabled_form = strpos($request->get("_route"), "_show") ? true : false;
45
46
        if ($guid === null) {
47
            $access_right = new AccessRight();
48
        } else {
49
            $access_right = $em->getRepository("RibsAdminBundle:AccessRight")->findOneBy(["guid" => $guid]);
50
            $list_rights_user = explode(",", $access_right->getAccessRights());
51
        }
52
53
        $admins = $em->getRepository("RibsAdminBundle:User")->findBy(["admin" => true, "archived" => false]);
54
55
        $form = $this->createForm("PiouPiou\RibsAdminBundle\Form\AccessRight", $access_right, ["disabled" => $disabled_form]);
56
        $form->handleRequest($request);
57
58
        if ($form->isSubmitted() && $form->isValid()) {
59
            return $this->handleEditForm($request, $access_right);
0 ignored issues
show
Bug introduced by
It seems like $access_right can also be of type null; however, parameter $access_right of PiouPiou\RibsAdminBundle...oller::handleEditForm() does only seem to accept PiouPiou\RibsAdminBundle\Entity\AccessRight, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            return $this->handleEditForm($request, /** @scrutinizer ignore-type */ $access_right);
Loading history...
60
        }
61
62
        return $this->render("@RibsAdmin/access-rights/edit.html.twig", [
63
            "access_right" => $access_right,
64
            "form" => $form->createView(),
65
            "form_errors" => $form->getErrors(),
66
            "list_rights_user" => $list_rights_user,
67
            "admins" => $admins,
68
            "ribs_admin_rights" => json_decode(file_get_contents($globals->getBaseBundlePath() . "/Resources/json/ribsadmin_rights.json")),
69
            "modules" => $module->getAllInfosModules(),
70
            "disabled_form" => $disabled_form
71
        ]);
72
    }
73
74
    /**
75
     * @Route("/access-rights-management/delete/{guid}", name="ribsadmin_access_rights_delete")
76
     * @param string $guid
77
     * @return RedirectResponse function that delete an access right list
78
     */
79
    public function delete(string $guid): RedirectResponse
80
    {
81
        $em = $this->getDoctrine()->getManager();
82
        $list = $em->getRepository("RibsAdminBundle:AccessRight")->findOneBy(["guid" => $guid]);
83
84
        if ($list) {
85
            foreach ($list->getUsers() as $user) {
86
                $user->setAccessRightList(null);
87
            }
88
89
            $em->remove($list);
90
            $em->flush();
91
92
            $this->addFlash("success-flash", "The right list was deleted");
93
        } else {
94
            $this->addFlash("error-flash", "The right list wasn't found");
95
        }
96
97
        return $this->redirectToRoute("ribsadmin_access_rights");
98
    }
99
100
    /**
101
     * @param Request $request
102
     * @param AccessRight $access_right
103
     * @return RedirectResponse function that handle the form request
104
     */
105
    private function handleEditForm(Request $request, AccessRight $access_right): RedirectResponse
106
    {
107
        $em = $this->getDoctrine()->getManager();
108
109
        if ($request->get("right") === null) {
110
            $rights = "";
111
        } else {
112
            $rights = implode(",", $request->get("right"));
0 ignored issues
show
Bug introduced by
It seems like $request->get('right') can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            $rights = implode(",", /** @scrutinizer ignore-type */ $request->get("right"));
Loading history...
113
        }
114
115
        $access_right->setAccessRights($rights);
116
        $em->persist($access_right);
117
        $em->flush();
118
119
        $em->getRepository("RibsAdminBundle:AccessRight")->deleteAllUsersList($access_right);
0 ignored issues
show
Bug introduced by
The method deleteAllUsersList() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Doctrine\ORM\EntityRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
        $em->getRepository("RibsAdminBundle:AccessRight")->/** @scrutinizer ignore-call */ deleteAllUsersList($access_right);
Loading history...
120
        $admins = $request->get("admins");
121
122
        if ($admins !== null) {
123
            foreach ($admins as $admin) {
124
                $em->getRepository("RibsAdminBundle:AccessRight")->setAccessRightListUser($access_right->getId(), $admin);
0 ignored issues
show
Bug introduced by
The method setAccessRightListUser() does not exist on Doctrine\Persistence\ObjectRepository. It seems like you code against a sub-type of Doctrine\Persistence\ObjectRepository such as Doctrine\ORM\EntityRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
                $em->getRepository("RibsAdminBundle:AccessRight")->/** @scrutinizer ignore-call */ setAccessRightListUser($access_right->getId(), $admin);
Loading history...
125
            }
126
        }
127
128
        $this->addFlash("success-flash", "The right list was correctly edited");
129
130
        return $this->redirectToRoute("ribsadmin_access_rights");
131
    }
132
}
133