Total Complexity | 47 |
Total Lines | 245 |
Duplicated Lines | 0 % |
Changes | 9 | ||
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 |
||
26 | class Mods implements \Kitodo\Dlf\Common\MetadataInterface |
||
27 | { |
||
28 | /** |
||
29 | * The metadata XML |
||
30 | * |
||
31 | * @var \SimpleXMLElement |
||
32 | **/ |
||
33 | private $xml; |
||
34 | |||
35 | /** |
||
36 | * The metadata array |
||
37 | * |
||
38 | * @var array |
||
39 | **/ |
||
40 | private $metadata; |
||
41 | |||
42 | /** |
||
43 | * This extracts the essential MODS metadata from XML |
||
44 | * |
||
45 | * @access public |
||
46 | * |
||
47 | * @param \SimpleXMLElement $xml: The XML to extract the metadata from |
||
48 | * @param array &$metadata: The metadata array to fill |
||
49 | * |
||
50 | * @return void |
||
51 | */ |
||
52 | public function extractMetadata(\SimpleXMLElement $xml, array &$metadata) |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Get "author" and "author_sorting". |
||
69 | * |
||
70 | * @access private |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | private function getAuthors() { |
||
75 | $authors = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="aut"]'); |
||
76 | |||
77 | // Get "author" and "author_sorting" again if that was too sophisticated. |
||
78 | if (empty($authors)) { |
||
79 | // Get all names which do not have any role term assigned and assume these are authors. |
||
80 | $authors = $this->xml->xpath('./mods:name[not(./mods:role)]'); |
||
81 | } |
||
82 | if (!empty($authors)) { |
||
83 | for ($i = 0, $j = count($authors); $i < $j; $i++) { |
||
84 | $authors[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
85 | |||
86 | $identifier = $authors[$i]->xpath('./mods:name/mods:nameIdentifier[@type="orcid"]'); |
||
87 | if (!empty((string) $identifier[0])) { |
||
88 | $this->getAuthorFromOrcidApi((string) $identifier[0], $authors, $i); |
||
89 | } else { |
||
90 | $this->getAuthorFromXml($authors, $i); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | private function getAuthorFromOrcidApi($orcidId, $authors, $i) { |
||
97 | $profile = new OrcidProfile($orcidId); |
||
98 | $name = $profile->getFullName(); |
||
99 | if (!empty($name)) { |
||
100 | $this->metadata['author'][$i] = [ |
||
101 | 'name' => $name, |
||
102 | 'url' => 'https://orcid.org/' . $orcidId |
||
103 | ]; |
||
104 | } else { |
||
105 | //fallback into display form |
||
106 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | private function getAuthorFromXml($authors, $i) { |
||
111 | $this->getAuthorFromXmlDisplayForm($authors, $i); |
||
112 | |||
113 | $nameParts = $authors[$i]->xpath('./mods:namePart'); |
||
114 | |||
115 | if (empty($this->metadata['author'][$i]) && $nameParts) { |
||
116 | $name = []; |
||
117 | $k = 4; |
||
118 | foreach ($nameParts as $namePart) { |
||
119 | if ( |
||
120 | isset($namePart['type']) |
||
121 | && (string) $namePart['type'] == 'family' |
||
122 | ) { |
||
123 | $name[0] = (string) $namePart; |
||
124 | } elseif ( |
||
125 | isset($namePart['type']) |
||
126 | && (string) $namePart['type'] == 'given' |
||
127 | ) { |
||
128 | $name[1] = (string) $namePart; |
||
129 | } elseif ( |
||
130 | isset($namePart['type']) |
||
131 | && (string) $namePart['type'] == 'termsOfAddress' |
||
132 | ) { |
||
133 | $name[2] = (string) $namePart; |
||
134 | } elseif ( |
||
135 | isset($namePart['type']) |
||
136 | && (string) $namePart['type'] == 'date' |
||
137 | ) { |
||
138 | $name[3] = (string) $namePart; |
||
139 | } else { |
||
140 | $name[$k] = (string) $namePart; |
||
141 | } |
||
142 | $k++; |
||
143 | } |
||
144 | ksort($name); |
||
145 | $this->metadata['author'][$i] = trim(implode(', ', $name)); |
||
146 | } |
||
147 | // Append "valueURI" to name using Unicode unit separator. |
||
148 | if (isset($authors[$i]['valueURI'])) { |
||
149 | $this->metadata['author'][$i] .= chr(31) . (string) $authors[$i]['valueURI']; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | private function getAuthorFromXmlDisplayForm($authors, $i) { |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get holder. |
||
162 | * |
||
163 | * @access private |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | private function getHolders() { |
||
168 | $holders = $this->xml->xpath('./mods:name[./mods:role/mods:roleTerm[@type="code" and @authority="marcrelator"]="prv"]'); |
||
169 | |||
170 | if (!empty($holders)) { |
||
171 | for ($i = 0, $j = count($holders); $i < $j; $i++) { |
||
172 | $holders[$i]->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3'); |
||
173 | |||
174 | $identifier = $holders[$i]->xpath('./mods:name/mods:nameIdentifier[@type="viaf"]'); |
||
175 | if (!empty((string) $identifier[0])) { |
||
176 | $this->getHolderFromViafApi((string) $identifier[0], $holders, $i); |
||
177 | } else { |
||
178 | $this->getHolderFromXml($holders, $i); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | private function getHolderFromViafApi($viafId, $holders, $i) { |
||
195 | } |
||
196 | } |
||
197 | |||
198 | private function getHolderFromXml($holders, $i) { |
||
199 | $this->getHolderFromXmlDisplayForm($holders, $i); |
||
200 | // Append "valueURI" to name using Unicode unit separator. |
||
201 | if (isset($holders[$i]['valueURI'])) { |
||
202 | $this->metadata['holder'][$i] .= chr(31) . (string) $holders[$i]['valueURI']; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | private function getHolderFromXmlDisplayForm($holders, $i) { |
||
207 | // Check if there is a display form. |
||
208 | $displayForm = $holders[$i]->xpath('./mods:displayForm'); |
||
209 | if ($displayForm) { |
||
210 | $this->metadata['holder'][$i] = (string) $displayForm[0]; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get "place" and "place_sorting". |
||
216 | * |
||
217 | * @access private |
||
218 | * |
||
219 | * @return void |
||
220 | */ |
||
221 | private function getPlaces() { |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get "year" and "year_sorting". |
||
240 | * |
||
241 | * @access private |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | private function getYears() { |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 |