Completed
Push — master ( f7252d...955fe9 )
by André
19:56 queued 06:52
created

Indexer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 41.54 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 27
loc 65
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

1 Method

Rating   Name   Duplication   Size   Complexity  
C createSearchIndex() 27 50 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Search\Legacy\Content;
10
11
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12
use eZ\Publish\Core\Search\Common\Indexer as SearchIndexer;
13
use eZ\Publish\Core\Search\Legacy\Content\Handler as SearchHandler;
14
use PDO;
15
use RuntimeException;
16
use Symfony\Component\Console\Helper\ProgressBar;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Indexer extends SearchIndexer
20
{
21
    /**
22
     * @var \eZ\Publish\Core\Search\Legacy\Content\Handler
23
     */
24
    protected $searchHandler;
25
26
    /**
27
     * Create search engine index.
28
     *
29
     * @param \Symfony\Component\Console\Output\OutputInterface $output
30
     * @param int $iterationCount
31
     * @param bool $commit commit changes after each iteration
32
     */
33
    public function createSearchIndex(OutputInterface $output, $iterationCount, $commit)
34
    {
35
        $output->writeln('Creating Legacy Search Engine Index...');
36
37
        if (!$this->searchHandler instanceof SearchHandler) {
38
            throw new RuntimeException(sprintf('Expected to find an instance of %s, but found %s', SearchHandler::class, get_class($this->searchHandler)));
39
        }
40
41
        $stmt = $this->getContentDbFieldsStmt(['count(id)']);
42
        $totalCount = intval($stmt->fetchColumn());
43
        $stmt = $this->getContentDbFieldsStmt(['id', 'current_version']);
44
45
        $this->searchHandler->purgeIndex();
46
47
        $progress = new ProgressBar($output);
48
        $progress->start($totalCount);
49
50
        $i = 0;
51 View Code Duplication
        do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            $contentObjects = [];
53
            for ($k = 0; $k <= $iterationCount; ++$k) {
54
                if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) {
55
                    break;
56
                }
57
                try {
58
                    $contentObjects[] = $this->persistenceHandler->contentHandler()->load(
59
                        $row['id'],
60
                        $row['current_version']
61
                    );
62
                } catch (NotFoundException $e) {
63
                    $this->logWarning($progress, "Could not load current version of Content with id ${row['id']}, so skipped for indexing. Full exception: " . $e->getMessage());
64
                }
65
            }
66
67
            foreach ($contentObjects as $content) {
68
                try {
69
                    $this->searchHandler->indexContent($content);
70
                } catch (NotFoundException $e) {
71
                    // Ignore content objects that have some sort of missing data on it
72
                    $this->logWarning($progress, 'Content with id ' . $content->versionInfo->id . ' has missing data, so skipped for indexing. Full exception: ' . $e->getMessage());
73
                }
74
            }
75
76
            $progress->advance($k);
77
        } while (($i += $iterationCount) < $totalCount);
78
        $progress->finish();
79
        $output->writeln('');
80
81
        $output->writeln('Finished creating Legacy Search Engine Index');
82
    }
83
}
84