| Total Complexity | 40 |
| Total Lines | 419 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ParserGenerator 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 ParserGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ParserGenerator |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * clientConfigurationManager |
||
| 30 | * |
||
| 31 | * @var \EWW\Dpf\Configuration\ClientConfigurationManager |
||
| 32 | * |
||
| 33 | */ |
||
| 34 | protected $clientConfigurationManager; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * documentTypeRepository |
||
| 38 | * |
||
| 39 | * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
||
| 40 | */ |
||
| 41 | protected $documentTypeRepository = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * formData |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $formData = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * files from form |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $files = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * metsData |
||
| 58 | * |
||
| 59 | * @var DOMDocument |
||
|
|
|||
| 60 | */ |
||
| 61 | protected $metsData = ''; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * xml data |
||
| 65 | * @var DOMDocument |
||
| 66 | */ |
||
| 67 | protected $xmlData = ''; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * xml header |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $xmlHeader = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * xPathXMLGenerator |
||
| 77 | * @var object |
||
| 78 | */ |
||
| 79 | protected $parser = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * ref id counter |
||
| 83 | */ |
||
| 84 | protected $counter = 0; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * namespaces as string |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $namespaceString = ''; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * ParserGenerator constructor. |
||
| 94 | * @param int $clientPid |
||
| 95 | */ |
||
| 96 | public function __construct($clientPid = 0) |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * returns the mods xml string |
||
| 129 | * @return string mods xml |
||
| 130 | */ |
||
| 131 | public function getXMLData() |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * build mods from form array |
||
| 141 | * @param array $array structured form data array |
||
| 142 | */ |
||
| 143 | public function buildXmlFromForm($array) |
||
| 144 | { |
||
| 145 | $fedoraNamespace = $this->clientConfigurationManager->getFedoraNamespace(); |
||
| 146 | |||
| 147 | $this->xmlData = $this->xmlData; |
||
| 148 | // Build xml mods from form fields |
||
| 149 | // loop each group |
||
| 150 | foreach ($array['metadata'] as $key => $group) { |
||
| 151 | //groups |
||
| 152 | $mapping = $group['mapping']; |
||
| 153 | |||
| 154 | $values = $group['values']; |
||
| 155 | $attributes = $group['attributes']; |
||
| 156 | |||
| 157 | $attributeXPath = ''; |
||
| 158 | $extensionAttribute = ''; |
||
| 159 | foreach ($attributes as $attribute) { |
||
| 160 | if (!$attribute["modsExtension"]) { |
||
| 161 | $attributeXPath .= '[' . $attribute['mapping'] . '="' . $attribute['value'] . '"]'; |
||
| 162 | } else { |
||
| 163 | $extensionAttribute .= '[' . $attribute['mapping'] . '="' . $attribute['value'] . '"]'; |
||
| 164 | } |
||
| 165 | |||
| 166 | } |
||
| 167 | |||
| 168 | // mods extension |
||
| 169 | if ($group['modsExtensionMapping']) { |
||
| 170 | $counter = sprintf("%'03d", $this->counter); |
||
| 171 | $attributeXPath .= '[@ID="'.$fedoraNamespace.'_' . $counter . '"]'; |
||
| 172 | } |
||
| 173 | |||
| 174 | $existsExtensionFlag = false; |
||
| 175 | $i = 0; |
||
| 176 | // loop each object |
||
| 177 | if (!empty($values)) { |
||
| 178 | //$values = empty($values)? [] : $values; |
||
| 179 | foreach ($values as $value) { |
||
| 180 | |||
| 181 | if ($value['modsExtension']) { |
||
| 182 | $existsExtensionFlag = true; |
||
| 183 | // mods extension |
||
| 184 | $counter = sprintf("%'03d", $this->counter); |
||
| 185 | $referenceAttribute = $extensionAttribute . '[@' . $group['modsExtensionReference'] . '="' . $fedoraNamespace . '_' . $counter . '"]'; |
||
| 186 | |||
| 187 | $path = $group['modsExtensionMapping'] . $referenceAttribute . '%/' . $value['mapping']; |
||
| 188 | |||
| 189 | $xml = $this->customXPath($path, false, $value['value']); |
||
| 190 | } else { |
||
| 191 | $path = $mapping . $attributeXPath . '%/' . $value['mapping']; |
||
| 192 | |||
| 193 | if ($i == 0) { |
||
| 194 | $newGroupFlag = true; |
||
| 195 | } else { |
||
| 196 | $newGroupFlag = false; |
||
| 197 | } |
||
| 198 | |||
| 199 | $this->customXPath($path, $newGroupFlag, $value['value']); |
||
| 200 | $i++; |
||
| 201 | |||
| 202 | } |
||
| 203 | |||
| 204 | } |
||
| 205 | } else { |
||
| 206 | if (!empty($attributeXPath)) { |
||
| 207 | $path = $mapping . $attributeXPath; |
||
| 208 | $this->customXPath($path, true, '', true); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | if (!$existsExtensionFlag && $group['modsExtensionMapping']) { |
||
| 212 | $xPath = $group['modsExtensionMapping'] . $extensionAttribute . '[@' . $group['modsExtensionReference'] . '="'.$fedoraNamespace.'_' . $counter . '"]'; |
||
| 213 | $xml = $this->customXPath($xPath, true, '', true); |
||
| 214 | } |
||
| 215 | if ($group['modsExtensionMapping']) { |
||
| 216 | $this->counter++; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * get xml from xpath |
||
| 223 | * @param xpath $xPath xPath expression |
||
| 224 | * @return xml |
||
| 225 | */ |
||
| 226 | public function parseXPath($xPath) |
||
| 227 | { |
||
| 228 | |||
| 229 | $this->parser->generateXmlFromXPath($xPath); |
||
| 230 | $xml = $this->parser->getXML(); |
||
| 231 | |||
| 232 | return $xml; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function parseXPathWrapped($xPath) |
||
| 236 | { |
||
| 237 | $this->parser->generateXmlFromXPath($xPath); |
||
| 238 | $xml = $this->parser->getXML(); |
||
| 239 | |||
| 240 | $xml = '<data' . $this->namespaceString . '>' . $xml . '</data>'; |
||
| 241 | |||
| 242 | return $xml; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Customized xPath parser |
||
| 247 | * @param xpath $xPath xpath expression |
||
| 248 | * @param string $value form value |
||
| 249 | * @return xml created xml |
||
| 250 | */ |
||
| 251 | public function customXPath($xPath, $newGroupFlag = false, $value = '', $attributeOnly = false) |
||
| 252 | { |
||
| 253 | if (!$attributeOnly) { |
||
| 254 | // Explode xPath |
||
| 255 | $newPath = explode('%', $xPath); |
||
| 256 | |||
| 257 | $praedicateFlag = false; |
||
| 258 | $explodedXPath = explode('[', $newPath[0]); |
||
| 259 | if (count($explodedXPath) > 1) { |
||
| 260 | // praedicate is given |
||
| 261 | if (substr($explodedXPath[1], 0, 1) == "@") { |
||
| 262 | // attribute |
||
| 263 | $path = $newPath[0]; |
||
| 264 | } else { |
||
| 265 | // path |
||
| 266 | $path = $explodedXPath[0]; |
||
| 267 | } |
||
| 268 | |||
| 269 | $praedicateFlag = true; |
||
| 270 | } else { |
||
| 271 | $path = $newPath[0]; |
||
| 272 | } |
||
| 273 | |||
| 274 | if (isset($value) === true && $value !== '') { |
||
| 275 | $newPath[1] = $newPath[1] . '="' . $value . '"'; |
||
| 276 | } |
||
| 277 | |||
| 278 | $modsDataXPath = \EWW\Dpf\Helper\XPath::create($this->xmlData); |
||
| 279 | |||
| 280 | if (!$newGroupFlag && $modsDataXPath->query('/data/' . $newPath[0])->length > 0) { |
||
| 281 | // first xpath path exist |
||
| 282 | |||
| 283 | // build xml from second xpath part |
||
| 284 | $xml = $this->parseXPath($newPath[1]); |
||
| 285 | |||
| 286 | // check if xpath [] are nested |
||
| 287 | $search = '/(\/\w*:\w*)\[(.*)\]/'; |
||
| 288 | preg_match($search, $newPath[1], $match); |
||
| 289 | preg_match($search, $match[2], $secondMatch); |
||
| 290 | // first part nested xpath |
||
| 291 | if ($match[2] && $secondMatch[2]) { |
||
| 292 | $nested = $match[2]; |
||
| 293 | |||
| 294 | $nestedXml = $this->parseXPath($nested); |
||
| 295 | |||
| 296 | // object xpath without nested element [] |
||
| 297 | $newPath[1] = str_replace('['.$match[2].']', '', $newPath[1]); |
||
| 298 | |||
| 299 | $xml = $this->parseXPath($newPath[1]); |
||
| 300 | |||
| 301 | } |
||
| 302 | |||
| 303 | // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces, |
||
| 304 | // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here, |
||
| 305 | // since it is about child elements that are then added to the overall XML. |
||
| 306 | libxml_use_internal_errors(true); |
||
| 307 | $docXML = new \DOMDocument(); |
||
| 308 | $docXML->loadXML($xml); |
||
| 309 | libxml_use_internal_errors(false); |
||
| 310 | |||
| 311 | $domXPath = \EWW\Dpf\Helper\XPath::create($this->xmlData); |
||
| 312 | |||
| 313 | // second part nested xpath |
||
| 314 | if ($match[2] && $secondMatch[2]) { |
||
| 315 | |||
| 316 | // import node from nested |
||
| 317 | // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces, |
||
| 318 | // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here, |
||
| 319 | // since it is about child elements that are then added to the overall XML. |
||
| 320 | libxml_use_internal_errors(true); |
||
| 321 | $docXMLNested = new \DOMDocument(); |
||
| 322 | $docXMLNested->loadXML($nestedXml); |
||
| 323 | libxml_use_internal_errors(false); |
||
| 324 | |||
| 325 | $xPath = \EWW\Dpf\Helper\XPath::create($docXML); |
||
| 326 | |||
| 327 | $nodeList = $xPath->query($match[1]); |
||
| 328 | $node = $nodeList->item(0); |
||
| 329 | |||
| 330 | $importNode = $docXML->importNode($docXMLNested->getElementsByTagName("mods")->item(0)->firstChild, true); |
||
| 331 | |||
| 332 | $node->appendChild($importNode); |
||
| 333 | } |
||
| 334 | |||
| 335 | $domNode = $domXPath->query('/data/' . $path); |
||
| 336 | $node = $docXML->documentElement; |
||
| 337 | |||
| 338 | $nodeAppendModsData = $this->xmlData->importNode($node, true); |
||
| 339 | $domNode->item($domNode->length - 1)->appendChild($nodeAppendModsData); |
||
| 340 | } else { |
||
| 341 | // first xpath doesn't exist |
||
| 342 | // parse first xpath part |
||
| 343 | $xml1 = $this->parseXPathWrapped($newPath[0]); |
||
| 344 | |||
| 345 | $doc1 = new \DOMDocument(); |
||
| 346 | $doc1->loadXML($xml1); |
||
| 347 | |||
| 348 | $domXPath = \EWW\Dpf\Helper\XPath::create($doc1); |
||
| 349 | |||
| 350 | $domNode = $domXPath->query('//' . $path); |
||
| 351 | |||
| 352 | // parse second xpath part |
||
| 353 | $xml2 = $this->parseXPathWrapped($path . $newPath[1]); |
||
| 354 | |||
| 355 | // check if xpath [] are nested |
||
| 356 | $search = '/(\/\w*:?\w*)\[(.*)\]/'; |
||
| 357 | preg_match($search, $newPath[1], $match); |
||
| 358 | preg_match($search, $match[2], $secondMatch); |
||
| 359 | |||
| 360 | // first part nested xpath |
||
| 361 | if ($match[2] && $secondMatch[2]) { |
||
| 362 | $nested = $match[2]; |
||
| 363 | |||
| 364 | $nestedXml = $this->parseXPathWrapped($nested); |
||
| 365 | |||
| 366 | // object xpath without nested element [] |
||
| 367 | $newPath[1] = str_replace('['.$match[2].']', '', $newPath[1]); |
||
| 368 | |||
| 369 | $xml2 = $this->parseXPathWrapped($path . $newPath[1]); |
||
| 370 | } |
||
| 371 | |||
| 372 | $doc2 = new \DOMDocument(); |
||
| 373 | $doc2->loadXML($xml2); |
||
| 374 | |||
| 375 | $domXPath2 = \EWW\Dpf\Helper\XPath::create($doc2); |
||
| 376 | |||
| 377 | // second part nested xpath |
||
| 378 | if ($match[2] && $secondMatch[2]) { |
||
| 379 | // import node from nested |
||
| 380 | $docXMLNested = new \DOMDocument(); |
||
| 381 | $docXMLNested->loadXML($nestedXml); |
||
| 382 | |||
| 383 | $xPath = \EWW\Dpf\Helper\XPath::create($doc2); |
||
| 384 | $nodeList = $xPath->query('//' . $path . $match[1]); |
||
| 385 | $node = $nodeList->item(0); |
||
| 386 | |||
| 387 | $importNode = $doc2->importNode($docXMLNested->documentElement, true); |
||
| 388 | |||
| 389 | $node->appendChild($importNode); |
||
| 390 | } |
||
| 391 | $domNode2 = $domXPath2->query('//' . $path)->item(0)->childNodes->item(0); |
||
| 392 | |||
| 393 | // merge xml nodes |
||
| 394 | $nodeToBeAppended = $doc1->importNode($domNode2, true); |
||
| 395 | $domNode->item(0)->appendChild($nodeToBeAppended); |
||
| 396 | |||
| 397 | // add to modsData (merge not required) |
||
| 398 | // get mods tag |
||
| 399 | foreach ($this->xmlData->childNodes as $childNode) { |
||
| 400 | // Skip comments inside the xml. |
||
| 401 | if ($childNode instanceof \DOMElement) { |
||
| 402 | $firstChild = $childNode; |
||
| 403 | break; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | //$firstChild = $this->xmlData->childNodes->item(0); |
||
| 407 | $firstItem = $doc1->documentElement->firstChild; |
||
| 408 | $nodeAppendModsData = $this->xmlData->importNode($firstItem, true); |
||
| 409 | $firstChild->appendChild($nodeAppendModsData); |
||
| 410 | |||
| 411 | return $doc1->saveXML(); |
||
| 412 | } |
||
| 413 | } else { |
||
| 414 | // attribute only |
||
| 415 | $xml = $this->parseXPath($xPath); |
||
| 416 | |||
| 417 | // FIXME: XPATHXmlGenerator XPATH does not generate any namespaces, |
||
| 418 | // which DOMDocument cannot cope with. Actually, namespaces should not be necessary here, |
||
| 419 | // since it is about child elements that are then added to the overall XML. |
||
| 420 | libxml_use_internal_errors(true); |
||
| 421 | $docXML = new \DOMDocument(); |
||
| 422 | $docXML->loadXML($xml); |
||
| 423 | libxml_use_internal_errors(false); |
||
| 424 | |||
| 425 | $domXPath = \EWW\Dpf\Helper\XPath::create($this->xmlData); |
||
| 426 | $domNode = $domXPath->query('/data'); |
||
| 427 | |||
| 428 | $node = $docXML->documentElement; |
||
| 429 | |||
| 430 | $nodeAppendModsData = $this->xmlData->importNode($node, true); |
||
| 431 | $domNode->item($domNode->length - 1)->appendChild($nodeAppendModsData); |
||
| 432 | |||
| 433 | return $docXML->saveXML(); |
||
| 434 | } |
||
| 435 | |||
| 436 | return $this->xmlData->saveXML(); |
||
| 437 | } |
||
| 438 | |||
| 439 | public function setXML($value = '') { |
||
| 445 | } |
||
| 446 | } |
||
| 447 |