1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the UserCreate 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\API\Repository\UserService; |
17
|
|
|
use eZ\Publish\API\Repository\ContentTypeService; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Parser for UserCreate. |
21
|
|
|
*/ |
22
|
|
|
class UserCreate extends BaseParser |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* User service. |
26
|
|
|
* |
27
|
|
|
* @var \eZ\Publish\API\Repository\UserService |
28
|
|
|
*/ |
29
|
|
|
protected $userService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ContentType service. |
33
|
|
|
* |
34
|
|
|
* @var \eZ\Publish\API\Repository\ContentTypeService |
35
|
|
|
*/ |
36
|
|
|
protected $contentTypeService; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* FieldType parser. |
40
|
|
|
* |
41
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser |
42
|
|
|
*/ |
43
|
|
|
protected $fieldTypeParser; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Parser tools. |
47
|
|
|
* |
48
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\ParserTools |
49
|
|
|
*/ |
50
|
|
|
protected $parserTools; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Construct. |
54
|
|
|
* |
55
|
|
|
* @param \eZ\Publish\API\Repository\UserService $userService |
56
|
|
|
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService |
57
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\FieldTypeParser $fieldTypeParser |
58
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParserTools $parserTools |
59
|
|
|
*/ |
60
|
|
|
public function __construct(UserService $userService, ContentTypeService $contentTypeService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools) |
61
|
|
|
{ |
62
|
|
|
$this->userService = $userService; |
63
|
|
|
$this->contentTypeService = $contentTypeService; |
64
|
|
|
$this->fieldTypeParser = $fieldTypeParser; |
65
|
|
|
$this->parserTools = $parserTools; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Parse input structure. |
70
|
|
|
* |
71
|
|
|
* @param array $data |
72
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
73
|
|
|
* |
74
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\UserCreateStruct |
75
|
|
|
*/ |
76
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
77
|
|
|
{ |
78
|
|
|
$contentType = null; |
79
|
|
|
if (array_key_exists('ContentType', $data) && is_array($data['ContentType'])) { |
80
|
|
|
if (!array_key_exists('_href', $data['ContentType'])) { |
81
|
|
|
throw new Exceptions\Parser("Missing '_href' attribute for ContentType element in UserCreate."); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$contentType = $this->contentTypeService->loadContentType( |
85
|
|
|
$this->requestParser->parseHref($data['ContentType']['_href'], 'contentTypeId') |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!array_key_exists('mainLanguageCode', $data)) { |
90
|
|
|
throw new Exceptions\Parser("Missing 'mainLanguageCode' element for UserCreate."); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!array_key_exists('login', $data)) { |
94
|
|
|
throw new Exceptions\Parser("Missing 'login' element for UserCreate."); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!array_key_exists('email', $data)) { |
98
|
|
|
throw new Exceptions\Parser("Missing 'email' element for UserCreate."); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!array_key_exists('password', $data)) { |
102
|
|
|
throw new Exceptions\Parser("Missing 'password' element for UserCreate."); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$userCreateStruct = $this->userService->newUserCreateStruct( |
106
|
|
|
$data['login'], |
107
|
|
|
$data['email'], |
108
|
|
|
$data['password'], |
109
|
|
|
$data['mainLanguageCode'], |
110
|
|
|
$contentType |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
if (array_key_exists('Section', $data) && is_array($data['Section'])) { |
114
|
|
|
if (!array_key_exists('_href', $data['Section'])) { |
115
|
|
|
throw new Exceptions\Parser("Missing '_href' attribute for Section element in UserCreate."); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$userCreateStruct->sectionId = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (array_key_exists('remoteId', $data)) { |
122
|
|
|
$userCreateStruct->remoteId = $data['remoteId']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (array_key_exists('enabled', $data)) { |
126
|
|
|
$userCreateStruct->enabled = $this->parserTools->parseBooleanValue($data['enabled']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!array_key_exists('fields', $data) || !is_array($data['fields']) || !is_array($data['fields']['field'])) { |
130
|
|
|
throw new Exceptions\Parser("Missing or invalid 'fields' element for UserCreate."); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
foreach ($data['fields']['field'] as $fieldData) { |
|
|
|
|
134
|
|
|
if (!array_key_exists('fieldDefinitionIdentifier', $fieldData)) { |
135
|
|
|
throw new Exceptions\Parser("Missing 'fieldDefinitionIdentifier' element in field data for UserCreate."); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$fieldDefinition = $userCreateStruct->contentType->getFieldDefinition($fieldData['fieldDefinitionIdentifier']); |
139
|
|
|
if (!$fieldDefinition) { |
140
|
|
|
throw new Exceptions\Parser( |
141
|
|
|
"'{$fieldData['fieldDefinitionIdentifier']}' is invalid field definition identifier for '{$userCreateStruct->contentType->identifier}' content type in UserCreate." |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!array_key_exists('fieldValue', $fieldData)) { |
146
|
|
|
throw new Exceptions\Parser("Missing 'fieldValue' element for '{$fieldData['fieldDefinitionIdentifier']}' identifier in UserCreate."); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$fieldValue = $this->fieldTypeParser->parseValue($fieldDefinition->fieldTypeIdentifier, $fieldData['fieldValue']); |
150
|
|
|
|
151
|
|
|
$languageCode = null; |
152
|
|
|
if (array_key_exists('languageCode', $fieldData)) { |
153
|
|
|
$languageCode = $fieldData['languageCode']; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$userCreateStruct->setField($fieldData['fieldDefinitionIdentifier'], $fieldValue, $languageCode); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $userCreateStruct; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.