| Conditions | 8 |
| Paths | 8 |
| Total Lines | 52 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 147 | public function actionSaveFieldLayout() |
||
| 148 | { |
||
| 149 | $this->requirePostRequest(); |
||
| 150 | $this->requireAcceptsJson(); |
||
| 151 | |||
| 152 | $spoonedBlockTypeId = Craft::$app->getRequest()->getParam('spoonedBlockTypeId'); |
||
| 153 | $blockTypeFieldLayouts = Craft::$app->getRequest()->getParam('blockTypeFieldLayouts'); |
||
|
|
|||
| 154 | |||
| 155 | if ($spoonedBlockTypeId) { |
||
| 156 | if (!$spoonedBlockType = Spoon::$plugin->blockTypes->getById($spoonedBlockTypeId)) { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | // Set the field layout on the model |
||
| 164 | $postedFieldLayout = Craft::$app->getRequest()->getParam('blockTypeFieldLayouts'); |
||
| 165 | |||
| 166 | // Check if we have one |
||
| 167 | if ($postedFieldLayout) { |
||
| 168 | $assembledLayout = Craft::$app->fields->assembleLayout($postedFieldLayout); |
||
| 169 | $spoonedBlockType->setFieldLayout($assembledLayout); |
||
| 170 | |||
| 171 | // Save it |
||
| 172 | if (!Spoon::$plugin->blockTypes->saveFieldLayout($spoonedBlockType)) { |
||
| 173 | return $this->asJson([ |
||
| 174 | 'success' => false |
||
| 175 | ]); |
||
| 176 | } |
||
| 177 | } else if ($spoonedBlockType->fieldLayoutId) { |
||
| 178 | |||
| 179 | // We don’t have a new field layout, so remove the old one if there is one |
||
| 180 | $oldFieldLayoutId = $spoonedBlockType->fieldLayoutId; |
||
| 181 | if (!Craft::$app->fields->deleteLayoutById($oldFieldLayoutId)) { |
||
| 182 | return $this->asJson([ |
||
| 183 | 'success' => false |
||
| 184 | ]); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Also null the col on our block type row |
||
| 188 | $spoonedBlockType->fieldLayoutId = null; |
||
| 189 | if (!Spoon::$plugin->blockTypes->save($spoonedBlockType)) { |
||
| 190 | return $this->asJson([ |
||
| 191 | 'success' => false |
||
| 192 | ]); |
||
| 193 | } |
||
| 194 | |||
| 195 | } |
||
| 196 | |||
| 197 | return $this->asJson([ |
||
| 198 | 'success' => true |
||
| 199 | ]); |
||
| 203 |