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

Service/ExportService.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
$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;
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)
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