1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the UserUpdate parser class. |
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\REST\Server\Input\Parser; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\REST\Common\Input\BaseParser; |
12
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher; |
13
|
|
|
use eZ\Publish\Core\REST\Common\Input\FieldTypeParser; |
14
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParserTools; |
15
|
|
|
use eZ\Publish\Core\REST\Common\Exceptions; |
16
|
|
|
use eZ\Publish\Core\REST\Server\Values\RestUserUpdateStruct; |
17
|
|
|
use eZ\Publish\API\Repository\UserService; |
18
|
|
|
use eZ\Publish\API\Repository\ContentService; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Parser for UserUpdate. |
22
|
|
|
*/ |
23
|
|
|
class UserUpdate extends BaseParser |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* User service. |
27
|
|
|
* |
28
|
|
|
* @var \eZ\Publish\API\Repository\UserService |
29
|
|
|
*/ |
30
|
|
|
protected $userService; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Content service. |
34
|
|
|
* |
35
|
|
|
* @var \eZ\Publish\API\Repository\ContentService |
36
|
|
|
*/ |
37
|
|
|
protected $contentService; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* FieldType parser. |
41
|
|
|
* |
42
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser |
43
|
|
|
*/ |
44
|
|
|
protected $fieldTypeParser; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Parser tools. |
48
|
|
|
* |
49
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\ParserTools |
50
|
|
|
*/ |
51
|
|
|
protected $parserTools; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Construct. |
55
|
|
|
* |
56
|
|
|
* @param \eZ\Publish\API\Repository\UserService $userService |
57
|
|
|
* @param \eZ\Publish\API\Repository\ContentService $contentService |
58
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\FieldTypeParser $fieldTypeParser |
59
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParserTools $parserTools |
60
|
|
|
*/ |
61
|
|
|
public function __construct(UserService $userService, ContentService $contentService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools) |
62
|
|
|
{ |
63
|
|
|
$this->userService = $userService; |
64
|
|
|
$this->contentService = $contentService; |
65
|
|
|
$this->fieldTypeParser = $fieldTypeParser; |
66
|
|
|
$this->parserTools = $parserTools; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Parse input structure. |
71
|
|
|
* |
72
|
|
|
* @param array $data |
73
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
74
|
|
|
* |
75
|
|
|
* @return \eZ\Publish\Core\REST\Server\Values\RestUserUpdateStruct |
76
|
|
|
*/ |
77
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
78
|
|
|
{ |
79
|
|
|
$parsedData = []; |
80
|
|
|
|
81
|
|
|
//@todo XSD has a login element, but it's not possible to update login |
82
|
|
|
|
83
|
|
|
if (array_key_exists('email', $data)) { |
84
|
|
|
$parsedData['email'] = $data['email']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (array_key_exists('password', $data)) { |
88
|
|
|
$parsedData['password'] = $data['password']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (array_key_exists('enabled', $data)) { |
92
|
|
|
$parsedData['enabled'] = $this->parserTools->parseBooleanValue($data['enabled']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (array_key_exists('mainLanguageCode', $data)) { |
96
|
|
|
$parsedData['mainLanguageCode'] = $data['mainLanguageCode']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
if (array_key_exists('Section', $data) && is_array($data['Section'])) { |
|
|
|
|
100
|
|
|
if (!array_key_exists('_href', $data['Section'])) { |
101
|
|
|
throw new Exceptions\Parser("Missing '_href' attribute for Section element in UserUpdate."); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$parsedData['sectionId'] = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (array_key_exists('remoteId', $data)) { |
108
|
|
|
$parsedData['remoteId'] = $data['remoteId']; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (array_key_exists('fields', $data)) { |
112
|
|
|
$userId = $this->requestParser->parseHref($data['__url'], 'userId'); |
113
|
|
|
|
114
|
|
|
if (!is_array($data['fields']) || !array_key_exists('field', $data['fields']) || !is_array($data['fields']['field'])) { |
115
|
|
|
throw new Exceptions\Parser("Invalid 'fields' element for UserUpdate."); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$parsedData['fields'] = []; |
119
|
|
View Code Duplication |
foreach ($data['fields']['field'] as $fieldData) { |
|
|
|
|
120
|
|
|
if (!array_key_exists('fieldDefinitionIdentifier', $fieldData)) { |
121
|
|
|
throw new Exceptions\Parser("Missing 'fieldDefinitionIdentifier' element in field data for UserUpdate."); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (!array_key_exists('fieldValue', $fieldData)) { |
125
|
|
|
throw new Exceptions\Parser("Missing 'fieldValue' element for '{$fieldData['fieldDefinitionIdentifier']}' identifier in UserUpdate."); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$fieldValue = $this->fieldTypeParser->parseFieldValue($userId, $fieldData['fieldDefinitionIdentifier'], $fieldData['fieldValue']); |
129
|
|
|
|
130
|
|
|
$languageCode = null; |
131
|
|
|
if (array_key_exists('languageCode', $fieldData)) { |
132
|
|
|
$languageCode = $fieldData['languageCode']; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$parsedData['fields'][$fieldData['fieldDefinitionIdentifier']] = [ |
136
|
|
|
'fieldValue' => $fieldValue, |
137
|
|
|
'languageCode' => $languageCode, |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$userUpdateStruct = $this->userService->newUserUpdateStruct(); |
143
|
|
|
|
144
|
|
|
if (!empty($parsedData)) { |
145
|
|
|
if (array_key_exists('email', $parsedData)) { |
146
|
|
|
$userUpdateStruct->email = $parsedData['email']; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (array_key_exists('password', $parsedData)) { |
150
|
|
|
$userUpdateStruct->password = $parsedData['password']; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (array_key_exists('enabled', $parsedData)) { |
154
|
|
|
$userUpdateStruct->enabled = $parsedData['enabled']; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
View Code Duplication |
if (array_key_exists('mainLanguageCode', $parsedData) || array_key_exists('remoteId', $parsedData)) { |
|
|
|
|
158
|
|
|
$userUpdateStruct->contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct(); |
159
|
|
|
|
160
|
|
|
if (array_key_exists('mainLanguageCode', $parsedData)) { |
161
|
|
|
$userUpdateStruct->contentMetadataUpdateStruct->mainLanguageCode = $parsedData['mainLanguageCode']; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (array_key_exists('remoteId', $parsedData)) { |
165
|
|
|
$userUpdateStruct->contentMetadataUpdateStruct->remoteId = $parsedData['remoteId']; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
if (array_key_exists('fields', $parsedData)) { |
|
|
|
|
170
|
|
|
$userUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct(); |
171
|
|
|
|
172
|
|
|
foreach ($parsedData['fields'] as $fieldDefinitionIdentifier => $fieldValue) { |
173
|
|
|
$userUpdateStruct->contentUpdateStruct->setField( |
174
|
|
|
$fieldDefinitionIdentifier, |
175
|
|
|
$fieldValue['fieldValue'], |
176
|
|
|
$fieldValue['languageCode'] |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return new RestUserUpdateStruct( |
|
|
|
|
183
|
|
|
$userUpdateStruct, |
184
|
|
|
array_key_exists('sectionId', $parsedData) ? $parsedData['sectionId'] : null |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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.