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

Index::run()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 1
nop 0
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 2
rs 10
c 0
b 0
f 0
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