1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the UserGroupUpdate 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\Exceptions; |
15
|
|
|
use eZ\Publish\Core\REST\Server\Values\RestUserGroupUpdateStruct; |
16
|
|
|
use eZ\Publish\API\Repository\UserService; |
17
|
|
|
use eZ\Publish\API\Repository\ContentService; |
18
|
|
|
use eZ\Publish\API\Repository\LocationService; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Parser for UserGroupUpdate. |
22
|
|
|
*/ |
23
|
|
|
class UserGroupUpdate 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
|
|
|
* Location service. |
41
|
|
|
* |
42
|
|
|
* @var \eZ\Publish\API\Repository\LocationService |
43
|
|
|
*/ |
44
|
|
|
protected $locationService; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* FieldType parser. |
48
|
|
|
* |
49
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser |
50
|
|
|
*/ |
51
|
|
|
protected $fieldTypeParser; |
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\API\Repository\LocationService $locationService |
59
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\FieldTypeParser $fieldTypeParser |
60
|
|
|
*/ |
61
|
|
|
public function __construct(UserService $userService, ContentService $contentService, LocationService $locationService, FieldTypeParser $fieldTypeParser) |
62
|
|
|
{ |
63
|
|
|
$this->userService = $userService; |
64
|
|
|
$this->contentService = $contentService; |
65
|
|
|
$this->locationService = $locationService; |
66
|
|
|
$this->fieldTypeParser = $fieldTypeParser; |
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\RestUserGroupUpdateStruct |
76
|
|
|
*/ |
77
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
78
|
|
|
{ |
79
|
|
|
$parsedData = []; |
80
|
|
|
|
81
|
|
|
if (array_key_exists('mainLanguageCode', $data)) { |
82
|
|
|
$parsedData['mainLanguageCode'] = $data['mainLanguageCode']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
if (array_key_exists('Section', $data) && is_array($data['Section'])) { |
|
|
|
|
86
|
|
|
if (!array_key_exists('_href', $data['Section'])) { |
87
|
|
|
throw new Exceptions\Parser("Missing '_href' attribute for Section element in UserGroupUpdate."); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$parsedData['sectionId'] = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (array_key_exists('remoteId', $data)) { |
94
|
|
|
$parsedData['remoteId'] = $data['remoteId']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (array_key_exists('fields', $data)) { |
98
|
|
|
$groupLocationParts = explode('/', $this->requestParser->parseHref($data['__url'], 'groupPath')); |
99
|
|
|
|
100
|
|
|
$groupLocation = $this->locationService->loadLocation(array_pop($groupLocationParts)); |
101
|
|
|
|
102
|
|
|
if (!is_array($data['fields']) || !array_key_exists('field', $data['fields']) || !is_array($data['fields']['field'])) { |
103
|
|
|
throw new Exceptions\Parser("Invalid 'fields' element for UserGroupUpdate."); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$parsedData['fields'] = []; |
107
|
|
View Code Duplication |
foreach ($data['fields']['field'] as $fieldData) { |
|
|
|
|
108
|
|
|
if (!array_key_exists('fieldDefinitionIdentifier', $fieldData)) { |
109
|
|
|
throw new Exceptions\Parser("Missing 'fieldDefinitionIdentifier' element in field data for UserGroupUpdate."); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!array_key_exists('fieldValue', $fieldData)) { |
113
|
|
|
throw new Exceptions\Parser("Missing 'fieldValue' element for '{$fieldData['fieldDefinitionIdentifier']}' identifier in UserGroupUpdate."); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$fieldValue = $this->fieldTypeParser->parseFieldValue($groupLocation->contentId, $fieldData['fieldDefinitionIdentifier'], $fieldData['fieldValue']); |
117
|
|
|
|
118
|
|
|
$languageCode = null; |
119
|
|
|
if (array_key_exists('languageCode', $fieldData)) { |
120
|
|
|
$languageCode = $fieldData['languageCode']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$parsedData['fields'][$fieldData['fieldDefinitionIdentifier']] = [ |
124
|
|
|
'fieldValue' => $fieldValue, |
125
|
|
|
'languageCode' => $languageCode, |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct(); |
131
|
|
|
|
132
|
|
|
if (!empty($parsedData)) { |
133
|
|
View Code Duplication |
if (array_key_exists('mainLanguageCode', $parsedData) || array_key_exists('remoteId', $parsedData)) { |
|
|
|
|
134
|
|
|
$userGroupUpdateStruct->contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct(); |
135
|
|
|
|
136
|
|
|
if (array_key_exists('mainLanguageCode', $parsedData)) { |
137
|
|
|
$userGroupUpdateStruct->contentMetadataUpdateStruct->mainLanguageCode = $parsedData['mainLanguageCode']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (array_key_exists('remoteId', $parsedData)) { |
141
|
|
|
$userGroupUpdateStruct->contentMetadataUpdateStruct->remoteId = $parsedData['remoteId']; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
if (array_key_exists('fields', $parsedData)) { |
|
|
|
|
146
|
|
|
$userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct(); |
147
|
|
|
|
148
|
|
|
foreach ($parsedData['fields'] as $fieldDefinitionIdentifier => $fieldValue) { |
149
|
|
|
$userGroupUpdateStruct->contentUpdateStruct->setField( |
150
|
|
|
$fieldDefinitionIdentifier, |
151
|
|
|
$fieldValue['fieldValue'], |
152
|
|
|
$fieldValue['languageCode'] |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return new RestUserGroupUpdateStruct( |
|
|
|
|
159
|
|
|
$userGroupUpdateStruct, |
160
|
|
|
array_key_exists('sectionId', $parsedData) ? $parsedData['sectionId'] : null |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.