Completed
Push — develop ( b8a80f...4ca7cc )
by Sam
12s queued 10s
created

IndexCommand::getProgressBarRedrawFrequency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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