Passed
Push — develop ( 27b04e...f074c1 )
by
unknown
02:28
created

AccessInitializer::initialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 74
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 9.0335
c 0
b 0
f 0
cc 3
eloc 48
nc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AppBundle\Tenant\Initializer;
4
5
use Ds\Component\Security\Model\Permission;
6
use Ds\Component\Security\Service\AccessService;
7
use Ds\Component\Security\Service\PermissionService;
8
use Ds\Component\Tenant\Initializer\Initializer;
9
10
/**
11
 * Class AccessInitializer
12
 */
13
class AccessInitializer implements Initializer
14
{
15
    /**
16
     * @var \Ds\Component\Security\Service\AccessService
17
     */
18
    protected $accessService;
19
20
    /**
21
     * @var \Ds\Component\Security\Service\PermissionService
22
     */
23
    protected $permissionService;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param \Ds\Component\Security\Service\AccessService $accessService
29
     * @param \Ds\Component\Security\Service\PermissionService $permissionService
30
     */
31
    public function __construct(AccessService $accessService, PermissionService $permissionService)
32
    {
33
        $this->accessService = $accessService;
34
        $this->permissionService = $permissionService;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function initialize(array $data)
41
    {
42
        $items = [
43
            [
44
                'owner' => 'System',
45
                'owner_uuid' => $data['identity']['system']['uuid'],
46
                'assignee' => 'System',
47
                'assignee_uuid' => $data['identity']['system']['uuid'],
48
                'permissions' => [
49
                    [
50
                        'scope' => Permission::GENERIC,
51
                        'key' => 'entity',
52
                        'attributes' => [Permission::BROWSE, Permission::READ, Permission::EDIT, Permission::ADD, Permission::DELETE]
53
                    ],
54
                    [
55
                        'scope' => Permission::GENERIC,
56
                        'key' => 'property',
57
                        'attributes' => [Permission::BROWSE, Permission::READ, Permission::EDIT]
58
                    ],
59
                    [
60
                        'scope' => Permission::GENERIC,
61
                        'key' => 'generic',
62
                        'attributes' => [Permission::BROWSE, Permission::READ, Permission::EDIT, Permission::ADD, Permission::DELETE, Permission::EXECUTE]
63
                    ]
64
                ]
65
            ],
66
            [
67
                'owner' => 'BusinessUnit',
68
                'owner_uuid' => $data['business_unit']['administration']['uuid'],
69
                'assignee' => 'Staff',
70
                'assignee_uuid' => $data['identity']['admin']['uuid'],
71
                'permissions' => [
72
                    [
73
                        'scope' => Permission::GENERIC,
74
                        'key' => 'entity',
75
                        'attributes' => [Permission::BROWSE, Permission::READ, Permission::EDIT, Permission::ADD, Permission::DELETE]
76
                    ],
77
                    [
78
                        'scope' => Permission::GENERIC,
79
                        'key' => 'property',
80
                        'attributes' => [Permission::BROWSE, Permission::READ, Permission::EDIT]
81
                    ],
82
                    [
83
                        'scope' => Permission::GENERIC,
84
                        'key' => 'generic',
85
                        'attributes' => [Permission::BROWSE, Permission::READ, Permission::EDIT, Permission::ADD, Permission::DELETE, Permission::EXECUTE]
86
                    ]
87
                ]
88
            ]
89
        ];
90
91
        $manager = $this->accessService->getManager();
92
93
        foreach ($items as $item) {
94
            $access = $this->accessService->createInstance();
95
            $access
96
                ->setOwner($item['owner'])
97
                ->setOwnerUuid($item['owner_uuid'])
98
                ->setAssignee($item['assignee'])
99
                ->setAssigneeUuid($item['assignee_uuid'])
100
                ->setTenant($data['tenant']['uuid']);
101
102
            foreach ($item['permissions'] as $subItem) {
103
                $permission = $this->permissionService->createInstance();
104
                $permission
105
                    ->setScope($subItem['scope'])
106
                    ->setKey($subItem['key'])
107
                    ->setAttributes($subItem['attributes'])
108
                    ->setTenant($data['tenant']['uuid']);
109
                $access->addPermission($permission);
110
            }
111
112
            $manager->persist($access);
113
            $manager->flush();
114
        }
115
    }
116
}
117