Conditions | 27 |
Paths | 100 |
Total Lines | 240 |
Code Lines | 141 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
121 | public function getDocumentForm(Document $document, $generateEmptyFields = true) |
||
|
|||
122 | { |
||
123 | $documentForm = new \EWW\Dpf\Domain\Model\DocumentForm(); |
||
124 | $documentForm->generateCsrfToken(); |
||
125 | $documentForm->setUid($document->getDocumentType()->getUid()); |
||
126 | $documentForm->setDisplayName($document->getDocumentType()->getDisplayName()); |
||
127 | $documentForm->setName($document->getDocumentType()->getName()); |
||
128 | $documentForm->setDocumentUid($document->getUid()); |
||
129 | |||
130 | $documentForm->setPrimaryFileMandatory( |
||
131 | ( |
||
132 | ( |
||
133 | $this->getSettings()['deactivatePrimaryFileMandatoryCheck'] || |
||
134 | $document->getDocumentType()->getVirtualType() |
||
135 | )? false : true |
||
136 | ) |
||
137 | ); |
||
138 | |||
139 | $documentForm->setProcessNumber($document->getProcessNumber()); |
||
140 | $documentForm->setTemporary($document->isTemporary()); |
||
141 | |||
142 | $fedoraPid = $document->getObjectIdentifier(); |
||
143 | |||
144 | if (empty($fedoraPid)) { |
||
145 | $fedoraPid = $document->getReservedObjectIdentifier(); |
||
146 | } |
||
147 | |||
148 | $documentForm->setFedoraPid($fedoraPid); |
||
149 | |||
150 | $internalFormat = new \EWW\Dpf\Helper\InternalFormat($document->getXmlData(), $this->clientPid); |
||
151 | |||
152 | $excludeGroupAttributes = array(); |
||
153 | |||
154 | foreach ($document->getDocumentType()->getMetadataPage() as $metadataPage) { |
||
155 | $documentFormPage = new \EWW\Dpf\Domain\Model\DocumentFormPage(); |
||
156 | $documentFormPage->setUid($metadataPage->getUid()); |
||
157 | $documentFormPage->setDisplayName($metadataPage->getDisplayName()); |
||
158 | $documentFormPage->setName($metadataPage->getName()); |
||
159 | |||
160 | $documentFormPage->setAccessRestrictionRoles($metadataPage->getAccessRestrictionRoles()); |
||
161 | |||
162 | foreach ($metadataPage->getMetadataGroup() as $metadataGroup) { |
||
163 | /** @var MetadataGroup $metadataGroup */ |
||
164 | |||
165 | $documentFormGroup = new \EWW\Dpf\Domain\Model\DocumentFormGroup(); |
||
166 | $documentFormGroup->setUid($metadataGroup->getUid()); |
||
167 | $documentFormGroup->setDisplayName($metadataGroup->getDisplayName()); |
||
168 | $documentFormGroup->setName($metadataGroup->getName()); |
||
169 | $documentFormGroup->setMandatory($metadataGroup->getMandatory()); |
||
170 | |||
171 | $documentFormGroup->setAccessRestrictionRoles($metadataGroup->getAccessRestrictionRoles()); |
||
172 | |||
173 | $documentFormGroup->setInfoText($metadataGroup->getInfoText()); |
||
174 | $documentFormGroup->setGroupType($metadataGroup->getGroupType()); |
||
175 | $documentFormGroup->setMaxIteration($metadataGroup->getMaxIteration()); |
||
176 | |||
177 | $documentFormGroup->setOptionalGroups($metadataGroup->getOptionalGroups()); |
||
178 | $documentFormGroup->setRequiredGroups($metadataGroup->getRequiredGroups()); |
||
179 | |||
180 | $xpath = $internalFormat->getXpath(); |
||
181 | |||
182 | // get fixed attributes from xpath configuration |
||
183 | $fixedGroupAttributes = array(); |
||
184 | |||
185 | preg_match_all('/[A-Za-z0-9:@\.]+(\[@.*?\])*/', $metadataGroup->getAbsoluteMapping(), $groupMappingPathParts); |
||
186 | $groupMappingPathParts = $groupMappingPathParts[0]; |
||
187 | |||
188 | $groupMappingPath = end($groupMappingPathParts); |
||
189 | $groupMappingName = preg_replace('/\[@.+?\]/', '', $groupMappingPath); |
||
190 | |||
191 | if (preg_match_all('/\[@.+?\]/', $groupMappingPath, $matches)) { |
||
192 | $fixedGroupAttributes = $matches[0]; |
||
193 | } |
||
194 | |||
195 | // build mapping path, previous fixed attributes which are differ from |
||
196 | // the own fixed attributes are excluded |
||
197 | $queryGroupMapping = $metadataGroup->getAbsoluteMapping(); |
||
198 | if (strpos($queryGroupMapping, "@displayLabel") === false && is_array($excludeGroupAttributes[$groupMappingName])) { |
||
199 | foreach ($excludeGroupAttributes[$groupMappingName] as $excludeAttr => $excludeAttrValue) { |
||
200 | if (!in_array($excludeAttr, $fixedGroupAttributes)) { |
||
201 | $queryGroupMapping .= $excludeAttrValue; |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | // Read the group data. |
||
207 | if ($metadataGroup->hasMappingForReading()) { |
||
208 | $groupData = $xpath->query($metadataGroup->getAbsoluteMappingForReading()); |
||
209 | } else { |
||
210 | $groupData = $xpath->query($queryGroupMapping); |
||
211 | } |
||
212 | |||
213 | // Fixed attributes from groups must be excluded in following xpath queries |
||
214 | foreach ($fixedGroupAttributes as $excludeGroupAttribute) { |
||
215 | $excludeGroupAttributes[$groupMappingName][$excludeGroupAttribute] = "[not(" . trim($excludeGroupAttribute, "[] ") . ")]"; |
||
216 | } |
||
217 | |||
218 | if ($groupData->length > 0) { |
||
219 | foreach ($groupData as $key => $data) { |
||
220 | |||
221 | $documentFormGroupItem = clone ($documentFormGroup); |
||
222 | |||
223 | foreach ($metadataGroup->getMetadataObject() as $metadataObject) { |
||
224 | |||
225 | $documentFormField = new \EWW\Dpf\Domain\Model\DocumentFormField(); |
||
226 | $documentFormField->setUid($metadataObject->getUid()); |
||
227 | $documentFormField->setDisplayName($metadataObject->getDisplayName()); |
||
228 | $documentFormField->setName($metadataObject->getName()); |
||
229 | $documentFormField->setMandatory($metadataObject->getMandatory()); |
||
230 | |||
231 | $documentFormField->setAccessRestrictionRoles($metadataObject->getAccessRestrictionRoles()); |
||
232 | |||
233 | $documentFormField->setConsent($metadataObject->getConsent()); |
||
234 | $documentFormField->setValidation($metadataObject->getValidation()); |
||
235 | $documentFormField->setDataType($metadataObject->getDataType()); |
||
236 | $documentFormField->setMaxIteration($metadataObject->getMaxIteration()); |
||
237 | $documentFormField->setInputField($metadataObject->getInputField()); |
||
238 | $documentFormField->setInputOptions($metadataObject->getInputOptionList()); |
||
239 | $documentFormField->setFillOutService($metadataObject->getFillOutService()); |
||
240 | $documentFormField->setGndFieldUid($metadataObject->getGndFieldUid()); |
||
241 | $documentFormField->setMaxInputLength($metadataObject->getMaxInputLength()); |
||
242 | $documentFormField->setObjectType($metadataObject->getObjectType()); |
||
243 | |||
244 | $depositLicense = $this->depositLicenseRepository->findByUid($metadataObject->getDepositLicense()); |
||
245 | $documentFormField->setDepositLicense($depositLicense); |
||
246 | |||
247 | $documentFormField->setHelpText($metadataObject->getHelpText()); |
||
248 | |||
249 | $objectMapping = ""; |
||
250 | |||
251 | preg_match_all('/([A-Za-z0-9]+:[A-Za-z0-9]+(\[.*\])*|[A-Za-z0-9:@\.]+)/', $metadataObject->getRelativeMapping(), $objectMappingPath); |
||
252 | $objectMappingPath = $objectMappingPath[0]; |
||
253 | |||
254 | foreach ($objectMappingPath as $key => $value) { |
||
255 | |||
256 | // ensure that e.g. <mods:detail> and <mods:detail type="volume"> |
||
257 | // are not recognized as the same node |
||
258 | if ((strpos($value, "@") === false) && ($value != '.')) { |
||
259 | $objectMappingPath[$key] .= "[not(@*)]"; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | $objectMapping = implode("/", $objectMappingPath); |
||
264 | |||
265 | if ($objectMapping == '[not(@*)]' || empty($objectMappingPath)) { |
||
266 | $objectMapping = '.'; |
||
267 | } |
||
268 | |||
269 | if ($metadataObject->isModsExtension()) { |
||
270 | |||
271 | $referenceAttribute = $metadataGroup->getModsExtensionReference(); |
||
272 | $modsExtensionGroupMapping = $metadataGroup->getAbsoluteModsExtensionMapping(); |
||
273 | |||
274 | $refID = $data->getAttribute("ID"); |
||
275 | // filter hashes from referenceAttribute value for backwards compatibility reasons |
||
276 | $objectData = $xpath->query($modsExtensionGroupMapping . "[translate(@" . $referenceAttribute . ",'#','')=" . '"' . $refID . '"]/' . $objectMapping); |
||
277 | } else { |
||
278 | $objectData = $xpath->query($objectMapping, $data); |
||
279 | } |
||
280 | |||
281 | $documentFormField->setValue("", $metadataObject->getDefaultValue()); |
||
282 | |||
283 | if ($objectData->length > 0) { |
||
284 | |||
285 | foreach ($objectData as $key => $value) { |
||
286 | |||
287 | $documentFormFieldItem = clone ($documentFormField); |
||
288 | |||
289 | $objectValue = $value->nodeValue; |
||
290 | |||
291 | if ($metadataObject->getDataType() == \EWW\Dpf\Domain\Model\MetadataObject::INPUT_DATA_TYPE_DATE) { |
||
292 | $dateStr = explode('T', $objectValue); |
||
293 | $date = date_create_from_format('Y-m-d', trim($dateStr[0])); |
||
294 | if ($date) { |
||
295 | $objectValue = date_format($date, 'd.m.Y'); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | $objectValue = str_replace('"', "'", $objectValue); |
||
300 | |||
301 | $documentFormFieldItem->setValue($objectValue, $metadataObject->getDefaultValue()); |
||
302 | |||
303 | $documentFormGroupItem->addItem($documentFormFieldItem); |
||
304 | } |
||
305 | } else { |
||
306 | $documentFormGroupItem->addItem($documentFormField); |
||
307 | } |
||
308 | |||
309 | } |
||
310 | |||
311 | $documentFormPage->addItem($documentFormGroupItem); |
||
312 | } |
||
313 | } else { |
||
314 | |||
315 | $documentFormGroup->setEmptyGroup(true); |
||
316 | |||
317 | foreach ($metadataGroup->getMetadataObject() as $metadataObject) { |
||
318 | $documentFormField = new \EWW\Dpf\Domain\Model\DocumentFormField(); |
||
319 | $documentFormField->setUid($metadataObject->getUid()); |
||
320 | $documentFormField->setDisplayName($metadataObject->getDisplayName()); |
||
321 | $documentFormField->setName($metadataObject->getName()); |
||
322 | $documentFormField->setMandatory($metadataObject->getMandatory()); |
||
323 | |||
324 | $documentFormField->setAccessRestrictionRoles($metadataObject->getAccessRestrictionRoles()); |
||
325 | |||
326 | $documentFormField->setConsent($metadataObject->getConsent()); |
||
327 | $documentFormField->setValidation($metadataObject->getValidation()); |
||
328 | $documentFormField->setDataType($metadataObject->getDataType()); |
||
329 | $documentFormField->setMaxIteration($metadataObject->getMaxIteration()); |
||
330 | $documentFormField->setInputField($metadataObject->getInputField()); |
||
331 | $documentFormField->setInputOptions($metadataObject->getInputOptionList()); |
||
332 | $documentFormField->setFillOutService($metadataObject->getFillOutService()); |
||
333 | $documentFormField->setGndFieldUid($metadataObject->getGndFieldUid()); |
||
334 | $documentFormField->setMaxInputLength($metadataObject->getMaxInputLength()); |
||
335 | $documentFormField->setValue("", $metadataObject->getDefaultValue()); |
||
336 | $documentFormField->setObjectType($metadataObject->getObjectType()); |
||
337 | |||
338 | $depositLicense = $this->depositLicenseRepository->findByUid($metadataObject->getDepositLicense()); |
||
339 | $documentFormField->setDepositLicense($depositLicense); |
||
340 | |||
341 | $documentFormField->setHelpText($metadataObject->getHelpText()); |
||
342 | |||
343 | $documentFormGroup->addItem($documentFormField); |
||
344 | } |
||
345 | |||
346 | $documentFormPage->addItem($documentFormGroup); |
||
347 | } |
||
348 | } |
||
349 | |||
350 | $documentForm->addItem($documentFormPage); |
||
351 | } |
||
352 | |||
353 | // Files |
||
354 | $primaryFile = $this->fileRepository->getPrimaryFileByDocument($document); |
||
355 | $documentForm->setPrimaryFile($primaryFile); |
||
356 | |||
357 | $secondaryFiles = $this->fileRepository->getSecondaryFilesByDocument($document)->toArray(); |
||
358 | $documentForm->setSecondaryFiles($secondaryFiles); |
||
359 | |||
360 | return $documentForm; |
||
361 | } |
||
548 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.