TransitionPermissionSearch::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\workflow\roa\models;
4
5
use roaresearch\yii2\roa\ResourceSearch;
6
use yii\{data\ActiveDataProvider, web\NotFoundHttpException};
7
8
/**
9
 * Contract to filter and sort collections of `TransitionPermission` records.
10
 *
11
 * @author Angel (Faryshta) Guevara <[email protected]>
12
 */
13
class TransitionPermissionSearch extends TransitionPermission implements
14
    ResourceSearch
15
{
16
    /**
17
     * @inhertidoc
18
     */
19 1
    public function rules()
20
    {
21
        return [
22 1
            [['created_by', 'source_stage_id', 'target_stage_id'], 'integer'],
23
            [['permission'], 'string'],
24
        ];
25
    }
26
27
    /**
28
     * @inhertidoc
29
     */
30 1
    public function search(
31
        array $params,
32
        ?string $formName = ''
33
    ): ?ActiveDataProvider {
34 1
        $this->load($params, $formName);
35 1
        if (!$this->validate()) {
36 1
            return null;
37
        }
38
        if (
39 1
            null === $this->transition
40 1
            || $this->sourceStage->workflow_id != $params['workflow_id']
41
        ) {
42 1
            throw new NotFoundHttpException('Unexistant permission path.');
43
        }
44
45 1
        $class = get_parent_class();
46 1
        return new ActiveDataProvider([
47 1
            'query' => $class::find()->andFilterWhere([
48 1
                    'target_stage_id' => $this->target_stage_id,
49 1
                    'source_stage_id' => $this->source_stage_id,
50
                ])
51 1
                ->andFilterWhere(['like', 'permission', $this->permission]),
52
        ]);
53
    }
54
}
55