Completed
Push — master ( 09a07f...8de6a8 )
by Simonas
01:54
created

ExportService::exportIndex()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 86
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 86
rs 8.6583
c 2
b 1
f 0
cc 3
eloc 49
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\Result\RawIterator;
17
use ONGR\ElasticsearchBundle\Service\Json\JsonWriter;
18
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
19
use ONGR\ElasticsearchDSL\Search;
20
use Symfony\Component\Console\Helper\ProgressBar;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * ExportService class.
25
 */
26
class ExportService
27
{
28
    /**
29
     * Exports es index to provided file.
30
     *
31
     * @param Manager         $manager
32
     * @param string          $filename
33
     * @param array           $types
34
     * @param int             $chunkSize
35
     * @param int             $maxLinesInFile
36
     * @param OutputInterface $output
37
     */
38
    public function exportIndex(
39
        Manager $manager,
40
        $filename,
41
        $types,
42
        $chunkSize,
0 ignored issues
show
Unused Code introduced by
The parameter $chunkSize is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
        OutputInterface $output,
44
        $maxLinesInFile = 300000
45
    ) {
46
//        $params = [
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
////            'search_type' => 'scroll',
48
//            'scroll' => '10m',
49
////            'size' => $chunkSize,
50
//            '_source' => true,
51
//            'body' => [
52
//                'query' => [
53
//                    'match_all' => new \stdClass(),
54
//                ],
55
//            ],
56
//            'index' => $manager->getIndexName(),
57
//            'type' => $types,
58
//        ];
59
60
        $search = new Search();
61
        $search->addQuery(new MatchAllQuery());
62
        $queryParameters = [
63
                '_source' => true,
64
                'scroll' => '10m',
65
            ];
66
67
        $searchResults = $manager->search($types, $search->toArray(), $queryParameters);
68
69
        $results = new RawIterator(
70
            $searchResults,
0 ignored issues
show
Documentation introduced by
$searchResults is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
            $manager,
72
            [
73
                'duration' => $queryParameters['scroll'],
74
                '_scroll_id' => $searchResults['_scroll_id'],
75
            ]
76
        );
77
78
//        $results = new SearchHitIterator(
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
//            new SearchResponseIterator($manager->getClient(), $params)
80
//        );
81
82
        $progress = new ProgressBar($output, $results->count());
83
        $progress->setRedrawFrequency(100);
84
        $progress->start();
85
86
        $counter = $fileCounter = 0;
87
        $count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter);
88
89
        $date = date(\DateTime::ISO8601);
90
        $metadata = [
91
            'count' => $count,
92
            'date' => $date,
93
        ];
94
95
        $filename = str_replace('.json', '', $filename);
96
        $writer = $this->getWriter($this->getFilePath($filename.'.json'), $metadata);
97
98
        $file = [];
99
        foreach ($results as $data) {
100
            if ($counter >= $maxLinesInFile) {
101
                $writer->finalize();
102
                $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...
103
                $fileCounter++;
104
                $count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter);
105
                $metadata = [
106
                    'count' => $count,
107
                    'date' => $date,
108
                ];
109
                $writer = $this->getWriter($this->getFilePath($filename."_".$fileCounter.".json"), $metadata);
110
                $counter = 0;
111
            }
112
113
            $doc = array_intersect_key($data, array_flip(['_id', '_type', '_source']));
114
            $writer->push($doc);
115
            $file[] = $doc;
116
            $progress->advance();
117
            $counter++;
118
        }
119
120
        $writer->finalize();
121
        $progress->finish();
122
        $output->writeln('');
123
    }
124
125
    /**
126
     * Returns real file path.
127
     *
128
     * @param string $filename
129
     *
130
     * @return string
131
     */
132 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...
133
    {
134
        if ($filename{0} == '/' || strstr($filename, ':') !== false) {
135
            return $filename;
136
        }
137
138
        return getcwd() . '/' . $filename;
139
    }
140
141
    /**
142
     * Prepares JSON writer.
143
     *
144
     * @param string $filename
145
     * @param array  $metadata
146
     *
147
     * @return JsonWriter
148
     */
149
    protected function getWriter($filename, $metadata)
150
    {
151
        return new JsonWriter($filename, $metadata);
152
    }
153
154
    /**
155
     * @param int $resultsCount
156
     * @param int $maxLinesInFile
157
     * @param int $fileCounter
158
     *
159
     * @return int
160
     */
161
    protected function getFileCount($resultsCount, $maxLinesInFile, $fileCounter)
162
    {
163
        $leftToInsert = $resultsCount - ($fileCounter * $maxLinesInFile);
164
        if ($leftToInsert <= $maxLinesInFile) {
165
            $count = $leftToInsert;
166
        } else {
167
            $count = $maxLinesInFile;
168
        }
169
170
        return $count;
171
    }
172
}
173