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