Passed
Pull Request — develop (#115)
by
unknown
01:21
created

IndexCommand::handle()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 58
rs 8.4426
c 0
b 0
f 0
cc 7
nc 20
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Nord\Lumen\Elasticsearch\Console;
2
3
use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkAction;
4
use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkQuery;
5
use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkResponseAggregator;
6
7
abstract class IndexCommand extends AbstractCommand
8
{
9
10
    /**
11
     * The number of items to process before updating the progress bar
12
     */
13
    const PROGRESS_BAR_REDRAW_FREQUENCY = 50;
14
15
    /**
16
     * @return array
17
     */
18
    abstract public function getData();
19
20
    /**
21
     * @return string
22
     */
23
    abstract public function getIndex();
24
25
    /**
26
     * @return string
27
     */
28
    abstract public function getType();
29
30
    /**
31
     * @param mixed $item
32
     *
33
     * @return array
34
     */
35
    abstract public function getItemBody($item);
36
37
    /**
38
     * @param mixed $item
39
     *
40
     * @return string
41
     */
42
    abstract public function getItemId($item);
43
44
    /**
45
     * @param mixed $item
46
     *
47
     * @return string
48
     */
49
    abstract public function getItemParent($item);
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function handle()
55
    {
56
        $this->info(sprintf('Indexing data of type "%s" into "%s"', $this->getType(), $this->getIndex()));
57
58
        $data = $this->getData();
59
60
        $bar = $this->output->createProgressBar($this->getCount());
61
        $bar->setRedrawFrequency($this->getProgressBarRedrawFrequency());
62
63
        $bulkQuery = new BulkQuery($this->getBulkSize());
64
        $bulkResponseAggregator = new BulkResponseAggregator();
65
66
        foreach ($data as $item) {
67
            $action = new BulkAction();
68
69
            $meta = [
70
                '_index' => $this->getIndex(),
71
                '_type'  => $this->getType(),
72
                '_id'    => $this->getItemId($item),
73
            ];
74
75
            if (($parent = $this->getItemParent($item)) !== null) {
76
                $meta['_parent'] = $parent;
77
            }
78
79
            $action->setAction(BulkAction::ACTION_INDEX, $meta)
80
                   ->setBody($this->getItemBody($item));
81
82
            $bulkQuery->addAction($action);
83
84
            if ($bulkQuery->isReady()) {
85
                $response = $this->elasticsearchService->bulk($bulkQuery->toArray());
86
                $bulkQuery->reset();
87
                $bulkResponseAggregator->addResponse($response);
88
            }
89
90
            $bar->advance();
91
        }
92
93
        if ($bulkQuery->hasItems()) {
94
            $response = $this->elasticsearchService->bulk($bulkQuery->toArray());
95
            $bulkResponseAggregator->addResponse($response);
96
        }
97
98
        $bar->finish();
99
100
        $hasErrors = $bulkResponseAggregator->hasErrors();
101
        if($hasErrors) {
102
            $this->info("\n");
103
            $errors = $bulkResponseAggregator->getErrors();
104
            foreach($errors as $error) {
105
                $this->error($error);
106
            }
107
        }
108
109
        $this->info("\nDone!");
110
111
        return 0;
112
    }
113
114
    /**
115
     * @return int the bulk size (for bulk indexing)
116
     */
117
    protected function getBulkSize()
118
    {
119
        return BulkQuery::BULK_SIZE_DEFAULT;
120
    }
121
122
    /**
123
     * @return int the progress bar redraw frequency
124
     */
125
    protected function getProgressBarRedrawFrequency()
126
    {
127
        return self::PROGRESS_BAR_REDRAW_FREQUENCY;
128
    }
129
130
    /**
131
     * Get the total count.
132
     *
133
     * @return int
134
     */
135
    protected function getCount()
136
    {
137
        return count($this->getData());
138
    }
139
}
140