Completed
Push — EZP-31383 ( 83ce0c )
by
unknown
19:12
created

Mapper   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 244
Duplicated Lines 11.89 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 29
loc 244
c 0
b 0
f 0
rs 10
wmc 30
lcom 0
cbo 6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A mapUsers() 0 9 2
B mapPolicies() 0 41 10
A mapRole() 0 18 5
A mapRoles() 0 14 3
B mapRoleAssignments() 0 45 5
A createCreateStructFromRole() 9 9 1
A createRoleFromCreateStruct() 10 10 1
A mapUser() 0 14 2
A createRoleFromCopyStruct() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the User mapper.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\User;
10
11
use eZ\Publish\SPI\Persistence\User;
12
use eZ\Publish\SPI\Persistence\User\Role;
13
use eZ\Publish\SPI\Persistence\User\RoleCreateStruct;
14
use eZ\Publish\SPI\Persistence\User\Policy;
15
use eZ\Publish\SPI\Persistence\User\RoleAssignment;
16
17
/**
18
 * mapper for User related objects.
19
 */
20
class Mapper
21
{
22
    /**
23
     * Map user data into user object.
24
     *
25
     * @param array $data
26
     *
27
     * @return \eZ\Publish\SPI\Persistence\User
28
     */
29
    public function mapUser(array $data)
30
    {
31
        $user = new User();
32
        $user->id = $data['contentobject_id'];
33
        $user->login = $data['login'];
34
        $user->email = $data['email'];
35
        $user->passwordHash = $data['password_hash'];
36
        $user->hashAlgorithm = (int)$data['password_hash_type'];
37
        $user->passwordUpdatedAt = $data['password_updated_at'] !== null ? (int)$data['password_updated_at'] : null;
38
        $user->isEnabled = (bool)$data['is_enabled'];
39
        $user->maxLogin = $data['max_login'];
40
41
        return $user;
42
    }
43
44
    /**
45
     * Map data for a set of user data.
46
     *
47
     * @param array $data
48
     *
49
     * @return \eZ\Publish\SPI\Persistence\User[]
50
     */
51
    public function mapUsers(array $data)
52
    {
53
        $users = [];
54
        foreach ($data as $row) {
55
            $users[] = $this->mapUser($row);
56
        }
57
58
        return $users;
59
    }
60
61
    /**
62
     * Map policy data to an array of policies.
63
     *
64
     * @param array $data
65
     *
66
     * @return \eZ\Publish\SPI\Persistence\User\Policy
67
     */
68
    public function mapPolicies(array $data)
69
    {
70
        /** @var \eZ\Publish\SPI\Persistence\User\Policy[] */
71
        $policies = [];
72
        foreach ($data as $row) {
73
            $policyId = $row['ezpolicy_id'];
74
            if (!isset($policies[$policyId]) && ($policyId !== null)) {
75
                $originalId = null;
76
                if ($row['ezpolicy_original_id']) {
77
                    $originalId = (int)$row['ezpolicy_original_id'];
78
                } elseif ($row['ezrole_version']) {
79
                    $originalId = (int)$policyId;
80
                }
81
82
                $policies[$policyId] = new Policy(
83
                    [
84
                        'id' => (int)$policyId,
85
                        'roleId' => (int)$row['ezrole_id'],
86
                        'originalId' => $originalId,
87
                        'module' => $row['ezpolicy_module_name'],
88
                        'function' => $row['ezpolicy_function_name'],
89
                        'limitations' => '*', // limitations must be '*' if not a non empty array of limitations
90
                    ]
91
                );
92
            }
93
94
            if (!$row['ezpolicy_limitation_identifier']) {
95
                continue;
96
            } elseif ($policies[$policyId]->limitations === '*') {
97
                $policies[$policyId]->limitations = [];
98
            }
99
100
            if (!isset($policies[$policyId]->limitations[$row['ezpolicy_limitation_identifier']])) {
101
                $policies[$policyId]->limitations[$row['ezpolicy_limitation_identifier']] = [$row['ezpolicy_limitation_value_value']];
102
            } elseif (!in_array($row['ezpolicy_limitation_value_value'], $policies[$policyId]->limitations[$row['ezpolicy_limitation_identifier']])) {
103
                $policies[$policyId]->limitations[$row['ezpolicy_limitation_identifier']][] = $row['ezpolicy_limitation_value_value'];
104
            }
105
        }
106
107
        return array_values($policies);
108
    }
109
110
    /**
111
     * Map role data to a role.
112
     *
113
     * @param array $data
114
     *
115
     * @return \eZ\Publish\SPI\Persistence\User\Role
116
     */
117
    public function mapRole(array $data)
118
    {
119
        $role = new Role();
120
121
        foreach ($data as $row) {
122
            if (empty($role->id)) {
123
                $role->id = (int)$row['ezrole_id'];
124
                $role->identifier = $row['ezrole_name'];
125
                $role->status = $row['ezrole_version'] != 0 ? Role::STATUS_DRAFT : Role::STATUS_DEFINED;
126
                $role->originalId = $row['ezrole_version'] ? (int)$row['ezrole_version'] : Role::STATUS_DEFINED;
127
                // skip name and description as they don't exist in legacy
128
            }
129
        }
130
131
        $role->policies = $this->mapPolicies($data);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->mapPolicies($data) of type array<integer,?> is incompatible with the declared type array<integer,object<eZ\...rsistence\User\Policy>> of property $policies.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
132
133
        return $role;
134
    }
135
136
    /**
137
     * Map data for a set of roles.
138
     *
139
     * @param array $data
140
     *
141
     * @return \eZ\Publish\SPI\Persistence\User\Role[]
142
     */
143
    public function mapRoles(array $data)
144
    {
145
        $roleData = [];
146
        foreach ($data as $row) {
147
            $roleData[$row['ezrole_id']][] = $row;
148
        }
149
150
        $roles = [];
151
        foreach ($roleData as $data) {
152
            $roles[] = $this->mapRole($data);
153
        }
154
155
        return $roles;
156
    }
157
158
    /**
159
     * Map data for a set of role assignments.
160
     *
161
     * @param array $data
162
     *
163
     * @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[]
164
     */
165
    public function mapRoleAssignments(array $data)
166
    {
167
        $roleAssignmentData = [];
168
        foreach ($data as $row) {
169
            $id = (int)$row['id'];
170
            $roleId = (int)$row['role_id'];
171
            $contentId = (int)$row['contentobject_id'];
172
            // if user already have full access to a role, continue
173
            if (isset($roleAssignmentData[$roleId][$contentId])
174
                && $roleAssignmentData[$roleId][$contentId] instanceof RoleAssignment) {
175
                continue;
176
            }
177
178
            $limitIdentifier = $row['limit_identifier'];
179
            if (!empty($limitIdentifier)) {
180
                $roleAssignmentData[$roleId][$contentId][$limitIdentifier][$id] = new RoleAssignment(
181
                    [
182
                        'id' => $id,
183
                        'roleId' => $roleId,
184
                        'contentId' => $contentId,
185
                        'limitationIdentifier' => $limitIdentifier,
186
                        'values' => [$row['limit_value']],
187
                    ]
188
                );
189
            } else {
190
                $roleAssignmentData[$roleId][$contentId] = new RoleAssignment(
191
                    [
192
                        'id' => $id,
193
                        'roleId' => $roleId,
194
                        'contentId' => $contentId,
195
                    ]
196
                );
197
            }
198
        }
199
200
        $roleAssignments = [];
201
        array_walk_recursive(
202
            $roleAssignmentData,
203
            function ($roleAssignment) use (&$roleAssignments) {
204
                $roleAssignments[] = $roleAssignment;
205
            }
206
        );
207
208
        return $roleAssignments;
209
    }
210
211
    /**
212
     * Creates a create struct from an existing $role.
213
     *
214
     * @param \eZ\Publish\SPI\Persistence\User\Role $role
215
     *
216
     * @return \eZ\Publish\SPI\Persistence\User\RoleCreateStruct
217
     */
218 View Code Duplication
    public function createCreateStructFromRole(Role $role)
219
    {
220
        $createStruct = new RoleCreateStruct();
221
222
        $createStruct->identifier = $role->identifier;
223
        $createStruct->policies = $role->policies;
224
225
        return $createStruct;
226
    }
227
228
    /**
229
     * Maps properties from $struct to $role.
230
     *
231
     * @param \eZ\Publish\SPI\Persistence\User\RoleCreateStruct $createStruct
232
     *
233
     * @return \eZ\Publish\SPI\Persistence\User\Role
234
     */
235 View Code Duplication
    public function createRoleFromCreateStruct(RoleCreateStruct $createStruct)
236
    {
237
        $role = new Role();
238
239
        $role->identifier = $createStruct->identifier;
240
        $role->policies = $createStruct->policies;
0 ignored issues
show
Documentation Bug introduced by
It seems like $createStruct->policies of type array<integer,*> is incompatible with the declared type array<integer,object<eZ\...rsistence\User\Policy>> of property $policies.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
241
        $role->status = Role::STATUS_DRAFT;
242
243
        return $role;
244
    }
245
246
    /**
247
     * Maps properties from $struct to $role.
248
     *
249
     * @param \eZ\Publish\SPI\Persistence\User\RoleCopyStruct $copyStruct
250
     *
251
     * @return \eZ\Publish\SPI\Persistence\User\Role
252
     */
253 View Code Duplication
    public function createRoleFromCopyStruct(User\RoleCopyStruct $copyStruct)
254
    {
255
        $role = new Role();
256
257
        $role->identifier = $copyStruct->newIdentifier;
258
        $role->policies = $copyStruct->policies;
0 ignored issues
show
Documentation Bug introduced by
It seems like $copyStruct->policies of type array<integer,*> is incompatible with the declared type array<integer,object<eZ\...rsistence\User\Policy>> of property $policies.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
259
        $role->status = Role::STATUS_DEFINED;
260
261
        return $role;
262
    }
263
}
264