Conditions | 7 |
Paths | 20 |
Total Lines | 58 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php namespace Nord\Lumen\Elasticsearch\Console; |
||
63 | protected function indexData(string $indexName): void |
||
64 | { |
||
65 | $indexName = IndexNamePrefixer::getPrefixedIndexName($indexName); |
||
66 | |||
67 | $this->info(sprintf('Indexing data of type "%s" into "%s"', $this->getType(), $indexName)); |
||
68 | |||
69 | $data = $this->getData(); |
||
70 | |||
71 | $bar = $this->output->createProgressBar($this->getCount()); |
||
72 | $bar->setRedrawFrequency($this->getProgressBarRedrawFrequency()); |
||
73 | |||
74 | $bulkQuery = new BulkQuery($this->getBulkSize()); |
||
75 | $bulkResponseAggregator = new BulkResponseAggregator(); |
||
76 | |||
77 | foreach ($data as $item) { |
||
78 | $action = new BulkAction(); |
||
79 | |||
80 | $meta = [ |
||
81 | '_index' => $indexName, |
||
82 | '_type' => $this->getType(), |
||
83 | '_id' => $this->getItemId($item), |
||
84 | ]; |
||
85 | |||
86 | if (($parent = $this->getItemParent($item)) !== null) { |
||
87 | $meta['_parent'] = $parent; |
||
88 | } |
||
89 | |||
90 | $action->setAction(BulkAction::ACTION_INDEX, $meta) |
||
91 | ->setBody($this->getItemBody($item)); |
||
92 | |||
93 | $bulkQuery->addAction($action); |
||
94 | |||
95 | if ($bulkQuery->isReady()) { |
||
96 | $response = $this->elasticsearchService->bulk($bulkQuery->toArray()); |
||
97 | $bulkQuery->reset(); |
||
98 | $bulkResponseAggregator->addResponse($response); |
||
99 | } |
||
100 | |||
101 | $bar->advance(); |
||
102 | } |
||
103 | |||
104 | if ($bulkQuery->hasItems()) { |
||
105 | $response = $this->elasticsearchService->bulk($bulkQuery->toArray()); |
||
106 | $bulkResponseAggregator->addResponse($response); |
||
107 | } |
||
108 | |||
109 | $bar->finish(); |
||
110 | |||
111 | $hasErrors = $bulkResponseAggregator->hasErrors(); |
||
112 | if ($hasErrors) { |
||
113 | $this->info("\n"); |
||
114 | $errors = $bulkResponseAggregator->getErrors(); |
||
115 | foreach ($errors as $error) { |
||
116 | $this->error($error); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | $this->info("\nDone!"); |
||
121 | } |
||
149 |