IndexTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 92
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
createQuery() 0 1 ?
A run() 0 6 1
A runInternal() 0 9 2
A performAction() 0 7 1
A createDataProvider() 0 17 1
A dataProviderConfig() 0 10 1
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 craft\helpers\ArrayHelper;
12
use flipbox\craft\ember\helpers\ObjectHelper;
13
use yii\data\ActiveDataProvider;
14
use yii\data\DataProviderInterface;
15
use yii\db\QueryInterface;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 */
21
trait IndexTrait
22
{
23
    use PrepareDataTrait, CheckAccessTrait;
24
25
    /**
26
     * @var array
27
     */
28
    public $dataProvider = [];
29
30
    /**
31
     * @param array $config
32
     * @return QueryInterface
33
     */
34
    abstract protected function createQuery(array $config = []): QueryInterface;
35
36
    /**
37
     * @return DataProviderInterface
38
     * @throws \yii\base\InvalidConfigException
39
     * @throws \yii\web\HttpException
40
     */
41
    public function run(): DataProviderInterface
42
    {
43
        return $this->runInternal(
44
            $this->createDataProvider()
45
        );
46
    }
47
48
    /**
49
     * @param DataProviderInterface $dataProvider
50
     * @return DataProviderInterface
51
     * @throws \yii\web\HttpException
52
     */
53
    protected function runInternal(DataProviderInterface $dataProvider): DataProviderInterface
54
    {
55
        // Check access
56
        if (($access = $this->checkAccess($dataProvider)) !== true) {
57
            return $access;
58
        }
59
60
        return $this->performAction($dataProvider);
61
    }
62
63
    /**
64
     * @param DataProviderInterface $dataProvider
65
     * @return DataProviderInterface
66
     */
67
    protected function performAction(DataProviderInterface $dataProvider): DataProviderInterface
68
    {
69
        // Allow alterations to the data
70
        $this->prepareData($dataProvider);
71
72
        return $dataProvider;
73
    }
74
75
    /**
76
     * @param array $config
77
     * @return DataProviderInterface
78
     * @throws \yii\base\InvalidConfigException
79
     */
80
    protected function createDataProvider(array $config = []): DataProviderInterface
81
    {
82
        $queryConfig = (array) ArrayHelper::remove($config, 'query', []);
83
84
        /** @var DataProviderInterface $dataProvider */
85
        $dataProvider = ObjectHelper::create(
86
            $this->dataProviderConfig(ArrayHelper::merge(
87
                [
88
                    'query' => $this->createQuery($queryConfig)
89
                ],
90
                $config
91
            )),
92
            DataProviderInterface::class
93
        );
94
95
        return $dataProvider;
96
    }
97
98
    /**
99
     * @param array $config
100
     * @return array
101
     */
102
    protected function dataProviderConfig(array $config = []): array
103
    {
104
        return array_merge(
105
            [
106
                'class' => ActiveDataProvider::class
107
            ],
108
            $config,
109
            $this->dataProvider
110
        );
111
    }
112
}
113