Completed
Pull Request — master (#30)
by
unknown
03:43
created

UserController::addAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 8
Ratio 34.78 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 23
ccs 15
cts 15
cp 1
rs 9.0856
cc 3
eloc 15
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\ActivationType;
10
use Symfony\Component\Form\Extension\Core\Type\TextType;
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/User/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, [
34
                "attr" => ["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', ['id' => $user->getId()]),
47 3
                'validation_groups' => '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
     *
62
     * @Method("PUT")
63
     * @param  Request $request
64
     * @param  User $user
65
     * @return RedirectResponse
66
     */
67 1
    public function activationAction(Request $request, User $user)
68
    {
69 1
        $em = $this->getDoctrine()->getManager();
70 1
        $form = $this->createForm(ActivationType::class, $user, [
71 1
            'method' => "PUT",
72 1
            'action' => $this->generateUrl('activate_user', ['id' => $user->getId()]),
73 1
            'validation_groups' => 'edit',
74
        ]);
75 1
        $form->handleRequest($request);
76 1
        if ($form->isValid()) {
77 1
            $em->persist($user);
78 1
            $em->flush();
79
        }
80
81 1
        return $this->redirect($this->generateUrl("users_list"));
82
    }
83
84
    /**
85
     * @Route("/user/add", name="add_user")
86
     * @Template("@App/User/add.html.twig")
87
     *
88
     * @param Request $request
89
     * @return array|RedirectResponse
90
     */
91 1
    public function addAction(Request $request)
92
    {
93 1
        $em = $this->getDoctrine()->getManager();
94 1
        $user = new User();
95 1
        $form = $this->createForm(EditType::class, $user, [
96 1
            'action' => $this->generateUrl('add_user'),
97 1
            'validation_groups' => 'registration',
98
        ])
99 1
            ->add('Register', SubmitType::class, [
100
                'attr' => ['class' => 'btn btn-primary']
101 1
            ]);
102 1
        $form->handleRequest($request);
103 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...
104 1
            if ($form->isValid()) {
105 1
                $em->persist($user);
106 1
                $em->flush();
107
108 1
                return $this->redirect($this->generateUrl("users_list"));
109
            }
110
        }
111
112 1
        return ['form' => $form->createView()];
113
    }
114
115
    /**
116
     * @Route("/user/edit/{id}", name="edit_user")
117
     * @Template("@App/User/add.html.twig")
118
     *
119
     * @param Request $request
120
     * @param User $user
121
     * @return array|RedirectResponse
122
     */
123 1
    public function editAction(Request $request, User $user)
124
    {
125 1
        $em = $this->getDoctrine()->getManager();
126 1
        $form = $this->createForm(EditType::class, $user, [
127 1
            'action' => $this->generateUrl('edit_user', ['id' => $user->getId()]),
128 1
            'method' => 'POST',
129 1
            'validation_groups' => 'edit',
130
        ])
131 1
            ->add('image', TextType::class, [
132
                'attr' => [
133
                    'placeholder' => 'image',
134
                    'class' => 'form-control'
135 1
                ],
136
                'label' => false,
137
                'required' => false,
138
            ])
139 1
            ->add('Save', SubmitType::class, [
140
                'attr' => ['class' => 'btn btn-primary']
141 1
            ]);
142 1
        $form->handleRequest($request);
143 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...
144 1
            if ($form->isValid()) {
145 1
                $em->persist($user);
146 1
                $em->flush();
147
148 1
                return $this->redirect($this->generateUrl("users_list"));
149
            }
150
        }
151
152 1
        return ['form' => $form->createView()];
153
    }
154
}
155