Completed
Push — master ( 6ceb07...e22522 )
by Sergei
16:21
created

Controller/GroupsController.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Modera\BackendSecurityBundle\Controller;
4
5
use Modera\BackendSecurityBundle\ModeraBackendSecurityBundle;
6
use Modera\SecurityBundle\Entity\Group;
7
use Modera\ServerCrudBundle\Controller\AbstractCrudController;
8
use Modera\FoundationBundle\Translation\T;
9
use Modera\ServerCrudBundle\DataMapping\DataMapperInterface;
10
use Modera\ServerCrudBundle\NewValuesFactory\NewValuesFactoryInterface;
11
use Modera\ServerCrudBundle\Validation\DefaultEntityValidator;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * @author    Sergei Lissovski <[email protected]>
16
 * @copyright 2014 Modera Foundation
17
 */
18
class GroupsController extends AbstractCrudController
19
{
20
    /**
21
     * @return array
22
     */
23
    public function getConfig()
24
    {
25
        $em = $this->getDoctrine();
26
27
        $groupEntityValidator = function (array $params, Group $group, DefaultEntityValidator $defaultValidator, array $config, ContainerInterface $container) use ($em) {
0 ignored issues
show
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
29
            $validationResult = $defaultValidator->validate($group, $config);
30
31
            if (!$group->getRefName()) {
32
                return $validationResult;
33
            }
34
35
            /** @var Group[] $groupWithSuchRefNameList */
36
            $groupWithSuchRefNameList = $em->getRepository(Group::clazz())->findByRefName($group->getRefName());
37
38
            if (count($groupWithSuchRefNameList) > 0) {
39
                $groupWithSuchRefName = $groupWithSuchRefNameList[0];
40
41
                if ($groupWithSuchRefName->getId() != $group->getId()) {
42
                    $validationResult->addFieldError(
43
                        'refName',
44
                        T::trans(
45
                            'This refName is taken. Consider use \'%groupName%\' group or change current reference name.',
46
                            array('%groupName%' => $groupWithSuchRefName->getName())
47
                        )
48
                    );
49
                }
50
            }
51
52
            return $validationResult;
53
54
        };
55
56
        $mapEntity = function (array $params, Group $group, DataMapperInterface $defaultMapper, ContainerInterface $container) {
57
            $defaultMapper->mapData($params, $group);
58
59
            /*
60
             * Because of unique constrain we cannot save '' value as refName.
61
             * Only one time can, actually. :) So, to allow user use groups without
62
             * refName we have to set null by force because of ExtJs empty form value
63
             * is ''.
64
             */
65
            $refName = $group->getRefName();
66
            if ($refName === '') {
67
                $group->setRefName(null);
68
            } else {
69
                /*
70
                 * To help users avoid duplicates group we use normalizing for refName
71
                 */
72
                $group->setRefName(Group::normalizeRefNameString($refName));
73
            }
74
75
        };
76
77
        return array(
78
            'entity' => Group::clazz(),
79
            'security' => array(
80
                'role' => ModeraBackendSecurityBundle::ROLE_ACCESS_BACKEND_TOOLS_SECURITY_SECTION,
81
                'actions' => array(
82
                    'create' => ModeraBackendSecurityBundle::ROLE_MANAGE_PERMISSIONS,
83
                    'update' => ModeraBackendSecurityBundle::ROLE_MANAGE_PERMISSIONS,
84
                    'remove' => ModeraBackendSecurityBundle::ROLE_MANAGE_PERMISSIONS,
85
                ),
86
            ),
87
            'hydration' => array(
88
                'groups' => array(
89
                    'list' => function (Group $group) {
90
                            return array(
91
                                'id' => $group->getId(),
92
                                'name' => $group->getName(),
93
                                'usersCount' => count($group->getUsers()),
94
                            );
95
                        },
96
                    'delete-group' => ['name'],
97
                    'main-form' => ['id', 'name', 'refName'],
98
                    'compact-list' => ['id', 'name'],
99
                ),
100
                'profiles' => array(
101
                    'list', 'compact-list',
102
                    'delete-group',
103
                    'edit-group' => array('main-form'),
104
                ),
105
            ),
106
            'format_new_entity_values' => function (array $params, array $config, NewValuesFactoryInterface $defaultImpl, ContainerInterface $container) {
107
                return array(
108
                    'refName' => null,
109
                );
110
            },
111
            'new_entity_validator' => $groupEntityValidator,
112
            'updated_entity_validator' => $groupEntityValidator,
113
            'map_data_on_create' => $mapEntity,
114
            'map_data_on_update' => $mapEntity,
115
        );
116
    }
117
}
118