SpoolManager::reindexData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 *
4
 * @author Mihkel Viilveer <[email protected]>
5
 * @date 28.08.2014
6
 */
7
8
namespace opus\elastic\spooler;
9
10
use yii\base\BaseObject;
11
use yii\helpers\Console;
12
13
/**
14
 * Class ElasticManager
15
 *
16
 * @author Mihkel Viilveer <[email protected]>
17
 * @package opus\elastic
18
 */
19
class SpoolManager extends BaseObject
20
{
21
    /**
22
     * DataProvider classes which hold info for inserting data to elastic
23
     *
24
     * @var AbstractDataProvider[]
25
     */
26
    public $dataProviders = [];
27
28
    /**
29
     * Spooling data batch size
30
     *
31
     * @var int
32
     */
33
    public $batchSize = 100;
34
35
    /**
36
     * 1) check if index exists, if exists delete and create new one
37
     * 2) get mapping from provider and insert it
38
     * 3) import all products (w/o spool table)
39
     */
40
    public function reindex()
41
    {
42
        $this->createIndexes();
43
        $this->reindexData();
44
    }
45
46
    /**
47
     * Spools data to elastic index
48
     */
49
    public function spool()
50
    {
51
        foreach ($this->dataProviders as $dataProvider) {
52
            $this->spoolData($dataProvider);
53
        }
54
    }
55
56
    /**
57
     * Creates indexes
58
     *
59
     * @param bool $deleteOld deletes indexes if exists
60
     * @internal param array $indexes
61
     */
62
    private function createIndexes($deleteOld = true)
63
    {
64
        foreach ($this->dataProviders as $dataProvider) {
65
            $exists = \Yii::$app->elasticsearch->createCommand()->indexExists(
66
                $dataProvider->getIndexName()
67
            );
68
            if ($exists === true && $deleteOld === true) {
69
                \Yii::$app->elasticsearch->createCommand()->deleteIndex(
70
                    $dataProvider->getIndexName()
71
                );
72
            }
73
            \Yii::$app->elasticsearch->createCommand()->createIndex(
74
                $dataProvider->getIndexName()
75
            );
76
        }
77
    }
78
79
    /**
80
     * Creates mapping and insets data to elastic
81
     *
82
     * @throws \Exception
83
     * @throws \yii\db\Exception
84
     */
85
    private function reindexData()
86
    {
87
        foreach ($this->dataProviders as $dataProvider) {
88
            \Yii::$app->elasticsearch->createCommand()->setMapping(
89
                $dataProvider->getIndexName(),
90
                $dataProvider->getTypeName(),
91
                $dataProvider->getMapping()
92
            );
93
94
            Spooler::reindexData(
95
                $dataProvider->getRecordClassName(),
96
                $dataProvider->getRecordClassTableName()
97
            );
98
            $this->spoolData($dataProvider);
99
        }
100
    }
101
102
    /**
103
     * Mass inserts data to elasticsearch
104
     *
105
     * @param AbstractDataProvider $dataProvider
106
     */
107
    private function spoolData(AbstractDataProvider $dataProvider)
108
    {
109
        $dataProvider->initializeDependentData();
110
        $offset = 0;
111
        Spooler::removeProcessingRows();
112
        $count = Spooler::getTotalCount($dataProvider->getRecordClassName());
113
        Console::startProgress(0, $count);
0 ignored issues
show
Documentation introduced by
$count is of type null|string|false, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
        $processed = 0;
115
        while (($progress = Spooler::setProcessingRows(
116
                $this->batchSize,
117
                $offset,
118
                $dataProvider->getRecordClassName()
119
            )) > 0) {
120
            $data = $dataProvider->getData();
121
            \Yii::$app->elasticsearch->createCommand()->bulk(
122
                $dataProvider->getIndexName(),
123
                $dataProvider->getTypeName(),
124
                $data
125
            );
126
            Spooler::deleteProcessingRows();
127
            $processed += $progress;
128
            Console::updateProgress($processed, $count);
0 ignored issues
show
Documentation introduced by
$count is of type null|string|false, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
        }
130
        Console::endProgress("Done\n");
131
    }
132
}
133
134