Completed
Push — develop ( d0b441...19bbff )
by Nate
02:35
created

IndexTrait::normalizeQueryConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\actions;
10
11
use flipbox\craft\ember\helpers\ObjectHelper;
12
use yii\data\ActiveDataProvider;
13
use yii\data\DataProviderInterface;
14
use yii\db\QueryInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
trait IndexTrait
21
{
22
    use PrepareDataTrait, CheckAccessTrait;
23
24
    /**
25
     * @var array
26
     */
27
    public $dataProvider = [];
28
29
    /**
30
     * @return QueryInterface
31
     */
32
    abstract protected function createQuery(): QueryInterface;
33
34
    /**
35
     * @return DataProviderInterface
36
     * @throws \yii\base\InvalidConfigException
37
     * @throws \yii\web\HttpException
38
     */
39
    public function run(): DataProviderInterface
40
    {
41
        return $this->runInternal(
42
            $this->createDataProvider()
43
        );
44
    }
45
46
    /**
47
     * @param DataProviderInterface $dataProvider
48
     * @return DataProviderInterface
49
     * @throws \yii\web\HttpException
50
     */
51
    protected function runInternal(DataProviderInterface $dataProvider): DataProviderInterface
52
    {
53
        // Check access
54
        if (($access = $this->checkAccess($dataProvider)) !== true) {
55
            return $access;
56
        }
57
58
        return $this->performAction($dataProvider);
59
    }
60
61
    /**
62
     * @param DataProviderInterface $dataProvider
63
     * @return DataProviderInterface
64
     */
65
    protected function performAction(DataProviderInterface $dataProvider): DataProviderInterface
66
    {
67
        // Allow alterations to the data
68
        $this->prepareData($dataProvider);
69
70
        return $dataProvider;
71
    }
72
73
    /**
74
     * @return DataProviderInterface
75
     * @throws \yii\base\InvalidConfigException
76
     */
77
    protected function createDataProvider(): DataProviderInterface
78
    {
79
        /** @var DataProviderInterface $dataProvider */
80
        $dataProvider = ObjectHelper::create(
81
            $this->dataProviderConfig([
82
                'query' => $this->createQuery()
83
            ]),
84
            DataProviderInterface::class
85
        );
86
87
        return $dataProvider;
88
    }
89
90
    /**
91
     * @param array $config
92
     * @return array
93
     */
94
    protected function dataProviderConfig(array $config = []): array
95
    {
96
        return array_merge(
97
            [
98
                'class' => ActiveDataProvider::class
99
            ],
100
            $config,
101
            $this->dataProvider
102
        );
103
    }
104
}
105