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