Total Complexity | 60 |
Total Lines | 386 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like tx_dlf_metadata 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 tx_dlf_metadata, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class tx_dlf_metadata extends tx_dlf_plugin { |
||
22 | |||
23 | public $scriptRelPath = 'plugins/metadata/class.tx_dlf_metadata.php'; |
||
24 | |||
25 | /** |
||
26 | * This holds the hook objects |
||
27 | * |
||
28 | * @var array |
||
29 | * @access protected |
||
30 | */ |
||
31 | protected $hookObjects = array (); |
||
32 | |||
33 | /** |
||
34 | * The main method of the PlugIn |
||
35 | * |
||
36 | * @access public |
||
37 | * |
||
38 | * @param string $content: The PlugIn content |
||
39 | * @param array $conf: The PlugIn configuration |
||
40 | * |
||
41 | * @return string The content that is displayed on the website |
||
42 | */ |
||
43 | public function main($content, $conf) { |
||
44 | |||
45 | $this->init($conf); |
||
46 | |||
47 | // Turn cache on. |
||
48 | $this->setCache(TRUE); |
||
49 | |||
50 | // Load current document. |
||
51 | $this->loadDocument(); |
||
52 | |||
53 | if ($this->doc === NULL) { |
||
54 | |||
55 | // Quit without doing anything if required variables are not set. |
||
56 | return $content; |
||
57 | |||
58 | } else { |
||
59 | |||
60 | // Set default values if not set. |
||
61 | if (!isset($this->conf['rootline'])) { |
||
62 | |||
63 | $this->conf['rootline'] = 0; |
||
|
|||
64 | |||
65 | } |
||
66 | |||
67 | } |
||
68 | |||
69 | $metadata = array (); |
||
70 | |||
71 | if ($this->conf['rootline'] < 2) { |
||
72 | |||
73 | // Get current structure's @ID. |
||
74 | $ids = array (); |
||
75 | |||
76 | if (!empty($this->doc->physicalStructure[$this->piVars['page']]) && !empty($this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]])) { |
||
77 | |||
78 | foreach ($this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]] as $logId) { |
||
79 | |||
80 | $count = count($this->doc->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@ID="'.$logId.'"]/ancestor::*')); |
||
81 | |||
82 | $ids[$count][] = $logId; |
||
83 | |||
84 | } |
||
85 | |||
86 | } |
||
87 | |||
88 | ksort($ids); |
||
89 | |||
90 | reset($ids); |
||
91 | |||
92 | // Check if we should display all metadata up to the root. |
||
93 | if ($this->conf['rootline'] == 1) { |
||
94 | |||
95 | foreach ($ids as $id) { |
||
96 | |||
97 | foreach ($id as $sid) { |
||
98 | |||
99 | $data = $this->doc->getMetadata($sid, $this->conf['pages']); |
||
100 | |||
101 | if (!empty($data)) { |
||
102 | |||
103 | $data['_id'] = $sid; |
||
104 | |||
105 | $metadata[] = $data; |
||
106 | |||
107 | } |
||
108 | |||
109 | } |
||
110 | |||
111 | } |
||
112 | |||
113 | } else { |
||
114 | |||
115 | $id = array_pop($ids); |
||
116 | |||
117 | if (is_array($id)) { |
||
118 | |||
119 | foreach ($id as $sid) { |
||
120 | |||
121 | $data = $this->doc->getMetadata($sid, $this->conf['pages']); |
||
122 | |||
123 | if (!empty($data)) { |
||
124 | |||
125 | $data['_id'] = $sid; |
||
126 | |||
127 | $metadata[] = $data; |
||
128 | |||
129 | } |
||
130 | |||
131 | } |
||
132 | |||
133 | } |
||
134 | |||
135 | } |
||
136 | |||
137 | } |
||
138 | |||
139 | // Get titledata? |
||
140 | if (empty($metadata) || ($this->conf['rootline'] == 1 && $metadata[0]['_id'] != $this->doc->toplevelId)) { |
||
141 | |||
142 | $data = $this->doc->getTitleData($this->conf['pages']); |
||
143 | |||
144 | $data['_id'] = $this->doc->toplevelId; |
||
145 | |||
146 | array_unshift($metadata, $data); |
||
147 | |||
148 | } |
||
149 | |||
150 | if (empty($metadata)) { |
||
151 | |||
152 | if (TYPO3_DLOG) { |
||
153 | |||
154 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_metadata->main('.$content.', [data])] No metadata found for document with UID "'.$this->doc->uid.'"', $this->extKey, SYSLOG_SEVERITY_WARNING, $conf); |
||
155 | |||
156 | } |
||
157 | |||
158 | return $content; |
||
159 | |||
160 | } |
||
161 | |||
162 | ksort($metadata); |
||
163 | |||
164 | // Get hook objects. |
||
165 | $this->hookObjects = tx_dlf_helper::getHookObjects($this->scriptRelPath); |
||
166 | |||
167 | // Hook for getting a customized title bar (requested by SBB). |
||
168 | foreach ($this->hookObjects as $hookObj) { |
||
169 | |||
170 | if (method_exists($hookObj, 'main_customizeTitleBarGetCustomTemplate')) { |
||
171 | |||
172 | $hookObj->main_customizeTitleBarGetCustomTemplate($this, $metadata); |
||
173 | |||
174 | } |
||
175 | |||
176 | } |
||
177 | |||
178 | $content .= $this->printMetadata($metadata); |
||
179 | |||
180 | return $this->pi_wrapInBaseClass($content); |
||
181 | |||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Prepares the metadata array for output |
||
186 | * |
||
187 | * @access protected |
||
188 | * |
||
189 | * @param array $metadataArray: The metadata array |
||
190 | * |
||
191 | * @return string The metadata array ready for output |
||
192 | */ |
||
193 | protected function printMetadata(array $metadataArray) { |
||
407 | |||
408 | } |
||
411 |