Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

ElasticsearchCreateIndexCommand::execute()   C

Complexity

Conditions 9
Paths 36

Size

Total Lines 121

Duplication

Lines 37
Ratio 30.58 %

Importance

Changes 0
Metric Value
cc 9
nc 36
nop 2
dl 37
loc 121
rs 6.4444
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the ElasticsearchCreateIndexCommand class.
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\Bundle\EzPublishElasticsearchSearchEngineBundle\Command;
10
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Input\InputArgument;
15
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
16
use PDO;
17
18
/**
19
 * @deprecated since 6.7, use ezplatform:reindex command instead.
20
 */
21
class ElasticsearchCreateIndexCommand extends ContainerAwareCommand
22
{
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('ezplatform:elasticsearch_create_index')
27
            ->setDescription('Indexes the configured database in configured Elasticsearch index')
28
            ->addArgument('bulk_count', InputArgument::OPTIONAL, 'Number of Content objects indexed at once', 5)
29
            ->setHelp(
30
                <<<EOT
31
The command <info>%command.name%</info> indexes current configured database in configured Elasticsearch index.
32
EOT
33
            );
34
    }
35
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
            sprintf('%s is deprecated since 6.7. Use ezplatform:reindex command instead', $this->getName()),
40
            E_USER_DEPRECATED
41
        );
42
43
        $bulkCount = $input->getArgument('bulk_count');
44
45
        /** @var \eZ\Publish\SPI\Persistence\Handler $persistenceHandler */
46
        $persistenceHandler = $this->getContainer()->get('ezpublish.api.persistence_handler');
47
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandler */
48
        $searchHandler = $this->getContainer()->get('ezpublish.spi.search');
49
        /** @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler $databaseHandler */
50
        $databaseHandler = $this->getContainer()->get('ezpublish.connection');
51
52
        // Indexing Content
53
        $query = $databaseHandler->createSelectQuery();
54
        $query->select('count(id)')
55
            ->from('ezcontentobject')
56
            ->where($query->expr->eq('status', ContentInfo::STATUS_PUBLISHED));
57
        $stmt = $query->prepare();
58
        $stmt->execute();
59
        $totalCount = $stmt->fetchColumn();
60
61
        $query = $databaseHandler->createSelectQuery();
62
        $query->select('id', 'current_version')
63
            ->from('ezcontentobject')
64
            ->where($query->expr->eq('status', ContentInfo::STATUS_PUBLISHED));
65
66
        $stmt = $query->prepare();
67
        $stmt->execute();
68
69
        /** @var \eZ\Publish\Core\Search\Elasticsearch\Content\Handler $searchHandler */
70
        $searchHandler->setCommit(true);
71
        $searchHandler->purgeIndex();
72
73
        $output->writeln('Indexing Content...');
74
75
        /** @var \Symfony\Component\Console\Helper\ProgressHelper $progress */
76
        $progress = $this->getHelperSet()->get('progress');
77
        $progress->start($output, $totalCount);
78
        $i = 0;
79 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...
80
            $contentObjects = array();
81
82
            for ($k = 0; $k <= $bulkCount; ++$k) {
83
                if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) {
84
                    break;
85
                }
86
87
                $contentObjects[] = $persistenceHandler->contentHandler()->load(
88
                    $row['id'],
89
                    $row['current_version']
90
                );
91
            }
92
93
            if (!empty($contentObjects)) {
94
                $searchHandler->bulkIndexContent($contentObjects);
95
            }
96
97
            $progress->advance($k);
98
        } while (($i += $bulkCount) < $totalCount);
99
100
        $progress->finish();
101
102
        // Indexing Locations
103
        $query = $databaseHandler->createSelectQuery();
104
        $query
105
            ->select('count(node_id)')
106
            ->from('ezcontentobject_tree')
107
            ->where(
108
                $query->expr->neq(
109
                    $databaseHandler->quoteColumn('contentobject_id'),
110
                    $query->bindValue(0, null, PDO::PARAM_INT)
111
                )
112
            );
113
        $stmt = $query->prepare();
114
        $stmt->execute();
115
        $totalCount = $stmt->fetchColumn();
116
117
        $query = $databaseHandler->createSelectQuery();
118
        $query
119
            ->select('node_id')
120
            ->from('ezcontentobject_tree')
121
            ->where(
122
                $query->expr->neq(
123
                    $databaseHandler->quoteColumn('contentobject_id'),
124
                    $query->bindValue(0, null, PDO::PARAM_INT)
125
                )
126
            );
127
128
        $stmt = $query->prepare();
129
        $stmt->execute();
130
131
        $output->writeln('Indexing Locations...');
132
133
        /** @var \Symfony\Component\Console\Helper\ProgressHelper $progress */
134
        $progress = $this->getHelperSet()->get('progress');
135
        $progress->start($output, $totalCount);
136
        $i = 0;
137 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...
138
            $locations = array();
139
140
            for ($k = 0; $k <= $bulkCount; ++$k) {
141
                if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) {
142
                    break;
143
                }
144
145
                $locations[] = $persistenceHandler->locationHandler()->load($row['node_id']);
146
            }
147
148
            if (!empty($locations)) {
149
                $searchHandler->bulkIndexLocations($locations);
150
            }
151
152
            $progress->advance($k);
153
        } while (($i += $bulkCount) < $totalCount);
154
155
        $progress->finish();
156
    }
157
}
158