| Total Complexity | 45 |
| Total Lines | 287 |
| 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) |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getModsXml() |
||
| 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() |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | public function setTitle($title) |
||
| 78 | { |
||
| 79 | $titleNode = $this->getModsXpath()->query('/mods:mods/mods:titleInfo[@usage="primary"]/mods:title'); |
||
| 80 | |||
| 81 | if ($titleNode->length == 0) { |
||
| 82 | $titleNode = $this->getModsXpath()->query("/mods:mods/mods:titleInfo/mods:title"); |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($titleNode->length > 0) { |
||
| 86 | $titleNode->item(0)->nodeValue = $title; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | public function getAuthors() |
||
| 92 | { |
||
| 93 | return $this->getPersons("aut"); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getPublishers() |
||
| 97 | { |
||
| 98 | return $this->getPersons("edt"); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get persons of the given role |
||
| 103 | * |
||
| 104 | * @param $role |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | protected function getPersons($role) |
||
| 108 | { |
||
| 109 | $xpath = $this->getModsXpath(); |
||
| 110 | |||
| 111 | $authorNode = $xpath->query('/mods:mods/mods:name[mods:role/mods:roleTerm[@type="code"]="'.$role.'"]'); |
||
| 112 | |||
| 113 | $authors = array(); |
||
| 114 | |||
| 115 | foreach ($authorNode as $key => $author) { |
||
| 116 | |||
| 117 | $familyNodes = $xpath->query('mods:namePart[@type="family"]', $author); |
||
| 118 | |||
| 119 | $givenNodes = $xpath->query('mods:namePart[@type="given"]', $author); |
||
| 120 | |||
| 121 | $name = array(); |
||
| 122 | |||
| 123 | if ($givenNodes->length > 0) { |
||
| 124 | $name[] = $givenNodes->item(0)->nodeValue; |
||
| 125 | } |
||
| 126 | |||
| 127 | if ($familyNodes->length > 0) { |
||
| 128 | $name[] = $familyNodes->item(0)->nodeValue; |
||
| 129 | } |
||
| 130 | |||
| 131 | $authors[$key] = implode(" ", $name); |
||
| 132 | } |
||
| 133 | |||
| 134 | return $authors; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function setDateIssued($date) |
||
| 138 | { |
||
| 139 | |||
| 140 | $originInfo = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]'); |
||
| 141 | |||
| 142 | if ($originInfo->length > 0) { |
||
| 143 | $dateIssued = $this->getModsXpath()->query('mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]', $originInfo->item(0)); |
||
| 144 | |||
| 145 | if ($dateIssued->length == 0) { |
||
| 146 | $newDateIssued = $this->modsDom->createElement('mods:dateIssued'); |
||
| 147 | $newDateIssued->setAttribute('encoding', 'iso8601'); |
||
| 148 | $newDateIssued->setAttribute('keyDate', 'yes'); |
||
| 149 | $newDateIssued->nodeValue = $date; |
||
| 150 | $originInfo->item(0)->appendChild($newDateIssued); |
||
| 151 | } else { |
||
| 152 | $dateIssued->item(0)->nodeValue = $date; |
||
| 153 | } |
||
| 154 | |||
| 155 | } else { |
||
| 156 | |||
| 157 | $rootNode = $this->getModsXpath()->query('/mods:mods'); |
||
| 158 | |||
| 159 | if ($rootNode->length == 1) { |
||
| 160 | $newOriginInfo = $this->modsDom->createElement('mods:originInfo'); |
||
| 161 | $newOriginInfo->setAttribute('eventType', 'distribution'); |
||
| 162 | $rootNode->item(0)->appendChild($newOriginInfo); |
||
| 163 | |||
| 164 | $newDateIssued = $this->modsDom->createElement('mods:dateIssued'); |
||
| 165 | $newDateIssued->setAttribute('encoding', 'iso8601'); |
||
| 166 | $newDateIssued->setAttribute('keyDate', 'yes'); |
||
| 167 | $newDateIssued->nodeValue = $date; |
||
| 168 | $newOriginInfo->appendChild($newDateIssued); |
||
| 169 | } else { |
||
| 170 | throw new \Exception('Invalid xml data.'); |
||
| 171 | } |
||
| 172 | |||
| 173 | } |
||
| 174 | |||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | public function getPublishingYear() |
||
| 179 | { |
||
| 180 | $year = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="publication"]/mods:dateIssued[@encoding="iso8601"]'); |
||
| 181 | if ($year->length > 0) { |
||
| 182 | return $year->item(0)->nodeValue; |
||
| 183 | } |
||
| 184 | |||
| 185 | return null; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getOriginalSourceTitle() |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getDateIssued() |
||
| 199 | { |
||
| 200 | |||
| 201 | $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]'); |
||
| 202 | if ($dateIssued->length > 0) { |
||
| 203 | return $dateIssued->item(0)->nodeValue; |
||
| 204 | } |
||
| 205 | |||
| 206 | return null; |
||
| 207 | } |
||
| 208 | |||
| 209 | public function removeDateIssued() |
||
| 210 | { |
||
| 211 | |||
| 212 | $dateIssued = $this->getModsXpath()->query('/mods:mods/mods:originInfo[@eventType="distribution"]/mods:dateIssued[@encoding="iso8601"][@keyDate="yes"]'); |
||
| 213 | if ($dateIssued->length > 0) { |
||
| 214 | $dateIssued->item(0)->parentNode->removeChild($dateIssued->item(0)); |
||
| 215 | } |
||
| 216 | |||
| 217 | } |
||
| 218 | |||
| 219 | public function hasQucosaUrn() |
||
| 220 | { |
||
| 221 | $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]'); |
||
| 222 | |||
| 223 | $hasUrn = false; |
||
| 224 | |||
| 225 | foreach ($urnNodeList as $urnNode) { |
||
| 226 | $value = $urnNode->nodeValue; |
||
| 227 | $hasUrn = $hasUrn || !empty($value); |
||
| 228 | } |
||
| 229 | |||
| 230 | return $hasUrn; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function getQucosaUrn() |
||
| 234 | { |
||
| 235 | $urnNodeList = $this->getModsXpath()->query('/mods:mods/mods:identifier[@type="qucosa:urn"]'); |
||
| 236 | $urnList = ''; |
||
| 237 | |||
| 238 | if ($urnNodeList != null) { |
||
| 239 | foreach ($urnNodeList as $urnNode) { |
||
| 240 | $urnList = $urnNode->nodeValue; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | return $urnList; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function addQucosaUrn($urn) |
||
| 257 | } |
||
| 258 | |||
| 259 | } |
||
| 260 | |||
| 261 | public function clearAllUrn() |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getSourceDetails() |
||
| 305 | } |
||
| 306 | } |
||
| 307 |