Job   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 44
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 4
A execute() 0 8 2
resolve() 0 1 ?
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-searchable
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\searchable\queue;
9
10
use yii\queue\JobInterface;
11
12
/**
13
 * Class Job providing base methods need for index data jobs.
14
 *
15
 * @author Vuong Minh <[email protected]>
16
 * @since 1.0.0
17
 */
18
abstract class Job implements JobInterface
19
{
20
21
    /**
22
     * @var array primary key for invoke records.
23
     */
24
    protected $ids = [];
25
26
    /**
27
     * QueueJob constructor.
28
     *
29
     * @param \yii\db\ActiveRecord|\yii\db\ActiveRecord[] $models need to making searchable index data.
30
     */
31
    public function __construct($models)
32
    {
33
        $models = is_array($models) ? $models : [$models];
34
35
        foreach ($models as $model) {
36
            /** @var $model \yii\db\ActiveRecord */
37
            foreach ($model->getPrimaryKey(true) as $key => $value) {
38
                $this->ids[$key][] = $value;
39
            }
40
        }
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function execute($queue)
47
    {
48
        if (!empty($this->ids)) {
49
            /** @var \yii\db\ActiveRecord $modelClass */
50
            $models = $modelClass::findAll($this->ids);
0 ignored issues
show
Bug introduced by
The variable $modelClass does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
51
            $this->resolve($models);
52
        }
53
    }
54
55
    /**
56
     * Solve models job.
57
     *
58
     * @param array|\yii\db\ActiveRecord[] $models need to be execute searchable index job.
59
     */
60
    abstract protected function resolve(array $models): void;
61
}
62