Completed
Push — legacy ( c3f933 )
by Simonas
95:41
created

ImportService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 17.89 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 6
dl 17
loc 95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B executeRawImport() 9 34 5
A importIndex() 0 8 2
A getFilePath() 8 8 3
A getReader() 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 ONGR\ElasticsearchBundle\ORM\Manager;
15
use ONGR\ElasticsearchBundle\Service\Json\JsonReader;
16
use Symfony\Component\Console\Helper\ProgressBar;
17
use Symfony\Component\Console\Helper\ProgressHelper;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * ImportService class.
22
 */
23
class ImportService
24
{
25
    /**
26
     * Imports Elasticsearch index data.
27
     *
28
     * @param Manager         $manager
29
     * @param string          $filename
30
     * @param bool            $raw
31
     * @param OutputInterface $output
32
     * @param int             $bulkSize
33
     *
34
     * @throws \Exception
35
     */
36
    public function importIndex($manager, $filename, $raw, OutputInterface $output, $bulkSize = 1000)
37
    {
38
        if (!$raw) {
39
            throw new \Exception('Currently only raw import is supported. Please set --raw flag to use it.');
40
        }
41
42
        $this->executeRawImport($manager, $this->getFilePath($filename), $output, $bulkSize);
43
    }
44
45
    /**
46
     * Executes a raw import.
47
     *
48
     * @param Manager         $manager
49
     * @param string          $filename
50
     * @param OutputInterface $output
51
     * @param int             $bulkSize
52
     */
53
    protected function executeRawImport(Manager $manager, $filename, OutputInterface $output, $bulkSize)
54
    {
55
        $reader = $this->getReader($manager, $filename, false);
56
57 View Code Duplication
        if (class_exists('\Symfony\Component\Console\Helper\ProgressBar')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
            $progress = new ProgressBar($output, $reader->count());
59
            $progress->setRedrawFrequency(100);
60
            $progress->start();
61
        } else {
62
            $progress = new ProgressHelper();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Console\Helper\ProgressHelper has been deprecated with message: since version 2.5, to be removed in 3.0 Use {@link ProgressBar} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
63
            $progress->setRedrawFrequency(100);
64
            $progress->start($output, $reader->count());
65
        }
66
67
        foreach ($reader as $key => $document) {
68
            $data = $document['_source'];
69
            $data['_id'] = $document['_id'];
70
71
            $manager->getConnection()->bulk('index', $document['_type'], $data);
0 ignored issues
show
Documentation introduced by
$data is of type string, 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...
72
73
            if (($key + 1) % $bulkSize == 0) {
74
                $manager->commit();
75
            }
76
77
            $progress->advance();
78
        }
79
80
        if (($key + 1) % $bulkSize != 0) {
0 ignored issues
show
Bug introduced by
The variable $key seems to be defined by a foreach iteration on line 67. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
81
            $manager->commit();
82
        }
83
84
        $progress->finish();
85
        $output->writeln('');
86
    }
87
88
    /**
89
     * Returns a real file path.
90
     *
91
     * @param string $filename
92
     *
93
     * @return string
94
     */
95 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...
96
    {
97
        if ($filename{0} == '/' || strstr($filename, ':') !== false) {
98
            return $filename;
99
        }
100
101
        return realpath(getcwd() . '/' . $filename);
102
    }
103
104
    /**
105
     * Prepares JSON reader.
106
     *
107
     * @param Manager $manager
108
     * @param string  $filename
109
     * @param bool    $convertDocuments
110
     *
111
     * @return JsonReader
112
     */
113
    protected function getReader($manager, $filename, $convertDocuments)
114
    {
115
        return new JsonReader($manager, $filename, $convertDocuments);
116
    }
117
}
118