Completed
Pull Request — master (#145)
by Klochok
13:56
created

IndexAction::getDataProvider()   C

Complexity

Conditions 12
Paths 161

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 50
ccs 0
cts 17
cp 0
rs 6.4583
cc 12
nc 161
nop 0
crap 156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\actions;
12
13
use hipanel\base\FilterStorage;
14
use hipanel\grid\RepresentationCollectionFinder;
15
use hipanel\widgets\SynchronouslyCountEnabler;
16
use hiqdev\higrid\representations\RepresentationCollection;
17
use hiqdev\higrid\representations\RepresentationCollectionInterface;
18
use Yii;
19
use yii\grid\GridView;
20
use yii\helpers\ArrayHelper;
21
use yii\helpers\Inflector;
22
use yii\web\Controller;
23
24
/**
25
 * Class IndexAction.
26
 */
27
class IndexAction extends SearchAction
28
{
29
    /**
30
     * @var string view to render
31
     */
32
    protected $_view;
33
34
    public array $responseVariants = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
35
36
    /**
37
     * @var RepresentationCollectionFinder
38
     */
39
    private $representationCollectionFinder;
40
41
    public function setView($value)
42
    {
43
        $this->_view = $value;
44
    }
45
46
    public function getView()
47
    {
48
        if ($this->_view === null) {
49
            $this->_view = lcfirst(Inflector::id2camel($this->id));
50
        }
51
52
        return $this->_view;
53
    }
54
55
    public function __construct(string $id, Controller $controller, RepresentationCollectionFinder $representationCollectionFinder, array $config = [])
56
    {
57
        parent::__construct($id, $controller, $config);
58
        $this->representationCollectionFinder = $representationCollectionFinder;
59
    }
60
61
    /**
62
     * @var array The map of filters for the [[hipanel\base\FilterStorage|FilterStorage]]
63
     */
64
    public $filterStorageMap = [];
65
66
    protected function getDefaultRules()
67
    {
68
        return ArrayHelper::merge([
69
            'html | pjax' => [
70
                'save' => false,
71
                'flash' => false,
72
                'success' => [
73
                    'class' => RenderAction::class,
74
                    'view' => $this->getView(),
75
                    'data' => $this->data,
76
                    'params' => function () {
77
                        return [
78
                            'model' => $this->getSearchModel(),
79
                            'dataProvider' => $this->getDataProvider(),
80
                            'representationCollection' => $this->ensureRepresentationCollection(),
81
                            'uiModel' => $this->getUiModel(),
82
                        ];
83
                    },
84
                ],
85
            ],
86
            'GET ajax' => [
87
                'class' => VariantsAction::class,
88
                'variants' => array_merge([
89
                    'pager' => fn(VariantsAction $action): string => SynchronouslyCountEnabler::widget([
90
                        'dataProvider' => $action->parent->getDataProvider(),
91
                        'content' => fn(GridView $grid): string => $grid->renderPager(),
92
                    ]),
93
                    'summary' => fn(VariantsAction $action): string => SynchronouslyCountEnabler::widget([
94
                        'dataProvider' => $action->parent->getDataProvider(),
95
                        'content' => fn(GridView $grid): string => $grid->renderSummary(),
96
                    ]),
97
                ], $this->responseVariants),
98
            ],
99
        ], parent::getDefaultRules());
100
    }
101
102
    public function getUiModel()
103
    {
104
        return $this->controller->indexPageUiOptionsModel;
105
    }
106
107
    /**
108
     * Method tries to guess representation collection class name and create object
109
     * Creates empty collection when no specific representation exists.
110
     *
111
     * @return RepresentationCollection|RepresentationCollectionInterface
112
     */
113
    protected function ensureRepresentationCollection()
114
    {
115
        return $this->representationCollectionFinder->findOrFallback();
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getDataProvider()
122
    {
123
        if ($this->dataProvider === null) {
124
            $request = Yii::$app->request;
125
126
            $formName = $this->getSearchModel()->formName();
127
            $requestFilters = $request->get($formName) ?: $request->get() ?: $request->post();
128
129
            // Don't save filters for ajax requests, because
130
            // the request is probably triggered with select2 or smt similar
131
            if ($request->getIsPjax() || !$request->getIsAjax()) {
132
                $filterStorage = new FilterStorage(['map' => $this->filterStorageMap]);
133
134
                if ($request->getIsPost() && $request->post('clear-filters')) {
135
                    $filterStorage->clearFilters();
136
                }
137
138
                $filterStorage->set($requestFilters);
139
140
                // Apply filters from storage only when request does not contain any data
141
                if (empty($requestFilters)) {
142
                    $requestFilters = $filterStorage->get();
143
                }
144
            }
145
146
            $search = ArrayHelper::merge($this->findOptions, $requestFilters);
147
148
            $this->returnOptions[$this->controller->modelClassName()] = ArrayHelper::merge(
149
                ArrayHelper::remove($search, 'return', []),
150
                ArrayHelper::remove($search, 'rename', [])
151
            );
152
153
            if ($formName !== '') {
154
                $search = [$formName => $search];
155
            }
156
            $this->dataProvider = $this->getSearchModel()->search($search, $this->dataProviderOptions);
157
158
            // Set sort
159
            if ($this->getUiModel()->sort) {
160
                $this->dataProvider->setSort(['defaultOrder' => [$this->getUiModel()->sortAttribute => $this->getUiModel()->sortDirection]]);
161
            }
162
163
            // Set pageSize
164
            if ($this->getUiModel()->per_page) {
165
                $this->dataProvider->setPagination(['pageSize' => $this->getUiModel()->per_page]);
166
            }
167
        }
168
169
        return $this->dataProvider;
170
    }
171
}
172