Passed
Push — master ( 2183b6...ac87c4 )
by Angel
05:00
created

Workflow   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 88
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTotalStages() 0 3 1
A getDetailStages() 0 11 1
A tableName() 0 3 1
A rules() 0 6 1
A getStages() 0 4 1
A attributeLabels() 0 6 1
A attributeTypecast() 0 3 1
A softDeleteBehaviorConfig() 0 5 1
1
<?php
2
3
namespace roaresearch\yii2\workflow\models;
4
5
use roaresearch\yii2\rmdb\SoftDeleteActiveQuery;
6
use yii\db\ActiveQuery;
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
     */
27 25
    public static function tableName()
28
    {
29 25
        return '{{%workflow}}';
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 19
    protected function attributeTypecast(): ?array
36
    {
37 19
        return parent::attributeTypecast() + ['id' => 'integer'];
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 2
    public function rules()
44
    {
45
        return [
46 2
            [['name'], 'required'],
47
            [['name'], 'string', 'min' => 6],
48
            [['name'], 'unique'],
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 3
    public function attributeLabels()
56
    {
57 3
        return array_merge([
58 3
            'id' => 'ID',
59
            'name' => 'Workflow name',
60 3
        ], parent::attributeLabels());
61
    }
62
63
    /**
64
     * @return SoftDeleteActiveQuery
65
     */
66 3
    public function getStages(): SoftDeleteActiveQuery
67
    {
68 3
        return $this->hasMany($this->stageClass, ['workflow_id' => 'id'])
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hasMany($t...->inverseOf('workflow') returns the type yii\db\ActiveQuery which includes types incompatible with the type-hinted return roaresearch\yii2\rmdb\SoftDeleteActiveQuery.
Loading history...
69 3
            ->inverseOf('workflow');
70
    }
71
72
    /**
73
     * @return ActiveQuery
74
     */
75 1
    public function getDetailStages(): ActiveQuery
76
    {
77 1
        $query = $this->getStages();
78 1
        $query->multiple = false;
79
80 1
        return $query->select([
81 1
            'workflow_id',
82
            'totalStages' => 'count(distinct id)',
83 1
        ])->asArray()
84 1
        ->inverseOf(null)
85 1
        ->groupBy('workflow_id');
86
    }
87
88
    /**
89
     * @return int
90
     */
91 1
    public function getTotalStages(): int
92
    {
93 1
        return (int)$this->detailStages['totalStages'];
0 ignored issues
show
Bug Best Practice introduced by
The property detailStages does not exist on roaresearch\yii2\workflow\models\Workflow. Since you implemented __get, consider adding a @property annotation.
Loading history...
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 19
    protected function softDeleteBehaviorConfig(): array
100
    {
101 19
        return parent::softDeleteBehaviorConfig() + [
102 19
            'allowDeleteCallback' => function ($record) {
103 1
                return !$record->getStages()->exists();
104 19
            },
105
        ];
106
    }
107
}
108