TransitionPermission::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 31
nc 1
nop 0
dl 0
loc 47
ccs 18
cts 18
cp 1
crap 1
rs 9.424
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\workflow\models;
4
5
use yii\db\ActiveQuery;
6
7
/**
8
 * Model class for table `{{%workflow_transition}}`
9
 *
10
 * @property integer $source_stage_id
11
 * @property integer $target_stage_id
12
 * @property string $permission
13
 *
14
 * @property Transition $transition
15
 * @property Stage $sourceStage
16
 * @property Stage $targetStage
17
 */
18
class TransitionPermission extends \roaresearch\yii2\rmdb\models\Entity
19
{
20
    public const SCENARIO_UPDATE = 'update';
21
    public const SCENARIO_CREATE = 'create';
22
23
    /**
24
     * @var string full class name of the model to be used for the relations
25
     * `getSourceStage()` and `getTargetStage()`.
26
     */
27
    protected $stageClass = Stage::class;
28
29
    /**
30
     * @var string full class name of the model to be used for the relation
31
     * `getTransition()`.
32
     */
33
    protected $transitionClass = Transition::class;
34
35
    /**
36
     * @inheritdoc
37
     */
38 10
    public static function tableName()
39
    {
40 10
        return '{{%workflow_transition_permission}}';
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 7
    protected function attributeTypecast(): ?array
47
    {
48 7
        return parent::attributeTypecast() + [
49 7
            'source_stage_id' => 'integer',
50
            'target_stage_id' => 'integer',
51
        ];
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 1
    public function rules()
58
    {
59
        return [
60
            [
61 1
                ['!source_stage_id', '!target_stage_id', '!permission'],
62 1
                'safe',
63 1
                'on' => [self::SCENARIO_UPDATE],
64
            ],
65
            [['source_stage_id', 'target_stage_id', 'permission'], 'required'],
66
            [
67
                ['source_stage_id', 'target_stage_id'],
68 1
                'integer',
69 1
                'on' => [self::SCENARIO_CREATE],
70
            ],
71
            [
72
                ['permission'],
73 1
                'string',
74 1
                'min' => 6,
75 1
                'on' => [self::SCENARIO_CREATE],
76
            ],
77
78
            [
79
                ['target_stage_id'],
80 1
                'exist',
81
                'targetClass' => Transition::class,
82
                'targetAttribute' => [
83
                    'source_stage_id' => 'source_stage_id',
84
                    'target_stage_id' => 'target_stage_id',
85
                ],
86
                'skipOnError' => true,
87 1
                'when' => function () {
88 1
                    return !$this->hasErrors('source_stage_id');
89 1
                },
90 1
                'message' => 'There is no transaction between the stages.',
91 1
                'on' => [self::SCENARIO_CREATE],
92
            ],
93
94
            [
95
                ['permission'],
96 1
                'unique',
97
                'targetAttribute' => [
98
                    'source_stage_id',
99
                    'target_stage_id',
100
                    'permission',
101
                ],
102 1
                'message' => 'Permission already set for the transition.',
103 1
                'on' => [self::SCENARIO_CREATE],
104
            ],
105
        ];
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 2
    public function attributeLabels()
112
    {
113 2
        return array_merge([
114 2
            'source_stage_id' => 'Source Stage ID',
115
            'target_stage_id' => 'Target Stage ID',
116
            'permission' => 'Permission',
117 2
        ], parent::attributeLabels());
118
    }
119
120
    /**
121
     * @return ActiveQuery
122
     */
123 1
    public function getSourceStage(): ActiveQuery
124
    {
125 1
        return $this->hasOne($this->stageClass, ['id' => 'source_stage_id']);
126
    }
127
128
    /**
129
     * @return ActiveQuery
130
     */
131 1
    public function getTargetStage(): ActiveQuery
132
    {
133 1
        return $this->hasOne($this->stageClass, ['id' => 'target_stage_id']);
134
    }
135
136
    /**
137
     * @return ActiveQuery
138
     */
139 7
    public function getTransition(): ActiveQuery
140
    {
141 7
        return $this->hasOne(
142 7
            $this->transitionClass,
143
            [
144 7
                'source_stage_id' => 'source_stage_id',
145
                'target_stage_id' => 'target_stage_id',
146
            ]
147
        );
148
    }
149
}
150