| Conditions | 7 |
| Paths | 20 |
| Total Lines | 58 |
| Code Lines | 34 |
| 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; |
||
| 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 | } |
||
| 140 |