Index   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 4
c 4
b 1
f 1
lcom 1
cbo 4
dl 0
loc 67
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A newSearchModel() 0 4 1
A dataProvider() 0 8 2
A run() 0 10 1
1
<?php
2
/**
3
 * @link https://github.com/roboapp
4
 * @copyright Copyright (c) 2016 Roboapp
5
 * @license [MIT License](https://opensource.org/licenses/MIT)
6
 */
7
8
namespace roboapp\crud;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\base\Model;
13
14
/**
15
 * Action displays the models list with search support.
16
 *
17
 * @author iRipVanWinkle <[email protected]>
18
 * @since 1.0
19
 */
20
class Index extends BaseAction
21
{
22
    /**
23
     * @var string | callable a PHP callable that will be called to create the search model.
24
     * The callable should return the new model instance.
25
     */
26
    public $searchModel;
27
28
    /**
29
     * @var string name of the view
30
     */
31
    public $view = 'index';
32
33
    /**
34
     * @var string the name variable model in the view template
35
     */
36
    public $nameVariableModel = 'model';
37
38
    /**
39
     * @var string the name variable data provider in the view template
40
     */
41
    public $nameVariableDataProvider = 'dataProvider';
42
43
    /**
44
     * @var string the method name  for return DataProvider
45
     */
46
    public $searchMethodName = 'search';
47
48
    /**
49
     * Creates new search model instance.
50
     * @return Model new model instance.
51
     * @throws InvalidConfigException on invalid configuration.
52
     */
53 12
    protected function newSearchModel()
54
    {
55 12
        return \Yii::createObject($this->searchModel);
56 3
    }
57 9
58 6
    /**
59
     * @param Model $model new model instance.
60
     * @return mixed
61 3
     * @throws InvalidConfigException
62 3
     */
63
    protected function dataProvider($model)
64
    {
65
        if (method_exists($model, $this->searchMethodName)) {
66
            return $model->{$this->searchMethodName}(Yii::$app->getRequest()->getQueryParams());
67
        }
68
69
        throw new InvalidConfigException('"' . get_class($this) . '::searchModel" must be set existing method.');
70 9
    }
71
72 9
    /**
73 6
     * Displays models list.
74
     * @return mixed response.
75
     */
76 3
    public function run()
77
    {
78
        $searchModel = $this->newSearchModel();
79
        $dataProvider = $this->dataProvider($searchModel);
80
81
        return $this->controller->render($this->view, [
82
            $this->nameVariableModel => $searchModel,
83 12
            $this->nameVariableDataProvider => $dataProvider,
84
        ]);
85 12
    }
86
}
87