Conditions | 7 |
Paths | 4 |
Total Lines | 60 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
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 | $groupSortOrder = 1; |
||
76 | foreach ($blockTypesPostData as $groupName => $blockTypeIds) |
||
77 | { |
||
78 | $sortOrder = 1; |
||
79 | foreach ($blockTypeIds as $blockTypeId) |
||
80 | { |
||
81 | $spoonedBlockType = new BlockType(); |
||
82 | $spoonedBlockType->fieldId = $fieldId; |
||
83 | $spoonedBlockType->matrixBlockTypeId = $blockTypeId; |
||
84 | $spoonedBlockType->fieldLayoutId = isset($fieldLayoutIds[$blockTypeId]) ? $fieldLayoutIds[$blockTypeId] : null; |
||
85 | $spoonedBlockType->groupName = urldecode($groupName); |
||
86 | $spoonedBlockType->context = $context; |
||
87 | $spoonedBlockType->groupSortOrder = $groupSortOrder; |
||
88 | $spoonedBlockType->sortOrder = $sortOrder; |
||
89 | |||
90 | if (!Spoon::$plugin->blockTypes->save($spoonedBlockType)) |
||
91 | { |
||
92 | $errors++; |
||
93 | } |
||
94 | |||
95 | $sortOrder++; |
||
96 | } |
||
97 | $groupSortOrder++; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if ($errors > 0) |
||
102 | { |
||
103 | return $this->asJson([ |
||
104 | 'success' => false |
||
105 | ]); |
||
106 | } |
||
107 | |||
108 | return $this->asJson([ |
||
109 | 'success' => true |
||
110 | ]); |
||
203 |