IndexCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 11
eloc 40
c 8
b 0
f 1
dl 0
loc 139
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBulkSize() 0 3 1
A handle() 0 3 1
B indexData() 0 58 7
A getProgressBarRedrawFrequency() 0 3 1
A getCount() 0 3 1
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
use Nord\Lumen\Elasticsearch\IndexNamePrefixer;
7
8
abstract class IndexCommand extends AbstractCommand
9
{
10
11
    /**
12
     * The number of items to process before updating the progress bar
13
     */
14
    const PROGRESS_BAR_REDRAW_FREQUENCY = 50;
15
16
    /**
17
     * @return array
18
     */
19
    abstract public function getData();
20
21
    /**
22
     * @return string
23
     */
24
    abstract public function getIndex();
25
26
    /**
27
     * @return string
28
     */
29
    abstract public function getType();
30
31
    /**
32
     * @param mixed $item
33
     *
34
     * @return array
35
     */
36
    abstract public function getItemBody($item);
37
38
    /**
39
     * @param mixed $item
40
     *
41
     * @return string
42
     */
43
    abstract public function getItemId($item);
44
45
    /**
46
     * @param mixed $item
47
     *
48
     * @return string|null
49
     */
50
    abstract public function getItemParent($item);
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function handle()
56
    {
57
        $this->indexData($this->getIndex());
58
    }
59
60
    /**
61
     * @param string $indexName
62
     */
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
    }
122
123
    /**
124
     * @return int the bulk size (for bulk indexing)
125
     */
126
    protected function getBulkSize()
127
    {
128
        return BulkQuery::BULK_SIZE_DEFAULT;
129
    }
130
131
    /**
132
     * @return int the progress bar redraw frequency
133
     */
134
    protected function getProgressBarRedrawFrequency()
135
    {
136
        return self::PROGRESS_BAR_REDRAW_FREQUENCY;
137
    }
138
139
    /**
140
     * Get the total count.
141
     *
142
     * @return int
143
     */
144
    protected function getCount()
145
    {
146
        return count($this->getData());
147
    }
148
}
149