Completed
Push — 1.1 ( b62826 )
by Simonas
02:23
created

ExportService::exportIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 66
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 66
rs 9.3191
cc 3
eloc 48
nc 3
nop 6

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
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Service;
13
14
use Elasticsearch\Helper\Iterators\SearchHitIterator;
15
use Elasticsearch\Helper\Iterators\SearchResponseIterator;
16
use ONGR\ElasticsearchBundle\Service\Json\JsonWriter;
17
use Symfony\Component\Console\Helper\ProgressBar;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * ExportService class.
22
 */
23
class ExportService
24
{
25
    /**
26
     * Exports es index to provided file.
27
     *
28
     * @param Manager         $manager
29
     * @param string          $filename
30
     * @param array           $types
31
     * @param int             $chunkSize
32
     * @param int             $maxLinesInFile
33
     * @param OutputInterface $output
34
     */
35
    public function exportIndex(
36
        Manager $manager,
37
        $filename,
38
        $types,
39
        $chunkSize,
40
        OutputInterface $output,
41
        $maxLinesInFile = 300000
42
    ) {
43
        $params = [
44
            'search_type' => 'scan',
45
            'scroll' => '10m',
46
            'size' => $chunkSize,
47
            'source' => true,
48
            'body' => [
49
                'query' => [
50
                    'match_all' => [],
51
                ],
52
            ],
53
            'index' => $manager->getIndexName(),
54
            'type' => $types,
55
        ];
56
57
        $results = new SearchHitIterator(
58
            new SearchResponseIterator($manager->getClient(), $params)
59
        );
60
61
        $progress = new ProgressBar($output, $results->count());
62
        $progress->setRedrawFrequency(100);
63
        $progress->start();
64
65
        $counter = $fileCounter = 0;
66
        $count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter);
67
68
        $date = date(\DateTime::ISO8601);
69
        $metadata = [
70
            'count' => $count,
71
            'date' => $date,
72
        ];
73
74
        $filename = str_replace('.json', '', $filename);
75
        $writer = $this->getWriter($this->getFilePath($filename.'.json'), $metadata);
76
77
        foreach ($results as $data) {
78
            if ($counter >= $maxLinesInFile) {
79
                $writer->finalize();
80
                $writer = null;
0 ignored issues
show
Unused Code introduced by
$writer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
                $fileCounter++;
82
                $count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter);
83
                $metadata = [
84
                    'count' => $count,
85
                    'date' => $date,
86
                ];
87
                $writer = $this->getWriter($this->getFilePath($filename."_".$fileCounter.".json"), $metadata);
88
                $counter = 0;
89
            }
90
91
            $doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields']));
92
            $writer->push($doc);
93
            $progress->advance();
94
            $counter++;
95
        }
96
97
        $writer->finalize();
98
        $progress->finish();
99
        $output->writeln('');
100
    }
101
102
    /**
103
     * Returns real file path.
104
     *
105
     * @param string $filename
106
     *
107
     * @return string
108
     */
109 View Code Duplication
    protected function getFilePath($filename)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
110
    {
111
        if ($filename{0} == '/' || strstr($filename, ':') !== false) {
112
            return $filename;
113
        }
114
115
        return getcwd() . '/' . $filename;
116
    }
117
118
    /**
119
     * Prepares JSON writer.
120
     *
121
     * @param string $filename
122
     * @param array  $metadata
123
     *
124
     * @return JsonWriter
125
     */
126
    protected function getWriter($filename, $metadata)
127
    {
128
        return new JsonWriter($filename, $metadata);
129
    }
130
131
    /**
132
     * @param int $resultsCount
133
     * @param int $maxLinesInFile
134
     * @param int $fileCounter
135
     *
136
     * @return int
137
     */
138
    protected function getFileCount($resultsCount, $maxLinesInFile, $fileCounter)
139
    {
140
        $leftToInsert = $resultsCount - ($fileCounter * $maxLinesInFile);
141
        if ($leftToInsert <= $maxLinesInFile) {
142
            $count = $leftToInsert;
143
        } else {
144
            $count = $maxLinesInFile;
145
        }
146
147
        return $count;
148
    }
149
}
150