TenantUserController::createActionForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Tahoe\Bundle\MultiTenancyBundle\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Tahoe\Bundle\CrudBundle\Controller\CrudController;
7
use Tahoe\Bundle\MultiTenancyBundle\Form\UserListType;
8
use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantUserInterface;
9
10
/**
11
 * TenantUser controller.
12
 *
13
 */
14
class TenantUserController extends CrudController
15
{
16
    /**
17
     * Lists all Tenant entities.
18
     *
19
     */
20
    public function indexAction()
21
    {
22
        $tenant = $this->container->get('tahoe.multi_tenancy.tenant_resolver')->getTenant();
23
24
        $tenantUsers = $this->repository->findByTenant($tenant->getId());
25
26
        $invitationForm = $this->createForm('invitation_form', null, array(
27
                'method' => 'post',
28
                'action' => $this->generateUrl('invitation_create')
29
            ));
30
31
        $invitations = $this->container->get('invitation_repository')->findAll();
32
33
        $form = $this->createActionForm();
34
35
        return $this->render(
36
            'TahoeMultiTenancyBundle:TenantUser:index.html.twig',
37
            array(
38
                'tenant' => $tenant,
39
                'tenantUsers' => $tenantUsers,
40
                'invitationForm' => $invitationForm->createView(),
41
                'invitations' => $invitations,
42
                'form' => $form->createView()
43
            )
44
        );
45
    }
46
47
    protected function createActionForm()
48
    {
49
        return  $this->createForm(new UserListType(), array(
50
51
            ), array(
52
                'method' => 'post',
53
                'action' => $this->generateUrl('admin_tenant_user_handle_action')
54
            ));
55
    }
56
57
    public function handleActionAction(Request $request)
58
    {
59
        $form = $this->createActionForm();
60
61
        $form->handleRequest($request);
62
63
        if ($form->isValid()) {
64
65
            $data = $form->getData();
66
67
            if (is_array($data['promote'])) {
68
                $this->promoteUsers($data['promote']);
69
            }
70
71
            if (is_array($data['demote'])) {
72
                $this->demoteUsers($data['demote']);
73
            }
74
75
            if (is_array($data['remove'])) {
76
                $this->removeUsers($data['remove']);
77
            }
78
79
            return $this->redirect($this->generateUrl('tenantuser_index'));
80
        }
81
82
        throw new \Exception('Submitted invalid form on tenant user index page');
83
    }
84
85
    /**
86
     * @param array $userIdRole Must follow the pattern userId => Role, array(1=> 'ROLE_ADMIN')
87
     */
88 View Code Duplication
    protected function promoteUsers($userIdRole) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
89
90
        $tenantUserHandler = $this->container->get('tahoe.multi_tenancy.tenant_user_handler');
91
        $userRepository = $this->container->get('user_repository');
92
93
        $tenant = $this->container->get('tahoe.multi_tenancy.tenant_resolver')->getTenant();
94
95
        foreach ($userIdRole as $userId => $role) {
96
97
            $user = $userRepository->find($userId);
98
            $tenantUserHandler->addRoleToUserInTenant($role, $user, $tenant);
99
        }
100
    }
101
102
    /**
103
     * @param array $userIdRole Must follow the pattern userId => Role, array(1=> 'ROLE_ADMIN')
104
     */
105 View Code Duplication
    protected function demoteUsers($userIdRole) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
106
107
        $tenantUserHandler = $this->container->get('tahoe.multi_tenancy.tenant_user_handler');
108
        $userRepository = $this->container->get('user_repository');
109
110
        $tenant = $this->container->get('tahoe.multi_tenancy.tenant_resolver')->getTenant();
111
112
        foreach ($userIdRole as $userId => $role) {
113
114
            $user = $userRepository->find($userId);
115
            $tenantUserHandler->removeRoleFromUserInTenant($role, $user, $tenant);
116
        }
117
    }
118
119
    /**
120
     * @param array $userIdRole Must follow the pattern userId => ignored, array(1=> 'anything')
121
     */
122 View Code Duplication
    protected function removeUsers($userIdRole) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
123
        $tenantUserHandler = $this->container->get('tahoe.multi_tenancy.tenant_user_handler');
124
        $userRepository = $this->container->get('user_repository');
125
126
        $tenant = $this->container->get('tahoe.multi_tenancy.tenant_resolver')->getTenant();
127
128
        foreach ($userIdRole as $userId => $dummy) {
129
130
            $user = $userRepository->find($userId);
131
            $tenantUserHandler->removeUserFromTenant($user, $tenant);
132
        }
133
    }
134
}
135