Completed
Push — master ( 0256b9...9606fb )
by Timo
09:42 queued 09:36
created

Item   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 398
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 75.19%

Importance

Changes 0
Metric Value
wmc 42
lcom 2
cbo 4
dl 0
loc 398
ccs 100
cts 133
cp 0.7519
rs 8.295
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A hasIndexingProperties() 0 4 1
A getIndexQueueUid() 0 4 1
A getRootPageUid() 0 4 1
A getMountPointIdentifier() 0 4 1
A setRootPageUid() 0 4 1
A getErrors() 0 4 1
A getSite() 0 5 1
A getType() 0 4 1
A setType() 0 4 1
A getIndexingConfigurationName() 0 4 1
A setIndexingConfigurationName() 0 4 1
A getChanged() 0 4 1
A setChanged() 0 4 1
A getRecordUid() 0 6 1
A getRecord() 0 14 2
A setRecord() 0 4 1
A getRecordPageId() 0 6 1
B __construct() 0 20 6
A storeIndexingProperties() 0 10 2
A writeIndexingProperties() 0 16 3
A hasIndexingProperty() 0 6 1
A loadIndexingProperties() 0 16 4
A setIndexingProperty() 0 19 4
A getIndexingProperty() 0 13 2
A getIndexingProperties() 0 6 1
A getIndexingPropertyKeys() 0 6 1

How to fix   Complexity   

Complex Class

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
2
namespace ApacheSolrForTypo3\Solr\IndexQueue;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\IndexQueueIndexingPropertyRepository;
28
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\QueueItemRepository;
29
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
30
use ApacheSolrForTypo3\Solr\Site;
31
use TYPO3\CMS\Backend\Utility\BackendUtility;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
/**
35
 * Representation of an index queue item, carrying meta data and the record to be
36
 * indexed.
37
 *
38
 * @todo: Loose coupling from Repos
39
 *
40
 * @author Ingo Renner <[email protected]>
41
 */
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()
313
    {
314 7
        return $this->hasIndexingProperties;
315
    }
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);
335
    }
336
337
    /**
338 7
     * @param string $key
339 7
     * @return bool
340 7
     */
341
    public function hasIndexingProperty($key)
342
    {
343
        $this->loadIndexingProperties();
344 7
345
        return array_key_exists($key, $this->indexingProperties);
346
    }
347
348
    /**
349
     * Loads the indexing properties for the item - if not already loaded.
350 7
     */
351
    public function loadIndexingProperties()
352
    {
353
        if ($this->indexingPropertiesLoaded) {
354
            return;
355
        }
356
357 7
        $indexingProperties = $this->indexQueueIndexingPropertyRepository->findAllByIndexQueueUid(intval($this->indexQueueUid));
358
        $this->indexingPropertiesLoaded = true;
359 7
        if (empty($indexingProperties)) {
360 7
            return;
361 7
        }
362
363
        foreach ($indexingProperties as $indexingProperty) {
364 7
            $this->indexingProperties[$indexingProperty['property_key']] = $indexingProperty['property_value'];
365 7
        }
366 7
    }
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)
376 7
    {
377
        // make sure to not interfere with existing indexing properties
378
        $this->loadIndexingProperties();
379
380
        $key = (string)$key; // Scalar typehints now!
381
382 1
        if (!is_string($value) && !is_int($value) && !is_float($value)) {
383
            throw new \InvalidArgumentException(
384 1
                'Cannot set indexing property "' . $key
385
                . '", its value must be string, integer or float, '
386 1
                . 'type given was "' . gettype($value) . '"',
387
                1323173209
388
            );
389
        }
390
391
        $this->indexingProperties[$key] = $value;
392
        $this->hasIndexingProperties = true;
393 8
    }
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)
403
    {
404
        $this->loadIndexingProperties();
405
406
        if (!array_key_exists($key, $this->indexingProperties)) {
407
            throw new \InvalidArgumentException(
408 8
                'No indexing property "' . $key . '".',
409
                1323174143
410 8
            );
411
        }
412
413
        return $this->indexingProperties[$key];
414
    }
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()
422
    {
423 7
        $this->loadIndexingProperties();
424
425 7
        return $this->indexingProperties;
426
    }
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()
434
    {
435
        $this->loadIndexingProperties();
436 7
437 7
        return array_keys($this->indexingProperties);
438 7
    }
439
}
440