| Total Complexity | 42 |
| Total Lines | 272 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Mods 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 Mods, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Mods |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * metadataObjectRepository |
||
| 22 | * |
||
| 23 | * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository |
||
| 24 | * @inject |
||
| 25 | */ |
||
| 26 | protected $metadataObjectRepository = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * metadatGroupRepository |
||
| 30 | * |
||
| 31 | * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
||
| 32 | * @inject |
||
| 33 | */ |
||
| 34 | protected $metadataGroupRepository = null; |
||
| 35 | |||
| 36 | |||
| 37 | protected $modsDom; |
||
| 38 | |||
| 39 | public function __construct($modsXml) |
||
| 40 | { |
||
| 41 | $this->setModsXml($modsXml); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function setModsXml($modsXml) |
||
| 45 | { |
||
| 46 | $modsDom = new \DOMDocument(); |
||
| 47 | if (!empty($modsXml)) { |
||
| 48 | if (is_null(@$modsDom->loadXML($modsXml))) { |
||
| 49 | throw new \Exception("Couldn't load MODS data!"); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | $this->modsDom = $modsDom; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getModsXml() |
||
| 56 | { |
||
| 57 | return $this->modsDom->saveXML(); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getModsXpath() |
||
| 61 | { |
||
| 62 | $xpath = \EWW\Dpf\Helper\XPath::create($this->modsDom); |
||
| 63 | return $xpath; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function getTitle() |
||
| 67 | { |
||
| 68 | $titleNode = $this->getModsXpath()->query('/mods:mods/mods:titleInfo[@usage="primary"]/mods:title'); |
||
| 69 | |||
| 70 | if ($titleNode->length == 0) { |
||
| 71 | $titleNode = $this->getModsXpath()->query("/mods:mods/mods:titleInfo/mods:title"); |
||
| 72 | } |
||
| 73 | return $titleNode->item(0)->nodeValue; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function getAuthors() |
||
| 77 | { |
||
| 78 | return $this->getPersons("aut"); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function getPublishers() |
||
| 82 | { |
||
| 83 | return $this->getPersons("edt"); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get persons of the given role |
||
| 88 | * |
||
| 89 | * @param $role |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | protected function getPersons($role) |
||
| 93 | { |
||
| 94 | $xpath = $this->getModsXpath(); |
||
| 95 | |||
| 96 | $authorNode = $xpath->query('/mods:mods/mods:name[mods:role/mods:roleTerm[@type="code"]="'.$role.'"]'); |
||
| 97 | |||
| 98 | $authors = array(); |
||
| 99 | |||
| 100 | foreach ($authorNode as $key => $author) { |
||
| 101 | |||
| 102 | $familyNodes = $xpath->query('mods:namePart[@type="family"]', $author); |
||
| 103 | |||
| 104 | $givenNodes = $xpath->query('mods:namePart[@type="given"]', $author); |
||
| 105 | |||
| 106 | $name = array(); |
||
| 107 | |||
| 108 | if ($givenNodes->length > 0) { |
||
| 109 | $name[] = $givenNodes->item(0)->nodeValue; |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($familyNodes->length > 0) { |
||
| 113 | $name[] = $familyNodes->item(0)->nodeValue; |
||
| 114 | } |
||
| 115 | |||
| 116 | $authors[$key] = implode(" ", $name); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $authors; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function setDateIssued($date) |
||
| 123 | { |
||
| 124 | |||
| 125 | $originInfo = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]'); |
||
| 126 | |||
| 127 | if ($originInfo->length > 0) { |
||
| 128 | $dateIssued = $this->getModsXpath()->query('mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]', $originInfo->item(0)); |
||
| 129 | |||
| 130 | if ($dateIssued->length == 0) { |
||
| 131 | $newDateIssued = $this->modsDom->createElement('mods:dateIssued'); |
||
| 132 | $newDateIssued->setAttribute('encoding', 'iso8601'); |
||
| 133 | $newDateIssued->setAttribute('keyDate', 'yes'); |
||
| 134 | $newDateIssued->nodeValue = $date; |
||
| 135 | $originInfo->item(0)->appendChild($newDateIssued); |
||
| 136 | } else { |
||
| 137 | $dateIssued->item(0)->nodeValue = $date; |
||
| 138 | } |
||
| 139 | |||
| 140 | } else { |
||
| 141 | |||
| 142 | $rootNode = $this->getModsXpath()->query('/mods:mods'); |
||
| 143 | |||
| 144 | if ($rootNode->length == 1) { |
||
| 145 | $newOriginInfo = $this->modsDom->createElement('mods:originInfo'); |
||
| 146 | $newOriginInfo->setAttribute('eventType', 'distribution'); |
||
| 147 | $rootNode->item(0)->appendChild($newOriginInfo); |
||
| 148 | |||
| 149 | $newDateIssued = $this->modsDom->createElement('mods:dateIssued'); |
||
| 150 | $newDateIssued->setAttribute('encoding', 'iso8601'); |
||
| 151 | $newDateIssued->setAttribute('keyDate', 'yes'); |
||
| 152 | $newDateIssued->nodeValue = $date; |
||
| 153 | $newOriginInfo->appendChild($newDateIssued); |
||
| 154 | } else { |
||
| 155 | throw new \Exception('Invalid xml data.'); |
||
| 156 | } |
||
| 157 | |||
| 158 | } |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | public function getPublishingYear() |
||
| 164 | { |
||
| 165 | $year = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="publication"]/mods:dateIssued[@encoding="iso8601"]'); |
||
| 166 | if ($year->length > 0) { |
||
| 167 | return $year->item(0)->nodeValue; |
||
| 168 | } |
||
| 169 | |||
| 170 | return null; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function getOriginalSourceTitle() |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getDateIssued() |
||
| 184 | { |
||
| 185 | |||
| 186 | $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]'); |
||
| 187 | if ($dateIssued->length > 0) { |
||
| 188 | return $dateIssued->item(0)->nodeValue; |
||
| 189 | } |
||
| 190 | |||
| 191 | return null; |
||
| 192 | } |
||
| 193 | |||
| 194 | public function removeDateIssued() |
||
| 195 | { |
||
| 196 | |||
| 197 | $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]'); |
||
| 198 | if ($dateIssued->length > 0) { |
||
| 199 | $dateIssued->item(0)->parentNode->removeChild($dateIssued->item(0)); |
||
| 200 | } |
||
| 201 | |||
| 202 | } |
||
| 203 | |||
| 204 | public function hasQucosaUrn() |
||
| 205 | { |
||
| 206 | $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]'); |
||
| 207 | |||
| 208 | $hasUrn = false; |
||
| 209 | |||
| 210 | foreach ($urnNodeList as $urnNode) { |
||
| 211 | $value = $urnNode->nodeValue; |
||
| 212 | $hasUrn = $hasUrn || !empty($value); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $hasUrn; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function getQucosaUrn() |
||
| 219 | { |
||
| 220 | $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]'); |
||
| 221 | $urnList = ''; |
||
| 222 | |||
| 223 | if ($urnNodeList != null) { |
||
| 224 | foreach ($urnNodeList as $urnNode) { |
||
| 225 | $urnList = $urnNode->nodeValue; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | return $urnList; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function addQucosaUrn($urn) |
||
| 242 | } |
||
| 243 | |||
| 244 | } |
||
| 245 | |||
| 246 | public function clearAllUrn() |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getSourceDetails() |
||
| 290 | } |
||
| 291 | } |
||
| 292 |