Complex classes like Item 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Item, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class Item |
||
43 | { |
||
44 | /** |
||
45 | * The item's uid in the index queue (tx_solr_indexqueue_item.uid) |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $indexQueueUid; |
||
50 | |||
51 | /** |
||
52 | * The root page uid of the tree the item is located in (tx_solr_indexqueue_item.root) |
||
53 | * |
||
54 | * @var int |
||
55 | */ |
||
56 | protected $rootPageUid; |
||
57 | |||
58 | /** |
||
59 | * The record's type, usually a table name, but could also be a file type (tx_solr_indexqueue_item.item_type) |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $type; |
||
64 | |||
65 | /** |
||
66 | * The name of the indexing configuration that should be used when indexing (tx_solr_indexqueue_item.indexing_configuration) |
||
67 | * the item. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $indexingConfigurationName; |
||
72 | |||
73 | /** |
||
74 | * The unix timestamp when the record was last changed (tx_solr_indexqueue_item.changed) |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | protected $changed; |
||
79 | |||
80 | /** |
||
81 | * Indexing properties to provide additional information for the item's |
||
82 | * indexer / how to index the item. |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $indexingProperties = []; |
||
87 | |||
88 | /** |
||
89 | * Flag for lazy loading indexing properties. |
||
90 | * |
||
91 | * @var bool |
||
92 | */ |
||
93 | protected $indexingPropertiesLoaded = false; |
||
94 | |||
95 | /** |
||
96 | * Flag, whether indexing properties exits for this item. |
||
97 | * |
||
98 | * @var bool |
||
99 | */ |
||
100 | protected $hasIndexingProperties = false; |
||
101 | |||
102 | /** |
||
103 | * The record's uid. |
||
104 | * |
||
105 | * @var int |
||
106 | */ |
||
107 | protected $recordUid = 0; |
||
108 | |||
109 | /** |
||
110 | * The record itself |
||
111 | * |
||
112 | * @var array |
||
113 | */ |
||
114 | protected $record; |
||
115 | |||
116 | /** |
||
117 | * Moint point identifier. |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | protected $mountPointIdentifier; |
||
122 | |||
123 | /** |
||
124 | * @var string |
||
125 | */ |
||
126 | protected $errors = ''; |
||
127 | |||
128 | /** |
||
129 | * @var IndexQueueIndexingPropertyRepository |
||
130 | */ |
||
131 | 64 | protected $indexQueueIndexingPropertyRepository; |
|
132 | |||
133 | /** |
||
134 | * @var QueueItemRepository |
||
135 | 64 | */ |
|
136 | 64 | protected $queueItemRepository; |
|
137 | 64 | ||
138 | 64 | /** |
|
139 | 64 | * Constructor, takes item meta data information and resolves that to the full record. |
|
140 | 64 | * |
|
141 | 64 | * @param array $itemMetaData Metadata describing the item to index using the index queue. Is expected to contain a record from table tx_solr_indexqueue_item |
|
142 | * @param array $fullRecord Optional full record for the item. If provided, can save some SQL queries. |
||
143 | 64 | * @param IndexQueueIndexingPropertyRepository|null $indexQueueIndexingPropertyRepository |
|
144 | 64 | */ |
|
145 | public function __construct(array $itemMetaData, array $fullRecord = [], IndexQueueIndexingPropertyRepository $indexQueueIndexingPropertyRepository = null, QueueItemRepository $queueItemRepository = null) |
||
146 | 64 | { |
|
147 | 34 | $this->indexQueueUid = $itemMetaData['uid']; |
|
148 | $this->rootPageUid = $itemMetaData['root']; |
||
149 | 64 | $this->type = $itemMetaData['item_type']; |
|
150 | $this->recordUid = $itemMetaData['item_uid']; |
||
151 | $this->mountPointIdentifier = (string) empty($itemMetaData['pages_mountidentifier']) ? '' : $itemMetaData['pages_mountidentifier']; |
||
152 | $this->changed = $itemMetaData['changed']; |
||
153 | $this->errors = (string) empty($itemMetaData['errors']) ? '' : $itemMetaData['errors']; |
||
154 | 8 | ||
155 | $this->indexingConfigurationName = $itemMetaData['indexing_configuration']; |
||
156 | 8 | $this->hasIndexingProperties = (boolean)$itemMetaData['has_indexing_properties']; |
|
157 | |||
158 | if (!empty($fullRecord)) { |
||
159 | $this->record = $fullRecord; |
||
160 | } |
||
161 | |||
162 | $this->indexQueueIndexingPropertyRepository = isset($indexQueueIndexingPropertyRepository) ? $indexQueueIndexingPropertyRepository : GeneralUtility::makeInstance(IndexQueueIndexingPropertyRepository::class); |
||
163 | $this->queueItemRepository = isset($queueItemRepository) ? $queueItemRepository : GeneralUtility::makeInstance(QueueItemRepository::class); |
||
164 | 27 | } |
|
165 | |||
166 | 27 | /** |
|
167 | * Getter for Index Queue UID |
||
168 | * |
||
169 | * @return integer |
||
170 | */ |
||
171 | public function getIndexQueueUid() |
||
172 | { |
||
173 | return $this->indexQueueUid; |
||
174 | 21 | } |
|
175 | |||
176 | 21 | /** |
|
177 | * Gets the item's root page ID (uid) |
||
178 | * |
||
179 | * @return int root page ID |
||
180 | */ |
||
181 | public function getRootPageUid() |
||
182 | { |
||
183 | return $this->rootPageUid; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Returns mount point identifier |
||
188 | * |
||
189 | * @return string |
||
190 | 5 | */ |
|
191 | public function getMountPointIdentifier() |
||
192 | 5 | { |
|
193 | return $this->mountPointIdentifier; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param integer $uid |
||
198 | */ |
||
199 | public function setRootPageUid($uid) |
||
200 | 27 | { |
|
201 | $this->rootPageUid = intval($uid); |
||
202 | 27 | } |
|
203 | 27 | ||
204 | /** |
||
205 | * @return string |
||
206 | 23 | */ |
|
207 | public function getErrors() |
||
208 | 23 | { |
|
209 | return $this->errors; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Gets the site the item belongs to. |
||
214 | * |
||
215 | * @return Site Site instance the item belongs to. |
||
216 | 32 | */ |
|
217 | public function getSite() |
||
218 | 32 | { |
|
219 | $siteRepository = GeneralUtility::makeInstance(SiteRepository::class); |
||
220 | return $siteRepository->getSiteByRootPageId($this->rootPageUid); |
||
221 | } |
||
222 | |||
223 | public function getType() |
||
224 | { |
||
225 | return $this->type; |
||
226 | } |
||
227 | |||
228 | public function setType($type) |
||
229 | { |
||
230 | $this->type = $type; |
||
231 | } |
||
232 | |||
233 | public function getIndexingConfigurationName() |
||
234 | { |
||
235 | return $this->indexingConfigurationName; |
||
236 | 5 | } |
|
237 | |||
238 | 5 | public function setIndexingConfigurationName($indexingConfigurationName) |
|
239 | { |
||
240 | 5 | $this->indexingConfigurationName = $indexingConfigurationName; |
|
241 | } |
||
242 | |||
243 | public function getChanged() |
||
244 | { |
||
245 | return $this->changed; |
||
246 | } |
||
247 | |||
248 | public function setChanged($changed) |
||
249 | { |
||
250 | 20 | $this->changed = intval($changed); |
|
251 | } |
||
252 | 20 | ||
253 | 1 | public function getRecordUid() |
|
254 | 1 | { |
|
255 | 1 | $this->getRecord(); |
|
256 | 1 | ||
257 | 1 | return $this->record['uid']; |
|
258 | 1 | } |
|
259 | |||
260 | /** |
||
261 | * Gets the item's full record. |
||
262 | 20 | * |
|
263 | * Uses lazy loading. |
||
264 | * |
||
265 | * @return array The item's DB record. |
||
266 | */ |
||
267 | public function getRecord() |
||
268 | { |
||
269 | if (empty($this->record)) { |
||
270 | $this->record = (array)BackendUtility::getRecord( |
||
271 | $this->type, |
||
272 | $this->recordUid, |
||
273 | 17 | '*', |
|
274 | '', |
||
275 | 17 | false |
|
276 | ); |
||
277 | 17 | } |
|
278 | |||
279 | return $this->record; |
||
280 | } |
||
281 | |||
282 | public function setRecord(array $record) |
||
283 | { |
||
284 | 7 | $this->record = $record; |
|
285 | } |
||
286 | 7 | ||
287 | /** |
||
288 | 7 | * @return int |
|
289 | 7 | */ |
|
290 | public function getRecordPageId() |
||
291 | { |
||
292 | 7 | $this->getRecord(); |
|
293 | 7 | ||
294 | return $this->record['pid']; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Stores the indexing properties. |
||
299 | * |
||
300 | 7 | */ |
|
301 | public function storeIndexingProperties() |
||
302 | 7 | { |
|
303 | 7 | $this->indexQueueIndexingPropertyRepository->removeByRootPidAndIndexQueueUid(intval($this->rootPageUid), intval($this->indexQueueUid)); |
|
304 | 7 | ||
305 | 7 | if ($this->hasIndexingProperties()) { |
|
306 | $this->writeIndexingProperties(); |
||
307 | } |
||
308 | 7 | ||
309 | $this->queueItemRepository->updateHasIndexingPropertiesFlagByItemUid($this->indexQueueUid, $this->hasIndexingProperties); |
||
310 | } |
||
311 | |||
312 | public function hasIndexingProperties() |
||
316 | 7 | ||
317 | /** |
||
318 | 7 | * Writes all indexing properties. |
|
319 | */ |
||
320 | protected function writeIndexingProperties() |
||
321 | { |
||
322 | $properties = []; |
||
323 | foreach ($this->indexingProperties as $propertyKey => $propertyValue) { |
||
324 | $properties[] = [ |
||
325 | 'root' => $this->rootPageUid, |
||
326 | 7 | 'item_id' => $this->indexQueueUid, |
|
327 | 'property_key' => $propertyKey, |
||
328 | 7 | 'property_value' => $propertyValue |
|
329 | 7 | ]; |
|
330 | 7 | } |
|
331 | 7 | if (empty($properties)) { |
|
332 | 7 | return; |
|
333 | 7 | } |
|
334 | 7 | $this->indexQueueIndexingPropertyRepository->bulkInsert($properties); |
|
336 | |||
337 | /** |
||
338 | 7 | * @param string $key |
|
339 | 7 | * @return bool |
|
340 | 7 | */ |
|
341 | public function hasIndexingProperty($key) |
||
347 | |||
348 | /** |
||
349 | * Loads the indexing properties for the item - if not already loaded. |
||
350 | 7 | */ |
|
351 | public function loadIndexingProperties() |
||
367 | 7 | ||
368 | /** |
||
369 | * Sets an indexing property for the item. |
||
370 | 7 | * |
|
371 | * @param string $key Indexing property name |
||
372 | * @param string|int|float $value Indexing property value |
||
373 | * @throws \InvalidArgumentException when $value is not string, integer or float |
||
374 | */ |
||
375 | public function setIndexingProperty($key, $value) |
||
394 | |||
395 | 8 | /** |
|
396 | 8 | * Gets a specific indexing property by its name/key. |
|
397 | 8 | * |
|
398 | 8 | * @param string $key Indexing property name/key. |
|
399 | 8 | * @throws \InvalidArgumentException when the given $key does not exist. |
|
400 | * @return string |
||
401 | */ |
||
402 | 8 | public function getIndexingProperty($key) |
|
415 | |||
416 | /** |
||
417 | * Gets all indexing properties set for this item. |
||
418 | * |
||
419 | 7 | * @return array Array of indexing properties. |
|
420 | */ |
||
421 | public function getIndexingProperties() |
||
427 | 7 | ||
428 | /** |
||
429 | * Gets the names/keys of the item's indexing properties. |
||
430 | * |
||
431 | * @return array Array of indexing property names/keys |
||
432 | */ |
||
433 | public function getIndexingPropertyKeys() |
||
439 | } |
||
440 |