Passed
Push — master ( 52cf02...273cba )
by Alexander
18:21 queued 14:33
created

IndexAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 47.62%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 124
ccs 20
cts 42
cp 0.4762
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 7 2
B prepareDataProvider() 0 41 8
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\rest;
9
10
use Yii;
11
use yii\data\ActiveDataProvider;
12
use yii\data\DataFilter;
13
14
/**
15
 * IndexAction implements the API endpoint for listing multiple models.
16
 *
17
 * For more details and usage information on IndexAction, see the [guide article on rest controllers](guide:rest-controllers).
18
 *
19
 * @author Qiang Xue <[email protected]>
20
 * @since 2.0
21
 */
22
class IndexAction extends Action
23
{
24
    /**
25
     * @var callable a PHP callable that will be called to prepare a data provider that
26
     * should return a collection of the models. If not set, [[prepareDataProvider()]] will be used instead.
27
     * The signature of the callable should be:
28
     *
29
     * ```php
30
     * function (IndexAction $action) {
31
     *     // $action is the action object currently running
32
     * }
33
     * ```
34
     *
35
     * The callable should return an instance of [[ActiveDataProvider]].
36
     *
37
     * If [[dataFilter]] is set the result of [[DataFilter::build()]] will be passed to the callable as a second parameter.
38
     * In this case the signature of the callable should be the following:
39
     *
40
     * ```php
41
     * function (IndexAction $action, mixed $filter) {
42
     *     // $action is the action object currently running
43
     *     // $filter the built filter condition
44
     * }
45
     * ```
46
     */
47
    public $prepareDataProvider;
48
    /**
49
     * @var callable a PHP callable that will be called to prepare query in prepareDataProvider
50
     * Should return $query
51
     * For example:
52
     *
53
     * ```php
54
     * function ($query, $requestParams) {
55
     *     $query->andFilterWhere(['id' => 1]);
56
     *     ...
57
     *     return $query;
58
     * }
59
     * ```
60
     *
61
     * @since 2.0.42
62
     */
63
    public $prepareSearchQuery;
64
    /**
65
     * @var DataFilter|null data filter to be used for the search filter composition.
66
     * You must setup this field explicitly in order to enable filter processing.
67
     * For example:
68
     *
69
     * ```php
70
     * [
71
     *     'class' => 'yii\data\ActiveDataFilter',
72
     *     'searchModel' => function () {
73
     *         return (new \yii\base\DynamicModel(['id' => null, 'name' => null, 'price' => null]))
74
     *             ->addRule('id', 'integer')
75
     *             ->addRule('name', 'trim')
76
     *             ->addRule('name', 'string')
77
     *             ->addRule('price', 'number');
78
     *     },
79
     * ]
80
     * ```
81
     *
82
     * @see DataFilter
83
     *
84
     * @since 2.0.13
85
     */
86
    public $dataFilter;
87
88
89
    /**
90
     * @return ActiveDataProvider
91
     */
92 1
    public function run()
93
    {
94 1
        if ($this->checkAccess) {
95
            call_user_func($this->checkAccess, $this->id);
96
        }
97
98 1
        return $this->prepareDataProvider();
99
    }
100
101
    /**
102
     * Prepares the data provider that should return the requested collection of the models.
103
     * @return ActiveDataProvider
104
     */
105 1
    protected function prepareDataProvider()
106
    {
107 1
        $requestParams = Yii::$app->getRequest()->getBodyParams();
0 ignored issues
show
Bug introduced by
The method getBodyParams() does not exist on yii\console\Request. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        $requestParams = Yii::$app->getRequest()->/** @scrutinizer ignore-call */ getBodyParams();
Loading history...
108 1
        if (empty($requestParams)) {
109 1
            $requestParams = Yii::$app->getRequest()->getQueryParams();
0 ignored issues
show
Bug introduced by
The method getQueryParams() does not exist on yii\console\Request. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
            $requestParams = Yii::$app->getRequest()->/** @scrutinizer ignore-call */ getQueryParams();
Loading history...
110
        }
111
112 1
        $filter = null;
113 1
        if ($this->dataFilter !== null) {
114
            $this->dataFilter = Yii::createObject($this->dataFilter);
0 ignored issues
show
Bug introduced by
$this->dataFilter of type yii\data\DataFilter is incompatible with the type array|callable|string expected by parameter $type of yii\BaseYii::createObject(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            $this->dataFilter = Yii::createObject(/** @scrutinizer ignore-type */ $this->dataFilter);
Loading history...
115
            if ($this->dataFilter->load($requestParams)) {
116
                $filter = $this->dataFilter->build();
117
                if ($filter === false) {
118
                    return $this->dataFilter;
119
                }
120
            }
121
        }
122
123 1
        if ($this->prepareDataProvider !== null) {
124
            return call_user_func($this->prepareDataProvider, $this, $filter);
125
        }
126
127
        /* @var $modelClass \yii\db\BaseActiveRecord */
128 1
        $modelClass = $this->modelClass;
129
130 1
        $query = $modelClass::find();
131 1
        if (!empty($filter)) {
132
            $query->andWhere($filter);
133
        }
134 1
        if (is_callable($this->prepareSearchQuery)) {
135 1
            $query = call_user_func($this->prepareSearchQuery, $query, $requestParams);
136
        }
137
138 1
        return Yii::createObject([
139 1
            'class' => ActiveDataProvider::className(),
0 ignored issues
show
Deprecated Code introduced by
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

139
            'class' => /** @scrutinizer ignore-deprecated */ ActiveDataProvider::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
140 1
            'query' => $query,
141
            'pagination' => [
142 1
                'params' => $requestParams,
143
            ],
144
            'sort' => [
145 1
                'params' => $requestParams,
146
            ],
147
        ]);
148
    }
149
}
150