ImportService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 13.56 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 8
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A importIndex() 0 35 4
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\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
    public function importIndex(
24
        IndexService $index,
25
        $filename,
26
        OutputInterface $output,
27
        $options
28
    ) {
29
        $reader = $this->getReader($index, $this->getFilePath($filename), $options);
30
31
        $progress = new ProgressBar($output, $reader->count());
32
        $progress->setRedrawFrequency(100);
33
        $progress->start();
34
35
        $bulkSize = $options['bulk-size'];
36
        foreach ($reader as $key => $document) {
37
            $data = $document['_source'];
38
            $data['_id'] = $document['_id'];
39
40
            if (array_key_exists('fields', $document)) {
41
                $data = array_merge($document['fields'], $data);
42
            }
43
44
            $index->bulk('index', $data);
45
46
            if (($key + 1) % $bulkSize == 0) {
47
                $index->commit();
48
            }
49
50
            $progress->advance();
51
        }
52
53
        $index->commit();
54
55
        $progress->finish();
56
        $output->writeln('');
57
    }
58
59
    /**
60
     * Returns a real file path.
61
     *
62
     * @param string $filename
63
     *
64
     * @return string
65
     */
66 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...
67
    {
68
        if ($filename[0] == '/' || strstr($filename, ':') !== false) {
69
            return $filename;
70
        }
71
72
        return realpath(getcwd() . '/' . $filename);
73
    }
74
75
    protected function getReader(IndexService $manager, $filename, $options): JsonReader
76
    {
77
        return new JsonReader($manager, $filename, $options);
78
    }
79
}
80