Completed
Pull Request — master (#6)
by aguevaraIL
18:47
created

Index   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 39
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0
wmc 4
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};
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 2
     */
29
    public function init()
30 2
    {
31
        $this->searchClass ?: throw new InvalidConfigException(
32
            $this::class . '::$searchClass must be set.'
33
        );
34
    }
35 2
36
    /**
37
     * @return DataProviderInterface|ActiveRecordInterface
38
     */
39
    public function run(): DataProvideInterface | ARContractSearch
40 2
    {
41
        $searchModel = $this->buildSearchModel();
42 2
        $dataProvider = $searchModel->search(
43
            Yii::$app->request->getQueryParams(),
44 2
            $this->formName
45 2
        );
46 2
        $this->checkAccess($searchModel, Yii::$app->request->getQueryParams());
47 2
48
        return $dataProvider ?: $searchModel;
49 2
    }
50
51 2
    /**
52
     * @return ARContractSearch the model used to create the data provider
53
     */
54
    protected function buildSearchModel(): ARContractSearch
55
    {
56
        return new {$this->searchClass}();
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '{' on line 56 at column 19
Loading history...
57
    }
58
}
59