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