| Conditions | 10 |
| Paths | 2 |
| Total Lines | 65 |
| Code Lines | 32 |
| 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 |
||
| 50 | public function actionSave() |
||
| 51 | { |
||
| 52 | $this->requirePostRequest(); |
||
| 53 | $this->requireAcceptsJson(); |
||
| 54 | |||
| 55 | // This will be an array of Tab Names with Block Type IDs. |
||
| 56 | // The order in which they appear is the order in which they should also |
||
| 57 | // be returned in eventually, so we will just rely on the id to describe this |
||
| 58 | // and make sure each time we are referencing a context that already exists to |
||
| 59 | // delete the rows matching that context before proceeding with the save. |
||
| 60 | $blockTypesPostData = Craft::$app->getRequest()->getParam('spoonedBlockTypes'); |
||
| 61 | |||
| 62 | $context = (string)Craft::$app->getRequest()->getParam('context'); |
||
| 63 | $fieldId = (integer)Craft::$app->getRequest()->getParam('fieldId'); |
||
| 64 | |||
| 65 | // Get any existing field layouts so we don’t lose them |
||
| 66 | $fieldLayoutIds = Spoon::$plugin->blockTypes->getFieldLayoutIds($context, $fieldId); |
||
| 67 | |||
| 68 | // Remove all current block types by context |
||
| 69 | Spoon::$plugin->blockTypes->deleteByContext($context, $fieldId); |
||
| 70 | |||
| 71 | // Loop over the data and save new rows for each block type / group combo |
||
| 72 | $errors = 0; |
||
| 73 | if (is_array($blockTypesPostData)) |
||
| 74 | { |
||
| 75 | foreach ($blockTypesPostData as $groupName => $blockTypeIds) |
||
| 76 | { |
||
| 77 | foreach ($blockTypeIds as $blockTypeId) |
||
| 78 | { |
||
| 79 | $spoonedBlockType = new BlockType(); |
||
| 80 | $spoonedBlockType->fieldId = $fieldId; |
||
| 81 | $spoonedBlockType->matrixBlockTypeId = $blockTypeId; |
||
| 82 | $spoonedBlockType->fieldLayoutId = isset($fieldLayoutIds[$blockTypeId]) ? $fieldLayoutIds[$blockTypeId] : null; |
||
| 83 | $spoonedBlockType->groupName = urldecode($groupName); |
||
| 84 | $spoonedBlockType->context = $context; |
||
| 85 | |||
| 86 | if (!Spoon::$plugin->blockTypes->save($spoonedBlockType)) |
||
| 87 | { |
||
| 88 | $errors++; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($errors > 0) |
||
| 95 | { |
||
| 96 | return $this->asJson([ |
||
| 97 | 'success' => false |
||
| 98 | ]); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $this->asJson([ |
||
| 102 | 'success' => true |
||
| 103 | ]); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Delete a set of Spoon block types for a given field and context |
||
| 108 | * |
||
| 109 | * @return \yii\web\Response |
||
| 110 | * @throws \yii\db\Exception |
||
| 111 | * @throws \yii\web\BadRequestHttpException |
||
| 112 | */ |
||
| 113 | public function actionDelete() |
||
| 114 | { |
||
| 115 | $this->requirePostRequest(); |
||
| 190 |