| Conditions | 19 |
| Paths | 1992 |
| Total Lines | 106 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 187 | public function getMetadataFromJson($jsonData, $documentType = null) |
||
| 188 | { |
||
| 189 | $jsonData = empty($jsonData)? null: $jsonData; |
||
| 190 | $jsonObject = new JsonObject($jsonData); |
||
| 191 | |||
| 192 | if ($documentType) { |
||
| 193 | $publicationType = $documentType; |
||
| 194 | } else { |
||
| 195 | $publicationType = $jsonObject->get('$.publicationType'); |
||
| 196 | if ($publicationType && is_array($publicationType)) { |
||
| 197 | $publicationType = $publicationType[0]; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** @var \EWW\Dpf\Domain\Model\DocumentType $documentType */ |
||
| 201 | $documentType = $this->documentTypeRepository->findOneByName($publicationType); |
||
| 202 | } |
||
| 203 | |||
| 204 | $resultData = []; |
||
| 205 | |||
| 206 | if (empty($documentType)) { |
||
| 207 | // default type |
||
| 208 | $documentType = $this->documentTypeRepository->findOneByName('article'); |
||
| 209 | } |
||
| 210 | |||
| 211 | foreach ($documentType->getMetadataPage() as $metadataPage) { |
||
| 212 | |||
| 213 | foreach ($metadataPage->getMetadataGroup() as $metadataGroup) { |
||
| 214 | |||
| 215 | // Group mapping |
||
| 216 | $jsonDataObject = new JsonObject($jsonData); |
||
| 217 | $jsonGroupMapping = $metadataGroup->getJsonMapping(); |
||
| 218 | $groupItems = []; |
||
| 219 | if ($jsonGroupMapping) { |
||
| 220 | $groupItems = $jsonDataObject->get($jsonGroupMapping); |
||
| 221 | } |
||
| 222 | |||
| 223 | if (empty($groupItems)) { |
||
| 224 | $groupItems = []; |
||
| 225 | } |
||
| 226 | |||
| 227 | foreach ($groupItems as $groupItem) { |
||
| 228 | |||
| 229 | $resultGroup = [ |
||
| 230 | 'attributes' => [], |
||
| 231 | 'values' => [] |
||
| 232 | ]; |
||
| 233 | $resultGroup['mapping'] = $metadataGroup->getRelativeMapping(); |
||
| 234 | $resultGroup['modsExtensionMapping'] = $metadataGroup->getRelativeModsExtensionMapping(); |
||
| 235 | $resultGroup['modsExtensionReference'] = trim($metadataGroup->getModsExtensionReference(), " /"); |
||
| 236 | $resultGroup['groupUid'] = $metadataGroup->getUid(); |
||
| 237 | |||
| 238 | foreach ($metadataGroup->getMetadataObject() as $metadataObject) { |
||
| 239 | |||
| 240 | $json = json_encode($groupItem); |
||
| 241 | |||
| 242 | $jsonObject = new JsonObject($json); |
||
| 243 | |||
| 244 | $fieldItems = []; |
||
| 245 | $jsonFieldMapping = $metadataObject->getJsonMapping(); |
||
| 246 | |||
| 247 | if ($jsonFieldMapping) { |
||
| 248 | $fieldItems = $jsonObject->get($jsonFieldMapping); |
||
| 249 | if (empty($fieldItems)) { |
||
| 250 | $fieldItems = []; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | foreach ($fieldItems as $fieldItem) { |
||
| 255 | $resultField = []; |
||
| 256 | |||
| 257 | if (!is_array($fieldItem)) { |
||
| 258 | $value = $fieldItem; |
||
| 259 | } else { |
||
| 260 | $value = implode("; ", $fieldItem); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ($metadataObject->getDataType() == \EWW\Dpf\Domain\Model\MetadataObject::INPUT_DATA_TYPE_DATE) { |
||
| 264 | $date = date_create_from_format('d.m.Y', trim($value)); |
||
| 265 | if ($date) { |
||
| 266 | $value = date_format($date, 'Y-m-d'); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | //if ($value) { |
||
| 271 | $value = str_replace('"', "'", $value); |
||
| 272 | $fieldMapping = $metadataObject->getRelativeMapping(); |
||
| 273 | $resultField['modsExtension'] = $metadataObject->getModsExtension(); |
||
| 274 | $resultField['mapping'] = $fieldMapping; |
||
| 275 | $resultField['value'] = $value; |
||
| 276 | |||
| 277 | if (strpos($fieldMapping, "@") === 0) { |
||
| 278 | $resultGroup['attributes'][] = $resultField; |
||
| 279 | } else { |
||
| 280 | $resultGroup['values'][] = $resultField; |
||
| 281 | } |
||
| 282 | //} |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | $resultData[] = $resultGroup;; |
||
| 287 | } |
||
| 288 | |||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | return $resultData; |
||
| 293 | } |
||
| 328 |