Completed
Pull Request — 1.0 (#637)
by
unknown
02:32
created

ImportService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 8
c 4
b 0
f 2
lcom 0
cbo 4
dl 8
loc 76
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getReader() 0 4 1
B importIndex() 0 35 4
A getFilePath() 8 8 3

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 ONGR\ElasticsearchBundle\Service\Json\JsonReader;
15
use Symfony\Component\Console\Helper\ProgressBar;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * ImportService class.
20
 */
21
class ImportService
22
{
23
    /**
24
     * Imports Elasticsearch index data.
25
     *
26
     * @param Manager         $manager
27
     * @param string          $filename
28
     * @param OutputInterface $output
29
     * @param array           $options
30
     */
31
    public function importIndex(
32
        Manager $manager,
33
        $filename,
34
        OutputInterface $output,
35
        $options
36
    ) {
37
        $reader = $this->getReader($manager, $this->getFilePath($filename), $options);
38
39
        $progress = new ProgressBar($output, $reader->count());
40
        $progress->setRedrawFrequency(100);
41
        $progress->start();
42
43
        $bulkSize = $options['bulk-size'];
44
        foreach ($reader as $key => $document) {
45
            $data = $document['_source'];
46
            $data['_id'] = $document['_id'];
47
48
            if (array_key_exists('fields', $document)) {
49
                $data = array_merge($document['fields'], $data);
50
            }
51
52
            $manager->bulk('index', $document['_type'], $data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $document['_source'] on line 45 can also be of type string; however, ONGR\ElasticsearchBundle\Service\Manager::bulk() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
54
            if (($key + 1) % $bulkSize == 0) {
55
                $manager->commit();
56
            }
57
58
            $progress->advance();
59
        }
60
61
        $manager->commit();
62
63
        $progress->finish();
64
        $output->writeln('');
65
    }
66
67
    /**
68
     * Returns a real file path.
69
     *
70
     * @param string $filename
71
     *
72
     * @return string
73
     */
74 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...
75
    {
76
        if ($filename{0} == '/' || strstr($filename, ':') !== false) {
77
            return $filename;
78
        }
79
80
        return realpath(getcwd() . '/' . $filename);
81
    }
82
83
    /**
84
     * Prepares JSON reader.
85
     *
86
     * @param Manager $manager
87
     * @param string  $filename
88
     * @param array   $options
89
     *
90
     * @return JsonReader
91
     */
92
    protected function getReader($manager, $filename, $options)
93
    {
94
        return new JsonReader($manager, $filename, $options);
95
    }
96
}
97