Conditions | 13 |
Paths | 32 |
Total Lines | 72 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
72 | protected function prepareDataProvider() |
||
73 | { |
||
74 | $filter = Yii::$app->request->get($this->filterAttribute); |
||
75 | $extraFilter = Yii::$app->request->get($this->extraFilter); |
||
76 | $queryParams = Yii::$app->request->getQueryParams(); |
||
77 | $updatedAttributes = []; |
||
78 | |||
79 | if (!empty($filter)) { |
||
80 | $queryParams[$this->filterAttribute] = json_decode($filter, true); |
||
|
|||
81 | } |
||
82 | |||
83 | if (!empty($extraFilter) && is_string($extraFilter)) { |
||
84 | $extraFilter = json_decode($extraFilter, true); |
||
85 | } |
||
86 | |||
87 | if (!empty($queryParams[$this->updatedAttribute])) { |
||
88 | $updatedAttributes = json_decode($queryParams[$this->updatedAttribute], true); |
||
89 | } |
||
90 | |||
91 | if (empty($updatedAttributes)) { |
||
92 | throw new BadRequestHttpException("Param '{$this->updatedAttribute}' cannot be empty"); |
||
93 | } |
||
94 | |||
95 | Yii::$app->request->setQueryParams($queryParams); |
||
96 | |||
97 | $this->prepareDataProvider = function (EIndexAction $action, $filter) use ($extraFilter) { |
||
98 | /** @var ActiveDataProvider $dataProvider */ |
||
99 | $dataProvider = call_user_func([$action->dataFilter->searchModel, 'getDataProvider']); |
||
100 | $dataProvider->query->andWhere($filter); |
||
101 | |||
102 | if ($this->addQuery) { |
||
103 | call_user_func($this->addQuery, $dataProvider->query, $extraFilter, $action->dataFilter); |
||
104 | |||
105 | if ($action->dataFilter->hasErrors()) { |
||
106 | return $action->dataFilter; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | if ($this->filterUser) { |
||
111 | $filterUserColumn = is_callable($this->filterUser) ? call_user_func($this->filterUser) : $this->filterUser; |
||
112 | |||
113 | if ($filterUserColumn !== null) { |
||
114 | $dataProvider->query->andWhere([$filterUserColumn => Yii::$app->user->getId()]); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | return $dataProvider; |
||
119 | }; |
||
120 | |||
121 | $dataProvider = parent::prepareDataProvider(); |
||
122 | /** @var ActiveQuery $query */ |
||
123 | $query = $dataProvider->query; |
||
124 | $query |
||
125 | ->limit(-1) |
||
126 | ->offset(-1) |
||
127 | ->orderBy([]); |
||
128 | |||
129 | $countUpdated = 0; |
||
130 | |||
131 | foreach ($query->each() as $model) { |
||
132 | /** @var $model ActiveRecord */ |
||
133 | $model->setAttributes($updatedAttributes); |
||
134 | |||
135 | if ($model->save()) { |
||
136 | $this->_updatedModels[] = $model; |
||
137 | $countUpdated++; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | Yii::$app->response->headers->set('X-Total-Updated', $countUpdated); |
||
142 | |||
143 | return; |
||
144 | } |
||
146 |