Total Complexity | 47 |
Total Lines | 247 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like XMLDataFormatter 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 XMLDataFormatter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class XMLDataFormatter extends DataFormatter |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @config |
||
23 | * @todo pass this from the API to the data formatter somehow |
||
24 | */ |
||
25 | private static $api_base = "api/v1/"; |
||
26 | |||
27 | protected $outputContentType = 'text/xml'; |
||
28 | |||
29 | /** |
||
30 | * @return array |
||
31 | */ |
||
32 | public function supportedExtensions() |
||
33 | { |
||
34 | return array( |
||
35 | 'xml' |
||
36 | ); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return array |
||
41 | */ |
||
42 | public function supportedMimeTypes() |
||
43 | { |
||
44 | return array( |
||
45 | 'text/xml', |
||
46 | 'application/xml', |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param $array |
||
52 | * @return string |
||
53 | * @throws \Exception |
||
54 | */ |
||
55 | public function convertArray($array) |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param $array |
||
68 | * @return string |
||
69 | * @throws \Exception |
||
70 | */ |
||
71 | public function convertArrayWithoutHeader($array) |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Generate an XML representation of the given {@link DataObject}. |
||
94 | * |
||
95 | * @param DataObject $obj |
||
96 | * @param $includeHeader Include <?xml ...?> header (Default: true) |
||
97 | * @return String XML |
||
98 | */ |
||
99 | public function convertDataObject(DataObjectInterface $obj, $fields = null) |
||
100 | { |
||
101 | $response = Controller::curr()->getResponse(); |
||
102 | if ($response) { |
||
103 | $response->addHeader("Content-Type", "text/xml"); |
||
104 | } |
||
105 | |||
106 | return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . $this->convertDataObjectWithoutHeader($obj, $fields); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param DataObject $obj |
||
111 | * @param null $fields |
||
112 | * @param null $relations |
||
113 | * @return string |
||
114 | */ |
||
115 | public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null, $relations = null) |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Generate an XML representation of the given {@link SS_List}. |
||
238 | * |
||
239 | * @param SS_List $set |
||
240 | * @return String XML |
||
241 | */ |
||
242 | public function convertDataObjectSet(SS_List $set, $fields = null) |
||
243 | { |
||
244 | Controller::curr()->getResponse()->addHeader("Content-Type", "text/xml"); |
||
245 | $className = $this->sanitiseClassName(get_class($set)); |
||
246 | |||
247 | $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
||
248 | $xml .= (is_numeric($this->totalSize)) ? "<$className totalSize=\"{$this->totalSize}\">\n" : "<$className>\n"; |
||
249 | foreach ($set as $item) { |
||
250 | $xml .= $this->convertDataObjectWithoutHeader($item, $fields); |
||
251 | } |
||
252 | $xml .= "</$className>"; |
||
253 | |||
254 | return $xml; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param string $strData |
||
259 | * @return array|void |
||
260 | * @throws \Exception |
||
261 | */ |
||
262 | public function convertStringToArray($strData) |
||
265 | } |
||
266 | } |
||
267 |