Test Failed
Pull Request — master (#4)
by Angel
04:35
created

Workflow   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 78
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
3
namespace roaresearch\yii2\workflow\models;
4
5
use roaresearch\yii2\rmdb\SoftDeleteActiveQuery
6
use yii\db\ActiveQuery;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_USE, expecting ',' or ';' on line 6 at column 0
Loading history...
7
8
/**
9
 * Model class for table `{{%workflow}}`
10
 *
11
 * @property integer $id
12
 * @property string $name
13
 *
14
 * @property Stage[] $stages
15
 */
16
class Workflow extends \roaresearch\yii2\rmdb\models\PersistentEntity
17
{
18
    /**
19
     * @var string full class name of the model to be used for the relation
20
     * `getStages()`.
21
     */
22
    protected $stageClass = Stage::class;
23
24
    /**
25
     * @inheritdoc
26 25
     */
27
    public static function tableName()
28 25
    {
29
        return '{{%workflow}}';
30
    }
31
32
    /**
33
     * @inheritdoc
34 19
     */
35
    protected function attributeTypecast(): ?array
36 19
    {
37
        return parent::attributeTypecast() + ['id' => 'integer'];
38
    }
39
40
    /**
41
     * @inheritdoc
42 2
     */
43
    public function rules()
44
    {
45 2
        return [
46
            [['name'], 'required'],
47
            [['name'], 'string', 'min' => 6],
48
            [['name'], 'unique'],
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54 3
     */
55
    public function attributeLabels()
56 3
    {
57 3
        return array_merge([
58
            'id' => 'ID',
59 3
            'name' => 'Workflow name',
60
        ], parent::attributeLabels());
61
    }
62
63
    /**
64
     * @return ActiveQuery
65 2
     */
66
    public function getStages(): SoftDeleteActiveQuery
67 2
    {
68 2
        return $this->hasMany($this->stageClass, ['workflow_id' => 'id'])
69
            ->inverseOf('workflow');
70
    }
71
72
    /**
73
     * @return ActiveQuery
74 1
     */
75
    public function getDetailStages(): ActiveQuery
76 1
    {
77 1
        $query = $this->getStages();
78
        $query->multiple = false;
79 1
80 1
        return $query->select([
81
            'workflow_id',
82 1
            'totalStages' => 'count(distinct id)',
83 1
        ])->asArray()
84 1
        ->inverseOf(null)
85
        ->groupBy('workflow_id');
86
    }
87
88
    /**
89
     * @return int
90 1
     */
91
    public function getTotalStages(): int
92 1
    {
93
        return (int)$this->detailStages['totalStages'];
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    protected function softDeleteConf(): array
100
    {
101
        return parent::softDeleteConf() + [
102
            'allowDeleteCallback' => function ($record) {
103
                return !$record->getStages()->exists();
104
            },
105
        ];
106
    }
107
}
108