| Total Complexity | 44 |
| Total Lines | 304 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like JsonToDocumentMapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JsonToDocumentMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class JsonToDocumentMapper |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * objectManager |
||
| 25 | * |
||
| 26 | * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
||
| 27 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 28 | */ |
||
| 29 | protected $objectManager; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * documentTypeRepository |
||
| 33 | * |
||
| 34 | * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
||
| 35 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 36 | */ |
||
| 37 | protected $documentTypeRepository = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * documentRepository |
||
| 41 | * |
||
| 42 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
| 43 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
| 44 | */ |
||
| 45 | protected $documentRepository = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Replaces the data from the document with the data from the json |
||
| 49 | * @param Document $document |
||
| 50 | * @param $jsonData |
||
| 51 | * @return Document |
||
| 52 | */ |
||
| 53 | public function editDocument(Document $document, $jsonData) |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Creates a document from the given json data |
||
| 129 | * |
||
| 130 | * @param string $jsonData |
||
| 131 | * @return Document $document |
||
| 132 | */ |
||
| 133 | public function getDocument($jsonData) |
||
| 134 | { |
||
| 135 | $jsonObject = new JsonObject($jsonData); |
||
| 136 | $publicationType = $jsonObject->get('$.publicationType'); |
||
| 137 | |||
| 138 | if ($publicationType && is_array($publicationType)) { |
||
| 139 | $publicationType = $publicationType[0]; |
||
| 140 | } |
||
| 141 | |||
| 142 | $documentType = $this->documentTypeRepository->findOneByName($publicationType); |
||
| 143 | if (!$documentType) { |
||
| 144 | return null; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** @var Document $document */ |
||
| 148 | $document = $this->objectManager->get(Document::class); |
||
| 149 | |||
| 150 | $document->setDocumentType($documentType); |
||
| 151 | |||
| 152 | $processNumberGenerator = $this->objectManager->get(ProcessNumberGenerator::class); |
||
| 153 | $processNumber = $processNumberGenerator->getProcessNumber(); |
||
| 154 | $document->setProcessNumber($processNumber); |
||
| 155 | |||
| 156 | $metaData = $this->getMetadataFromJson($jsonData); |
||
| 157 | |||
| 158 | $exporter = new \EWW\Dpf\Services\ParserGenerator(); |
||
| 159 | |||
| 160 | $documentData['documentUid'] = 0; |
||
| 161 | $documentData['metadata'] = $metaData; |
||
| 162 | $documentData['files'] = array(); |
||
| 163 | |||
| 164 | $exporter->buildXmlFromForm($documentData); |
||
| 165 | |||
| 166 | $internalXml = $exporter->getXMLData(); |
||
| 167 | $document->setXmlData($internalXml); |
||
| 168 | |||
| 169 | $internalFormat = new \EWW\Dpf\Helper\InternalFormat($internalXml); |
||
| 170 | |||
| 171 | $document->setTitle($internalFormat->getTitle()); |
||
| 172 | $document->setAuthors($internalFormat->getAuthors()); |
||
| 173 | $document->setDateIssued($internalFormat->getDateIssued()); |
||
| 174 | //$document->setEmbargoDate($formMetaData['embargo']); |
||
| 175 | |||
| 176 | $internalFormat->setDocumentType($documentType->getName()); |
||
| 177 | $internalFormat->setProcessNumber($document->getProcessNumber()); |
||
| 178 | |||
| 179 | $document->setXmlData($internalFormat->getXml()); |
||
| 180 | |||
| 181 | $document->setState(\EWW\Dpf\Domain\Workflow\DocumentWorkflow::STATE_REGISTERED_NONE); |
||
| 182 | |||
| 183 | return $document; |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 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 | } |
||
| 294 | |||
| 295 | protected function parseXpathString($xpathString) |
||
| 325 | } |
||
| 326 | |||
| 327 | } |
||
| 328 |