FixDataTrait::migrate()   B
last analyzed

Complexity

Conditions 8
Paths 26

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
c 1
b 0
f 0
nc 26
nop 4
dl 0
loc 26
rs 8.4444
1
<?php
2
3
namespace app\core\traits;
4
5
use yii\db\ActiveQuery;
6
7
trait FixDataTrait
8
{
9
    /**
10
     * @var integer 每次数量
11
     */
12
    protected $onceCount = 1000;
13
14
    /**
15
     * @var int (减轻DB负担)休眠,单位秒
16
     */
17
    protected $sleepSecond = 1;
18
19
    /**
20
     * 迁移数据库
21
     * @param ActiveQuery $query 查询 query
22
     * @param \Closure $existCallback 检查是否存在的回调函数
23
     * @param \Closure $callback 添加、迁移数据
24
     * @param bool $userTransaction
25
     * @return array 添加成功的 ID 数组
26
     * @throws \Exception
27
     */
28
    private function migrate($query, \Closure $existCallback, \Closure $callback, $userTransaction = true): array
29
    {
30
        $transaction = $userTransaction ? \Yii::$app->db->beginTransaction() : null;
31
        $count = $query->count();
32
        try {
33
            for ($i = 0; $i <= (int)ceil($count / $this->onceCount); $i++) {
34
                $items = $query
35
                    ->orderBy(['id' => SORT_ASC])
36
                    ->limit($this->onceCount)
37
                    ->offset($i * $this->onceCount)
38
                    ->all();
39
                // item is array
40
                foreach ($items as $item) {
41
                    if (!call_user_func($existCallback, $item)) {
42
                        // todo batch insert
43
                        $ids[] = call_user_func($callback, $item);
44
                    }
45
                }
46
            }
47
            $userTransaction ? $transaction->commit() : null;
0 ignored issues
show
Bug introduced by
The method commit() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            $userTransaction ? $transaction->/** @scrutinizer ignore-call */ commit() : null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
            sleep($this->sleepSecond);
49
            return $ids ?? [];
50
        } catch (\Exception $e) {
51
            $userTransaction ? $transaction->rollBack() : null;
52
            \Yii::error('修复数据失败', ['query' => $query, (string)$e]);
0 ignored issues
show
Bug introduced by
array('query' => $query, (string)$e) of type array<integer|string,string|yii\db\ActiveQuery> is incompatible with the type string expected by parameter $category of yii\BaseYii::error(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            \Yii::error('修复数据失败', /** @scrutinizer ignore-type */ ['query' => $query, (string)$e]);
Loading history...
53
            throw $e;
54
        }
55
    }
56
}
57