Completed
Pull Request — dev (#22)
by
unknown
03:39
created

UserController::addAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 8
Ratio 40 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 20
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 13
nc 3
nop 1
crap 3
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\User;
6
use AppBundle\Entity\DTO\Filter;
7
use AppBundle\Form\FilterType;
8
use AppBundle\Form\User\EditType;
9
use AppBundle\Form\User\RegistrationType;
10
use AppBundle\Form\User\ActivationType;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class UserController extends Controller
20
{
21
    /**
22
     * @Route("/users", name="users_list")
23
     * @Template("@App/users.html.twig")
24
     *
25
     * @param Request $request
26
     * @return array
27
     */
28 3
    public function listAction(Request $request)
29
    {
30 3
        $em = $this->getDoctrine()->getManager();
31 3
        $filter = new Filter;
32 3
        $filterForm = $this->createForm(FilterType::class, $filter)
33 3
            ->add("Search", SubmitType::class, array(
34
                "attr" => array("class" => "fa fa-search")
35 3
            ));
36 3
        $filterForm->handleRequest($request);
37 3
        $users = $this->get('knp_paginator')->paginate(
38 3
            $em->getRepository(User::class)->selectUsersByParams($filter),
39 3
            $request->query->getInt('page', 1),
40 3
            10
41
        );
42 3
        $activationForm = [];
43 3
        foreach ($users as $user) {
44 3
            $activationForm[$user->getId()] = $this->createForm(ActivationType::class, $user, [
45 3
                'method' => "PUT",
46 3
                'action' => $this->generateUrl('activate_user', array('id' => $user->getId())),
47
                'validation_groups' => array('edit'),
48
            ])
49 3
                ->createView();
50
        }
51
52
        return [
53 3
            "users" => $users,
54 3
            "filterForm" => $filterForm->createView(),
55 3
            "activationForm" => $activationForm
56
        ];
57
    }
58
59
    /**
60
     * @Route("/user/activate/{id}", name="activate_user")
61
     * @Template("@App/add.html.twig")
62
     *
63
     * @Method("PUT")
64
     * @param  Request $request
65
     * @param  User $user
66
     * @return RedirectResponse
67
     */
68 1
    public function activationAction(Request $request, User $user)
69
    {
70 1
        $em = $this->getDoctrine()->getManager();
71 1
        $form = $this->createForm(ActivationType::class, $user, [
72 1
            'method' => "PUT",
73 1
            'action' => $this->generateUrl('activate_user', array('id' => $user->getId())),
74
            'validation_groups' => array('edit'),
75
        ]);
76 1
        $form->handleRequest($request);
77 1
        if ($form->isValid()) {
78 1
            $em->persist($user);
79 1
            $em->flush();
80
        }
81
82 1
        return $this->redirect($this->generateUrl("users_list"));
83
    }
84
85
    /**
86
     * @Route("/user/add", name="add_user")
87
     * @Template("@App/add.html.twig")
88
     *
89
     * @param Request $request
90
     * @return array|RedirectResponse
91
     */
92 1
    public function addAction(Request $request)
93
    {
94 1
        $em = $this->getDoctrine()->getManager();
95 1
        $user = new User();
96 1
        $form = $this->createForm(RegistrationType::class, $user, [
97 1
            'action' => $this->generateUrl('add_user'),
98
            'validation_groups' => array('registration'),
99
        ]);
100 1
        $form->handleRequest($request);
101 1 View Code Duplication
        if ($form->isSubmitted()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102 1
            if ($form->isValid()) {
103 1
                $em->persist($user);
104 1
                $em->flush();
105
106 1
                return $this->redirect($this->generateUrl("users_list"));
107
            }
108
        }
109
110 1
        return ['form' => $form->createView()];
111
    }
112
113
    /**
114
     * @Route("/user/edit/{id}", name="edit_user")
115
     * @Template("@App/add.html.twig")
116
     *
117
     * @param Request $request
118
     * @param User $user
119
     * @return array|RedirectResponse
120
     */
121 1
    public function editAction(Request $request, User $user)
122
    {
123 1
        $em = $this->getDoctrine()->getManager();
124 1
        $form = $this->createForm(EditType::class, $user, [
125 1
            'action' => $this->generateUrl('edit_user', array('id' => $user->getId())),
126 1
            'method' => 'POST',
127
            'validation_groups' => array('edit'),
128
        ]);
129 1
        $form->handleRequest($request);
130 1 View Code Duplication
        if ($form->isSubmitted()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131 1
            if ($form->isValid()) {
132 1
                $em->persist($user);
133 1
                $em->flush();
134
135 1
                return $this->redirect($this->generateUrl("users_list"));
136
            }
137
        }
138
139 1
        return ['form' => $form->createView()];
140
    }
141
}
142