Total Complexity | 93 |
Total Lines | 512 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CcVersion1 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 CcVersion1, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class CcVersion1 extends CcVersionBase |
||
8 | { |
||
9 | const WEBCONTENT = 'webcontent'; |
||
10 | const QUESTIONBANK = 'imsqti_xmlv1p2/imscc_xmlv1p0/question-bank'; |
||
11 | const ASSESSMENT = 'imsqti_xmlv1p2/imscc_xmlv1p0/assessment'; |
||
12 | const ASSOCIATEDCONTENT = 'associatedcontent/imscc_xmlv1p0/learning-application-resource'; |
||
13 | const DISCUSSIONTOPIC = 'imsdt_xmlv1p0'; |
||
14 | const WEBLINK = 'imswl_xmlv1p0'; |
||
15 | |||
16 | public static $checker = [self::WEBCONTENT, |
||
17 | self::ASSESSMENT, |
||
18 | self::ASSOCIATEDCONTENT, |
||
19 | self::DISCUSSIONTOPIC, |
||
20 | self::QUESTIONBANK, |
||
21 | self::WEBLINK, ]; |
||
22 | |||
23 | public function __construct() |
||
24 | { |
||
25 | $this->ccnamespaces = ['imscc' => 'http://www.imsglobal.org/xsd/imscc/imscp_v1p1', |
||
26 | 'lomimscc' => 'http://ltsc.ieee.org/xsd/imscc/LOM', |
||
27 | 'lom' => 'http://ltsc.ieee.org/xsd/LOM', |
||
28 | 'voc' => 'http://ltsc.ieee.org/xsd/LOM/vocab', |
||
29 | 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
||
30 | ]; |
||
31 | |||
32 | $this->ccnsnames = [ |
||
33 | 'imscc' => 'http://www.imsglobal.org/profile/cc/ccv1p0/derived_schema/imscp_v1p2_localised.xsd', |
||
34 | 'lom' => 'http://www.imsglobal.org/profile/cc/ccv1p0/derived_schema/domainProfile_2/lomLoose_localised.xsd', |
||
35 | 'lomimscc' => 'http://www.imsglobal.org/profile/cc/ccv1p0/derived_schema/domainProfile_1/lomLoose_localised.xsd', |
||
36 | 'voc' => 'http://www.imsglobal.org/profile/cc/ccv1p0/derived_schema/domainProfile_2/vocab/loose.xsd', |
||
37 | ]; |
||
38 | |||
39 | $this->ccversion = '1.0.0'; |
||
40 | $this->camversion = '1.0.0'; |
||
41 | $this->_generator = 'Common Cartridge generator'; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Validate if the type are valid or not. |
||
46 | * |
||
47 | * @param string $type |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function valid($type) |
||
52 | { |
||
53 | return in_array($type, self::$checker); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Create Education Metadata (How To). |
||
58 | * |
||
59 | * @param object $met |
||
60 | * @param object $xmlnode |
||
61 | * |
||
62 | * @return DOMNode |
||
63 | */ |
||
64 | public function createMetadataEducational($met, DOMDocument &$doc, $xmlnode) |
||
65 | { |
||
66 | $nd = $doc->createElementNS($this->ccnamespaces['lom'], 'educational'); |
||
67 | $nd2 = $doc->createElementNS($this->ccnamespaces['lom'], 'intendedEndUserRole'); |
||
68 | $nd3 = $doc->createElementNS($this->ccnamespaces['voc'], 'vocabulary'); |
||
69 | |||
70 | $xmlnode->appendChild($nd); |
||
71 | $nd->appendChild($nd2); |
||
72 | $nd2->appendChild($nd3); |
||
73 | |||
74 | foreach ($met->arrayeducational as $name => $value) { |
||
75 | !is_array($value) ? $value = [$value] : null; |
||
76 | foreach ($value as $v) { |
||
77 | $nd4 = $doc->createElementNS($this->ccnamespaces['voc'], $name, $v[0]); |
||
78 | $nd3->appendChild($nd4); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | return $nd; |
||
83 | } |
||
84 | |||
85 | protected function onCreate(DOMDocument &$doc, $rootmanifestnode = null, $nmanifestID = null) |
||
142 | } |
||
143 | |||
144 | protected function updateAttribute(DOMDocument &$doc, $attrname, $attrvalue, DOMElement &$node) |
||
145 | { |
||
146 | $busenew = (is_object($node) && $node->hasAttribute($attrname)); |
||
147 | $nResult = null; |
||
148 | if (!$busenew && is_null($attrvalue)) { |
||
149 | $node->removeAttribute($attrname); |
||
150 | } else { |
||
151 | $nResult = $busenew ? $node->getAttributeNode($attrname) : $doc->createAttribute($attrname); |
||
152 | $nResult->nodeValue = $attrvalue; |
||
153 | if (!$busenew) { |
||
154 | $node->appendChild($nResult); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | return $nResult; |
||
159 | } |
||
160 | |||
161 | protected function updateAttributeNs(DOMDocument &$doc, $attrname, $attrnamespace, $attrvalue, DOMElement &$node) |
||
162 | { |
||
163 | $busenew = (is_object($node) && $node->hasAttributeNS($attrnamespace, $attrname)); |
||
164 | $nResult = null; |
||
165 | if (!$busenew && is_null($attrvalue)) { |
||
166 | $node->removeAttributeNS($attrnamespace, $attrname); |
||
167 | } else { |
||
168 | $nResult = $busenew ? $node->getAttributeNodeNS($attrnamespace, $attrname) : |
||
169 | $doc->createAttributeNS($attrnamespace, $attrname); |
||
170 | $nResult->nodeValue = $attrvalue; |
||
171 | if (!$busenew) { |
||
172 | $node->appendChild($nResult); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return $nResult; |
||
177 | } |
||
178 | |||
179 | protected function getChildNode(DOMDocument &$doc, $itemname, DOMElement &$node) |
||
180 | { |
||
181 | $nlist = $node->getElementsByTagName($itemname); |
||
182 | $item = is_object($nlist) && ($nlist->length > 0) ? $nlist->item(0) : null; |
||
183 | |||
184 | return $item; |
||
185 | } |
||
186 | |||
187 | protected function updateChildItem(DOMDocument &$doc, $itemname, $itemvalue, DOMElement &$node, $attrtostore = null) |
||
188 | { |
||
189 | $tnode = $this->getChildNode($doc, 'title', $node); |
||
190 | $usenew = is_null($tnode); |
||
191 | $tnode = $usenew ? $doc->createElementNS($this->ccnamespaces['imscc'], $itemname) : $tnode; |
||
192 | if (!is_null($attrtostore)) { |
||
193 | foreach ($attrtostore as $key => $value) { |
||
194 | $this->updateAttribute($doc, $key, $value, $tnode); |
||
195 | } |
||
196 | } |
||
197 | $tnode->nodeValue = $itemvalue; |
||
198 | if ($usenew) { |
||
199 | $node->appendChild($tnode); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | protected function updateItems($items, DOMDocument &$doc, DOMElement &$xmlnode) |
||
204 | { |
||
205 | foreach ($items as $key => $item) { |
||
206 | $itemnode = $doc->createElementNS($this->ccnamespaces['imscc'], 'item'); |
||
207 | $this->updateAttribute($doc, 'identifier', $key, $itemnode); |
||
208 | $this->updateAttribute($doc, 'identifierref', $item->identifierref, $itemnode); |
||
209 | $this->updateAttribute($doc, 'parameters', $item->parameters, $itemnode); |
||
210 | if (!empty($item->title)) { |
||
211 | $titlenode = $doc->createElementNS($this->ccnamespaces['imscc'], |
||
212 | 'title', |
||
213 | $item->title); |
||
214 | $itemnode->appendChild($titlenode); |
||
215 | } |
||
216 | if ($item->hasChildItems()) { |
||
217 | $this->updateItems($item->childitems, $doc, $itemnode); |
||
218 | } |
||
219 | $xmlnode->appendChild($itemnode); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Create a Resource (How to). |
||
225 | * |
||
226 | * @param CcIResource $res |
||
227 | * @param DOMDocument $doc |
||
228 | * @param object $xmlnode |
||
229 | * |
||
230 | * @return DOMNode |
||
231 | */ |
||
232 | protected function createResource(CcIResource &$res, DOMDocument &$doc, $xmlnode = null) |
||
233 | { |
||
234 | $usenew = is_object($xmlnode); |
||
235 | $dnode = $usenew ? $xmlnode : $doc->createElementNS($this->ccnamespaces['imscc'], "resource"); |
||
236 | $this->updateAttribute($doc, 'identifier', $res->identifier, $dnode); |
||
|
|||
237 | $this->updateAttribute($doc, 'type', $res->type, $dnode); |
||
238 | !is_null($res->href) ? $this->updateAttribute($doc, 'href', $res->href, $dnode) : null; |
||
239 | $this->updateAttribute($doc, 'base', $res->base, $dnode); |
||
240 | |||
241 | foreach ($res->files as $file) { |
||
242 | $nd = $doc->createElementNS($this->ccnamespaces['imscc'], 'file'); |
||
243 | $ndatt = $doc->createAttribute('href'); |
||
244 | $ndatt->nodeValue = $file; |
||
245 | $nd->appendChild($ndatt); |
||
246 | $dnode->appendChild($nd); |
||
247 | } |
||
248 | $this->resources[$res->identifier] = $res; |
||
249 | $this->resourcesInd[$res->files[0]] = $res->identifier; |
||
250 | |||
251 | foreach ($res->dependency as $dependency) { |
||
252 | $nd = $doc->createElementNS($this->ccnamespaces['imscc'], 'dependency'); |
||
253 | $ndatt = $doc->createAttribute('identifierref'); |
||
254 | $ndatt->nodeValue = $dependency; |
||
255 | $nd->appendChild($ndatt); |
||
256 | $dnode->appendChild($nd); |
||
257 | } |
||
258 | |||
259 | return $dnode; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Create an Item Folder (How To). |
||
264 | * |
||
265 | * @param CcIOrganization $org |
||
266 | * @param DOMDocument $doc |
||
267 | * @param DOMElement $xmlnode |
||
268 | */ |
||
269 | protected function createItemFolder(CcIOrganization &$org, DOMDocument &$doc, DOMElement &$xmlnode = null) |
||
270 | { |
||
271 | $itemfoldernode = $doc->createElementNS($this->ccnamespaces['imscc'], 'item'); |
||
272 | $this->updateAttribute($doc, 'identifier', "root", $itemfoldernode); |
||
273 | |||
274 | if ($org->hasItems()) { |
||
275 | $this->updateItems($org->itemlist, $doc, $itemfoldernode); |
||
276 | } |
||
277 | if (is_null($this->organizations)) { |
||
278 | $this->organizations = []; |
||
279 | } |
||
280 | $this->organizations[$org->identifier] = $org; |
||
281 | |||
282 | $xmlnode->appendChild($itemfoldernode); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Create an Organization (How To). |
||
287 | * |
||
288 | * @param CcIOrganization $org |
||
289 | * @param DOMDocument $doc |
||
290 | * @param object $xmlnode |
||
291 | * |
||
292 | * @return DOMNode |
||
293 | */ |
||
294 | protected function createOrganization(CcIOrganization &$org, DOMDocument &$doc, $xmlnode = null) |
||
295 | { |
||
296 | $usenew = is_object($xmlnode); |
||
297 | $dnode = $usenew ? $xmlnode : $doc->createElementNS($this->ccnamespaces['imscc'], "organization"); |
||
298 | $this->updateAttribute($doc, 'identifier', $org->identifier, $dnode); |
||
299 | $this->updateAttribute($doc, 'structure', $org->structure, $dnode); |
||
300 | |||
301 | $this->createItemFolder($org, $doc, $dnode); |
||
302 | |||
303 | return $dnode; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Create Metadata For Manifest (How To). |
||
308 | * |
||
309 | * @param CcIMetadataManifest $met |
||
310 | * @param DOMDocument $doc |
||
311 | * @param object $xmlnode |
||
312 | * |
||
313 | * @return DOMNode |
||
314 | */ |
||
315 | protected function createMetadataManifest(CcIMetadataManifest $met, DOMDocument &$doc, $xmlnode = null) |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Create Metadata For Resource (How To). |
||
336 | * |
||
337 | * @param CcIMetadataResource $met |
||
338 | * @param DOMDocument $doc |
||
339 | * @param object $xmlnode |
||
340 | * |
||
341 | * @return DOMNode |
||
342 | */ |
||
343 | protected function createMetadataResource(CcIMetadataResource $met, DOMDocument &$doc, $xmlnode = null) |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Create Metadata For File (How To). |
||
355 | * |
||
356 | * @param CcIMetadataFile $met |
||
357 | * @param DOMDocument $doc |
||
358 | * @param object $xmlnode |
||
359 | * |
||
360 | * @return DOMNode |
||
361 | */ |
||
362 | protected function createMetadataFile(CcIMetadataFile $met, DOMDocument &$doc, $xmlnode = null) |
||
363 | { |
||
364 | $dnode = $doc->createElementNS($this->ccnamespaces['lom'], "lom"); |
||
365 | |||
366 | !empty($xmlnode) ? $xmlnode->appendChild($dnode) : null; |
||
367 | !empty($met->arrayeducational) ? $this->createMetadataEducational($met, $doc, $dnode) : null; |
||
368 | |||
369 | return $dnode; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Create General Metadata (How To). |
||
374 | * |
||
375 | * @param object $met |
||
376 | * @param object $xmlnode |
||
377 | * |
||
378 | * @return DOMNode |
||
379 | */ |
||
380 | protected function createMetadataGeneral($met, DOMDocument &$doc, $xmlnode) |
||
381 | { |
||
382 | $nd = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'general'); |
||
383 | |||
384 | foreach ($met->arraygeneral as $name => $value) { |
||
385 | !is_array($value) ? $value = [$value] : null; |
||
386 | foreach ($value as $v) { |
||
387 | if ($name != 'language' && $name != 'catalog' && $name != 'entry') { |
||
388 | $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'], $name); |
||
389 | $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'string', $v[1]); |
||
390 | $ndatt = $doc->createAttribute('language'); |
||
391 | $ndatt->nodeValue = $v[0]; |
||
392 | $nd3->appendChild($ndatt); |
||
393 | $nd2->appendChild($nd3); |
||
394 | $nd->appendChild($nd2); |
||
395 | } else { |
||
396 | if ($name == 'language') { |
||
397 | $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'], $name, $v[0]); |
||
398 | $nd->appendChild($nd2); |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | } |
||
403 | if (!empty($met->arraygeneral['catalog']) || !empty($met->arraygeneral['entry'])) { |
||
404 | $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'identifier'); |
||
405 | $nd->appendChild($nd2); |
||
406 | if (!empty($met->arraygeneral['catalog'])) { |
||
407 | $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'catalog', $met->arraygeneral['catalog'][0][0]); |
||
408 | $nd2->appendChild($nd3); |
||
409 | } |
||
410 | if (!empty($met->arraygeneral['entry'])) { |
||
411 | $nd4 = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'entry', $met->arraygeneral['entry'][0][0]); |
||
412 | $nd2->appendChild($nd4); |
||
413 | } |
||
414 | } |
||
415 | |||
416 | return $nd; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Create Technical Metadata (How To). |
||
421 | * |
||
422 | * @param object $met |
||
423 | * @param object $xmlnode |
||
424 | * |
||
425 | * @return DOMNode |
||
426 | */ |
||
427 | protected function createMetadataTechnical($met, DOMDocument &$doc, $xmlnode) |
||
428 | { |
||
429 | $nd = $doc->createElementNS($this->ccnamespaces['lomimscc'], 'technical'); |
||
430 | $xmlnode->appendChild($nd); |
||
431 | |||
432 | foreach ($met->arraytech as $name => $value) { |
||
433 | !is_array($value) ? $value = [$value] : null; |
||
434 | foreach ($value as $v) { |
||
435 | $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'], $name, $v[0]); |
||
436 | $nd->appendChild($nd2); |
||
437 | } |
||
438 | } |
||
439 | |||
440 | return $nd; |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Create Rights Metadata (How To). |
||
445 | * |
||
446 | * @param object $met |
||
447 | * @param object $xmlnode |
||
448 | * |
||
449 | * @return DOMNode |
||
450 | */ |
||
451 | protected function createMetadataRights($met, DOMDocument &$doc, $xmlnode) |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * Create Lifecycle Metadata (How To). |
||
480 | * |
||
481 | * @param object $met |
||
482 | * @param DOMDocument $doc |
||
483 | * @param object $met |
||
484 | * @param object $xmlnode |
||
485 | * |
||
486 | * @return DOMNode |
||
487 | */ |
||
488 | protected function createMetadataLifecycle($met, DOMDocument &$doc, $xmlnode) |
||
519 | } |
||
520 | } |
||
521 |