UserController::deleteAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\User;
6
use App\Form\Type\UserEditType;
7
use Dontdrinkandroot\GitkiBundle\Controller\BaseController;
8
use Dontdrinkandroot\GitkiBundle\Service\Security\SecurityService;
9
use Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * @author Philip Washington Sorst <[email protected]>
13
 */
14
class UserController extends BaseController
15
{
16
    /**
17
     * UserController constructor.
18
     */
19
    public function __construct(SecurityService $securityService)
20
    {
21
        parent::__construct($securityService);
22
    }
23
24
    public function listAction()
25
    {
26
        $this->assertAdmin();
27
28
        $users = $this->get('fos_user.user_manager')->findUsers();
29
30
        return $this->render('User/list.html.twig', ['users' => $users]);
31
    }
32
33
    public function editAction(Request $request, $id)
34
    {
35
        $this->assertAdmin();
36
37
        $userManager = $this->get('fos_user.user_manager');
38
        $newUser = false;
39
        if ('new' === $id) {
40
            $newUser = true;
41
            $user = $userManager->createUser();
42
        } else {
43
            $userRepository = $this->get('doctrine')->getRepository(User::class);
44
            $user = $userRepository->find($id);
45
            if (null === $user) {
46
                throw $this->createNotFoundException();
47
            }
48
        }
49
50
        $form = $this->createForm(UserEditType::class, $user);
51
        $form->handleRequest($request);
52
53
        if ($form->isSubmitted() && $form->isValid()) {
54
            $userManager->updateUser($form->getData());
55
56
            if ($newUser) {
57
                return $this->redirectToRoute('ddr_gitki.user.edit', ['id' => $user->getId()]);
58
            }
59
        }
60
61
        return $this->render('User/edit.html.twig', ['form' => $form->createView()]);
62
    }
63
64
    public function deleteAction($id)
65
    {
66
        $this->assertAdmin();
67
68
        $userRepository = $this->get('doctrine')->getRepository(User::class);
69
        /** @var User $user */
70
        $user = $userRepository->find($id);
71
        if (null === $user) {
72
            throw $this->createNotFoundException();
73
        }
74
75
        $userManager = $this->get('fos_user.user_manager');
76
        $userManager->deleteUser($user);
77
78
        return $this->redirectToRoute('ddr_gitki.user.list');
79
    }
80
}
81