1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Transfer. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE file located |
7
|
|
|
* in the root directory. |
8
|
|
|
*/ |
9
|
|
|
namespace Transfer\EzPlatform\Repository\Values\Mapper; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Values\User\User; |
12
|
|
|
use eZ\Publish\API\Repository\Values\User\UserCreateStruct; |
13
|
|
|
use eZ\Publish\API\Repository\Values\User\UserUpdateStruct; |
14
|
|
|
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct; |
15
|
|
|
use Transfer\EzPlatform\Repository\Values\UserObject; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* User mapper. |
19
|
|
|
* |
20
|
|
|
* @author Harald Tollefsen <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class UserMapper |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var UserObject |
26
|
|
|
*/ |
27
|
|
|
public $userObject; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param UserObject $userObject |
31
|
|
|
*/ |
32
|
8 |
|
public function __construct(UserObject $userObject) |
33
|
|
|
{ |
34
|
8 |
|
$this->userObject = $userObject; |
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param User $user |
39
|
|
|
*/ |
40
|
1 |
|
public function userToObject(User $user) |
41
|
|
|
{ |
42
|
1 |
|
$this->userObject->data['username'] = $user->login; |
43
|
1 |
|
$this->userObject->data['email'] = $user->email; |
44
|
1 |
|
$this->userObject->data['enabled'] = $user->enabled; |
45
|
1 |
|
$this->userObject->data['max_login'] = $user->maxLogin; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param UserCreateStruct $createStruct |
50
|
|
|
*/ |
51
|
3 |
|
public function mapObjectToCreateStruct(UserCreateStruct $createStruct) |
52
|
|
|
{ |
53
|
|
|
// Name collection (ez => transfer) |
54
|
|
|
$keys = array( |
55
|
3 |
|
'enabled' => 'enabled', |
56
|
3 |
|
); |
57
|
|
|
|
58
|
3 |
|
$this->arrayToStruct($createStruct, $keys); |
59
|
|
|
|
60
|
3 |
|
$this->assignStructFieldValues($createStruct); |
61
|
|
|
|
62
|
3 |
|
$this->callStruct($createStruct); |
63
|
3 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param UserUpdateStruct $updateStruct |
67
|
|
|
*/ |
68
|
7 |
|
public function mapObjectToUpdateStruct(UserUpdateStruct $updateStruct) |
69
|
|
|
{ |
70
|
7 |
|
$updateStruct->contentUpdateStruct = new ContentUpdateStruct(); |
71
|
|
|
|
72
|
|
|
// Name collection (ez => transfer) |
73
|
|
|
$keys = array( |
74
|
7 |
|
'email' => 'email', |
75
|
7 |
|
'maxLogin' => 'max_login', |
76
|
7 |
|
'enabled' => 'enabled', |
77
|
7 |
|
); |
78
|
|
|
|
79
|
7 |
|
$this->arrayToStruct($updateStruct, $keys); |
80
|
|
|
|
81
|
7 |
|
$this->assignStructFieldValues($updateStruct); |
82
|
|
|
|
83
|
7 |
|
$this->callStruct($updateStruct); |
84
|
7 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param UserCreateStruct|UserUpdateStruct $struct |
88
|
|
|
* @param array $keys |
89
|
|
|
*/ |
90
|
8 |
View Code Duplication |
private function arrayToStruct($struct, $keys) |
|
|
|
|
91
|
|
|
{ |
92
|
8 |
|
foreach ($keys as $ezKey => $transferKey) { |
93
|
8 |
|
if (isset($this->userObject->data[$transferKey])) { |
94
|
8 |
|
$struct->$ezKey = $this->userObject->data[$transferKey]; |
95
|
8 |
|
} |
96
|
8 |
|
} |
97
|
8 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param UserCreateStruct|UserUpdateStruct $struct |
101
|
|
|
*/ |
102
|
8 |
View Code Duplication |
private function assignStructFieldValues($struct) |
|
|
|
|
103
|
|
|
{ |
104
|
8 |
|
foreach ($this->userObject->data['fields'] as $key => $value) { |
105
|
8 |
|
if ($struct instanceof UserUpdateStruct) { |
106
|
7 |
|
$struct->contentUpdateStruct->setField($key, $value); |
107
|
7 |
|
} else { |
108
|
3 |
|
$struct->setField($key, $value); |
109
|
|
|
} |
110
|
8 |
|
} |
111
|
8 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param UserCreateStruct|UserUpdateStruct $struct |
115
|
|
|
*/ |
116
|
8 |
|
private function callStruct($struct) |
117
|
|
|
{ |
118
|
8 |
|
if ($this->userObject->getProperty('struct_callback')) { |
119
|
1 |
|
$callback = $this->userObject->getProperty('struct_callback'); |
120
|
1 |
|
$callback($struct); |
121
|
1 |
|
} |
122
|
8 |
|
} |
123
|
|
|
} |
124
|
|
|
|
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.