Passed
Push — master ( c2b947...93db86 )
by Carlos
02:53
created

Index   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 39
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 14 2
A init() 0 4 2
1
<?php
2
3
namespace roaresearch\yii2\roa\actions;
4
5
use roaresearch\yii2\roa\hal\ARContractSearch;
6
use Yii;
7
use yii\{base\InvalidConfigException, data\DataProviderInterface, di\Instance};
8
9
/**
10
 * Action to retreive a filtered and sorted collection based on a `$searchClass`
11
 *
12
 * @author Angel (Faryshta) Guevara <[email protected]>
13
 */
14
class Index extends Action
15
{
16
    /**
17
     * @var string model class to retreive the records on the collection.
18
     */
19
    public string $searchClass;
20
21
    /**
22
     * @var string name of the form containing the filter data.
23
     */
24
    public string $formName = '';
25
26
    /**
27
     * @inheritdoc
28
     */
29 2
    public function init()
30
    {
31 2
        $this->searchClass ?: throw new InvalidConfigException(
32
            $this::class . '::$searchClass must be set.'
33
        );
34
    }
35
36
    /**
37
     * @return DataProviderInterface | ARContractSearch
38
     */
39 2
    public function run(): DataProviderInterface | ARContractSearch
40
    {
41
        /** @var ARContractSearch $searchModel */
42 2
        $searchModel = Instance::ensure(
43 2
            ['class' => $this->searchClass],
44
            ARContractSearch::class
45
        );
46 2
        $dataProvider = $searchModel->search(
47 2
            Yii::$app->request->getQueryParams(),
48 2
            $this->formName
49
        );
50 2
        $this->checkAccess($searchModel, Yii::$app->request->getQueryParams());
51
52 2
        return $dataProvider ?: $searchModel;
53
    }
54
}
55