M190814022727RequestId::getDbTargets()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 26
rs 9.2222
cc 6
nc 3
nop 0
1
<?php
2
namespace RazonYang\Yii2\Log\Db\Migration;
3
4
use yii\db\Migration;
5
use RazonYang\Yii2\Log\Db\Target;
6
use Yii;
7
8
/**
9
 * Class M190814022727RequestId
10
 */
11
class M190814022727RequestId extends Migration
12
{
13
    /**
14
     * @var DbTarget[] Targets to create log table for
15
     */
16
    private $dbTargets = [];
17
18
    /**
19
     * @throws InvalidConfigException
20
     * @return DbTarget[]
21
     */
22
    protected function getDbTargets()
23
    {
24
        if ($this->dbTargets === []) {
25
            $log = Yii::$app->getLog();
26
27
            $usedTargets = [];
28
            foreach ($log->targets as $target) {
29
                if ($target instanceof Target) {
30
                    $currentTarget = [
31
                        $target->db,
32
                        $target->logTable,
33
                    ];
34
                    if (!in_array($currentTarget, $usedTargets, true)) {
35
                        // do not create same table twice
36
                        $usedTargets[] = $currentTarget;
37
                        $this->dbTargets[] = $target;
38
                    }
39
                }
40
            }
41
42
            if ($this->dbTargets === []) {
43
                throw new InvalidConfigException('You should configure "log" component to use one or more database targets before executing this migration.');
0 ignored issues
show
Bug introduced by
The type RazonYang\Yii2\Log\Db\Mi...\InvalidConfigException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
            }
45
        }
46
47
        return $this->dbTargets;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dbTargets returns an array which contains values of type RazonYang\Yii2\Log\Db\Target which are incompatible with the documented value type RazonYang\Yii2\Log\Db\Migration\DbTarget.
Loading history...
48
    }
49
50
    public function up()
51
    {
52
        foreach ($this->getDbTargets() as $target) {
53
            $this->db = $target->db;
54
            $this->addColumn($target->logTable, 'request_id', $this->char(13)->notNull()->defaultValue('')->after('id'));
55
            $this->createIndex('idx_request_id', $target->logTable, 'request_id');
56
        }
57
    }
58
59
    public function down()
60
    {
61
        foreach ($this->getDbTargets() as $target) {
62
            $this->db = $target->db;
63
64
            $this->dropColumn($target->logTable, 'request_id');
65
        }
66
    }
67
}
68