Passed
Push — master ( 83de64...44197b )
by Anthony
02:43
created

AccountsController::archive()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 23
rs 9.7998
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Controller;
4
5
use PiouPiou\RibsAdminBundle\Entity\Account;
6
use PiouPiou\RibsAdminBundle\Entity\User;
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
class AccountsController extends AbstractController
14
{
15
    /**
16
     * @Route("/accounts/", name="ribsadmin_accounts")
17
     * @return Response
18
     */
19
    public function list(): Response
20
    {
21
        $em = $this->getDoctrine()->getManager();
22
        $current_account = $this->getUser()->getUser();
23
24
        $users = $em->getRepository("RibsAdminBundle:Account")->findAllUserArchived($current_account);
25
        $users_archived = $em->getRepository("RibsAdminBundle:Account")->findAllUserArchived($current_account, true);
26
27
        return $this->render('@RibsAdmin/accounts/list.html.twig', [
28
            "users" => $users,
29
            "users_archived" => $users_archived
30
        ]);
31
    }
32
33
    /**
34
     * @Route("/accounts/create/", name="ribsadmin_accounts_create")
35
     * @Route("/accounts/edit/{guid}", name="ribsadmin_accounts_edit")
36
     * @param Request $request
37
     * @param string|null $guid
38
     * @return Response
39
     */
40
    public function edit(Request $request, string $guid = null): Response
41
    {
42
        $em = $this->getDoctrine()->getManager();
43
44
        if ($guid === null) {
45
            $account = new Account();
46
            $old_password = null;
47
            $user = null;
48
        } else {
49
            $user = $em->getRepository(User::class)->findOneBy(["guid" => $guid]);
50
            $account = $em->getRepository(Account::class)->findOneBy(["user" => $user->getId()]);
51
            $old_password = $account->getPassword();
52
        }
53
54
        $form = $this->createForm("PiouPiou\RibsAdminBundle\Form\Account", $account);
55
56
        $form->handleRequest($request);
57
58
        if ($form->isSubmitted() && $form->isValid()) {
59
            /**
60
             * @var Account
61
             */
62
            $data = $form->getData();
63
64
            $account_exist = $em->getRepository(Account::class)->findOneBy(["username" => $data->getUsername()]);
65
66
            if ($account_exist && $account_exist === $account) {
67
                $account_exist = null;
68
            }
69
70
            if (!$account_exist) {
71
                if ($guid === null) {
72
                    $temp_password = $this->get("security.password_encoder")->encodePassword($data, $form->get("password")->getData());
0 ignored issues
show
Bug introduced by
The method get() does not exist on PiouPiou\RibsAdminBundle...ller\AccountsController. ( Ignorable by Annotation )

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

72
                    $temp_password = $this->/** @scrutinizer ignore-call */ get("security.password_encoder")->encodePassword($data, $form->get("password")->getData());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
                    $data->setPassword($temp_password);
74
                } else if ($form->get("password")->getData()) {
75
                    $temp_password = $this->get("security.password_encoder")->encodePassword($data, $form->get("password")->getData());
76
                    $data->setPassword($temp_password);
77
                } else {
78
                    $data->setPassword($old_password);
79
                }
80
81
                $em->persist($data);
82
                $em->flush();
83
84
                $username = $data->getUser()->getFirstName() . " " . $data->getUser()->getLastName();
85
86
                if ($guid === null) {
87
                    $this->addFlash("success-flash", "the account of " . $username . " was created");
88
                } else {
89
                    $this->addFlash("success-flash", "the account of " . $username . " was edited");
90
                }
91
92
                return $this->redirectToRoute("ribsadmin_accounts");
93
            } else {
94
                $this->addFlash("error-flash", "An account with username " . $data->getUsername() . " already exist");
95
                return $this->redirectToRoute($request->get("_route"), ["guid" => $guid]);
96
            }
97
        }
98
99
        return $this->render("@RibsAdmin/accounts/edit.html.twig", [
100
            "form" => $form->createView(),
101
            "form_errors" => $form->getErrors(),
102
            "user" => $user
103
        ]);
104
    }
105
106
    /**
107
     * method to disable or enable a user
108
     * @Route("/accounts/archive/{guid}/{activate}", name="ribsadmin_accounts_archive")
109
     * @param string $guid
110
     * @param bool $activate
111
     * @return RedirectResponse
112
     */
113
    public function archive(string $guid, bool $activate = false): RedirectResponse
114
    {
115
        $em = $this->getDoctrine()->getManager();
116
117
        $user = $em->getRepository("RibsAdminBundle:User")->findOneBy(["guid" => $guid]);
118
119
        if ($user) {
120
            if ($activate === true) {
121
                $user->setArchived(false);
122
                $word = "activated";
123
            } else {
124
                $user->setArchived(true);
125
                $word = "disabled";
126
            }
127
128
            $em->persist($user);
129
            $em->flush();
130
131
            $this->addFlash("success-flash", "The user " . $user->getFirstname() . " " . $user->getLastname() .
132
                " was " . $word . " sucessfuly");
133
        }
134
135
        return $this->redirectToRoute("ribsadmin_accounts");
136
    }
137
}
138