Passed
Pull Request — master (#35)
by Anton
07:27
created

IndexAction::prepareDataProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi\actions;
7
8
9
use yii\data\ActiveDataProvider;
10
11
class IndexAction extends Action
12
{
13
    /**
14
     * @var callable a PHP callable that will be called to prepare a data provider that
15
     * should return a collection of the models. If not set, [[prepareDataProvider()]] will be used instead.
16
     * The signature of the callable should be:
17
     *
18
     * ```php
19
     * function ($action) {
20
     *     // $action is the action object currently running
21
     * }
22
     * ```
23
     *
24
     * The callable should return an instance of [[ActiveDataProvider]].
25
     */
26
    public $prepareDataProvider;
27
28
29
    /**
30
     * @return ActiveDataProvider
31
     */
32
    public function run()
33
    {
34
        if ($this->checkAccess) {
35
            call_user_func($this->checkAccess, $this->id);
36
        }
37
38
        return $this->prepareDataProvider();
39
    }
40
41
    /**
42
     * Prepares the data provider that should return the requested collection of the models.
43
     * @return ActiveDataProvider
44
     */
45
    protected function prepareDataProvider()
46
    {
47
        if ($this->prepareDataProvider !== null) {
48
            return call_user_func($this->prepareDataProvider, $this);
49
        }
50
51
        /* @var $modelClass \yii\db\BaseActiveRecord */
52
        $modelClass = $this->modelClass;
53
54
        return new ActiveDataProvider([
55
            'query' => $modelClass::find(),
56
        ]);
57
    }
58
}