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

ExportService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 6.9 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 6
dl 8
loc 116
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B exportIndex() 0 74 5
A getFilePath() 8 8 3
A getWriter() 0 4 1

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 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 = 0;
66
        $fileCounter = 1;
67
68
        if ($results->count() <= $maxLinesInFile) {
69
            $count = $results->count();
70
        } elseif (($results->count() - ($fileCounter * $maxLinesInFile)) > $maxLinesInFile) {
71
            $count = $results->count() - ($fileCounter * $maxLinesInFile);
72
        } else {
73
            $count = $maxLinesInFile;
74
        }
75
76
        $date = date(\DateTime::ISO8601);
77
        $metadata = [
78
            'count' => $count,
79
            'date' => $date,
80
        ];
81
82
        $filename = str_replace('.json', '', $filename);
83
        $writer = $this->getWriter($this->getFilePath($filename.'.json'), $metadata);
84
85
        foreach ($results as $data) {
86
            if ($counter >= $maxLinesInFile) {
87
                $writer->finalize();
88
                $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...
89
                $fileCounter++;
90
                $counter = 0;
91
92
                $metadata = [
93
                    'count' => $count,
94
                    'date' => $date,
95
                ];
96
                $writer = $this->getWriter($this->getFilePath($filename."_".$fileCounter.".json"), $metadata);
97
            }
98
99
            $doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields']));
100
            $writer->push($doc);
101
            $progress->advance();
102
            $counter++;
103
        }
104
105
        $writer->finalize();
106
        $progress->finish();
107
        $output->writeln('');
108
    }
109
110
    /**
111
     * Returns real file path.
112
     *
113
     * @param string $filename
114
     *
115
     * @return string
116
     */
117 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...
118
    {
119
        if ($filename{0} == '/' || strstr($filename, ':') !== false) {
120
            return $filename;
121
        }
122
123
        return getcwd() . '/' . $filename;
124
    }
125
126
    /**
127
     * Prepares JSON writer.
128
     *
129
     * @param string $filename
130
     * @param array  $metadata
131
     *
132
     * @return JsonWriter
133
     */
134
    protected function getWriter($filename, $metadata)
135
    {
136
        return new JsonWriter($filename, $metadata);
137
    }
138
}
139