Total Complexity | 52 |
Total Lines | 512 |
Duplicated Lines | 0 % |
Changes | 30 | ||
Bugs | 0 | Features | 0 |
Complex classes like BlockTypes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BlockTypes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class BlockTypes extends Component |
||
35 | { |
||
36 | // Private Properties |
||
37 | // ========================================================================= |
||
38 | |||
39 | private $_blockTypesByContext; |
||
40 | |||
41 | private $_superTablePlugin; |
||
42 | |||
43 | private $_superTableService; |
||
44 | |||
45 | const CONFIG_BLOCKTYPE_KEY = 'spoonBlockTypes'; |
||
46 | |||
47 | // Public Methods |
||
48 | // ========================================================================= |
||
49 | |||
50 | /** |
||
51 | * Returns a Spoon block type model by its ID |
||
52 | * |
||
53 | * @param $id |
||
54 | * |
||
55 | * @return BlockType|null |
||
56 | * @throws BlockTypeNotFoundException |
||
57 | */ |
||
58 | public function getById($id) |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Returns a single BlockType Model by its context and blockTypeId |
||
71 | * |
||
72 | * @param bool $context |
||
73 | * @param bool $matrixBlockTypeId |
||
74 | * |
||
75 | * @return BlockType|bool|null |
||
76 | */ |
||
77 | public function getBlockType($context = false, $matrixBlockTypeId = false) |
||
95 | |||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns a block type by its context. |
||
100 | * |
||
101 | * @param string $context |
||
102 | * @param null|string $groupBy Group by an optional model attribute to group by |
||
103 | * @param bool $ignoreSubContext Optionally ignore the sub context (id) |
||
104 | * @param null|integer $fieldId Optionally filter by fieldId |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getByContext($context, $groupBy = null, $ignoreSubContext = false, $fieldId = null): array |
||
109 | { |
||
110 | |||
111 | if ($ignoreSubContext) { |
||
112 | |||
113 | if ($fieldId !== null) { |
||
114 | if ($context === 'global') { |
||
115 | $condition = [ |
||
116 | 'fieldId' => $fieldId, |
||
117 | 'context' => 'global' |
||
118 | ]; |
||
119 | } else { |
||
120 | $condition = [ |
||
121 | 'fieldId' => $fieldId, |
||
122 | ['like', 'context', $context.'%', false] |
||
123 | ]; |
||
124 | } |
||
125 | } else { |
||
126 | if ($context === 'global') { |
||
127 | $condition = [ |
||
128 | 'context' => 'global' |
||
129 | ]; |
||
130 | } else { |
||
131 | $condition = ['like', 'context', $context.'%', false]; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | } else { |
||
136 | $condition = [ |
||
137 | 'context' => $context |
||
138 | ]; |
||
139 | |||
140 | if ($fieldId !== null) |
||
141 | { |
||
142 | $condition['fieldId'] = $fieldId; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | $blockTypeRecords = BlockTypeRecord::find() |
||
147 | ->where($condition) |
||
148 | ->orderBy(['groupSortOrder' => SORT_ASC, 'sortOrder' => SORT_ASC]) |
||
149 | ->all(); |
||
150 | |||
151 | if ($blockTypeRecords) { |
||
152 | |||
153 | foreach ($blockTypeRecords as $blockTypeRecord) { |
||
154 | $blockType = $this->_populateBlockTypeFromRecord($blockTypeRecord); |
||
155 | $this->_blockTypesByContext[$context][$blockType->id] = $blockType; |
||
156 | } |
||
157 | |||
158 | } else { |
||
159 | return []; |
||
160 | } |
||
161 | |||
162 | if ($groupBy !== null) { |
||
163 | $return = []; |
||
164 | |||
165 | foreach ($this->_blockTypesByContext[$context] as $blockType) |
||
166 | { |
||
167 | $return[$blockType->$groupBy][] = $blockType; |
||
168 | } |
||
169 | |||
170 | return $return; |
||
171 | } |
||
172 | |||
173 | return $this->_blockTypesByContext[$context]; |
||
174 | |||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Saves our version of a block type into the project config |
||
179 | * |
||
180 | * @param BlockType $blockType |
||
181 | * |
||
182 | * @return bool |
||
183 | * @throws \Exception |
||
184 | */ |
||
185 | public function save(BlockType $blockType): bool |
||
186 | { |
||
187 | $isNew = !$blockType->id; |
||
188 | |||
189 | // Ensure the block type has a UID |
||
190 | if ($isNew) { |
||
191 | $blockType->uid = StringHelper::UUID(); |
||
192 | } else if (!$blockType->uid) { |
||
193 | $existingBlockTypeRecord = BlockTypeRecord::findOne($blockType->id); |
||
194 | |||
195 | if (!$existingBlockTypeRecord) { |
||
196 | throw new BlockTypeNotFoundException("No Spoon Block Type exists with the ID “{$blockType->id}”"); |
||
197 | } |
||
198 | |||
199 | $blockType->uid = $existingBlockTypeRecord->uid; |
||
200 | } |
||
201 | |||
202 | // Make sure it validates |
||
203 | if (!$blockType->validate()) { |
||
204 | return false; |
||
205 | } |
||
206 | |||
207 | // Save it to the project config |
||
208 | $configData = [ |
||
209 | 'groupName' => $blockType->groupName, |
||
210 | 'groupSortOrder' => $blockType->groupSortOrder, |
||
211 | 'sortOrder' => $blockType->sortOrder, |
||
212 | 'context' => $blockType->context, |
||
213 | 'field' => $blockType->getField()->uid, |
||
214 | 'matrixBlockType' => $blockType->getBlockType()->uid, |
||
215 | ]; |
||
216 | |||
217 | // Handle any currently attached field layouts |
||
218 | /** @var FieldLayout $fieldLayout */ |
||
219 | $fieldLayout = $blockType->getFieldLayout(); |
||
220 | |||
221 | if ($fieldLayout && $fieldLayout->uid) { |
||
222 | $fieldLayoutConfig = $fieldLayout->getConfig(); |
||
223 | |||
224 | $layoutUid = $fieldLayout->uid; |
||
225 | |||
226 | $configData['fieldLayout'] = [ |
||
227 | $layoutUid => $fieldLayoutConfig |
||
228 | ]; |
||
229 | } else { |
||
230 | $configData['fieldLayout'] = null; |
||
231 | } |
||
232 | |||
233 | $configPath = self::CONFIG_BLOCKTYPE_KEY . '.' . $blockType->uid; |
||
234 | |||
235 | Craft::$app->projectConfig->set($configPath, $configData); |
||
236 | |||
237 | if ($isNew) { |
||
238 | $blockType->id = Db::idByUid('{{%spoon_blocktypes}}', $blockType->uid); |
||
239 | } |
||
240 | |||
241 | return true; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Deletes all the block types for a given context from the project config |
||
246 | * |
||
247 | * @param null $context |
||
248 | * @param null $fieldId |
||
249 | * |
||
250 | * @return bool|null |
||
251 | * @throws \Exception |
||
252 | */ |
||
253 | public function deleteByContext($context = null, $fieldId = null) |
||
266 | } |
||
267 | |||
268 | // Project config methods |
||
269 | // ========================================================================= |
||
270 | |||
271 | /** |
||
272 | * Handles a changed block type and saves it to the database |
||
273 | * |
||
274 | * @param ConfigEvent $event |
||
275 | * |
||
276 | * @throws \Throwable |
||
277 | */ |
||
278 | public function handleChangedBlockType(ConfigEvent $event) |
||
279 | { |
||
280 | $fieldsService = Craft::$app->getFields(); |
||
281 | |||
282 | $uid = $event->tokenMatches[0]; |
||
283 | $data = $event->newValue; |
||
284 | |||
285 | // Make sure the field has been synced |
||
286 | $fieldId = Db::idByUid(Table::FIELDS, $data['field']); |
||
287 | if ($fieldId === null) { |
||
288 | Craft::$app->getProjectConfig()->defer($event, [$this, __FUNCTION__]); |
||
289 | return; |
||
290 | } |
||
291 | |||
292 | // Make sure the matrix block type has been synced |
||
293 | $matrixBlockTypeId = Db::idByUid(Table::MATRIXBLOCKTYPES, $data['matrixBlockType']); |
||
294 | if ($matrixBlockTypeId === null) { |
||
295 | Craft::$app->getProjectConfig()->defer($event, [$this, __FUNCTION__]); |
||
296 | return; |
||
297 | } |
||
298 | |||
299 | // Make sure fields and sites are processed |
||
300 | ProjectConfigHelper::ensureAllSitesProcessed(); |
||
301 | ProjectConfigHelper::ensureAllFieldsProcessed(); |
||
302 | |||
303 | $db = Craft::$app->getDb(); |
||
304 | $transaction = $db->beginTransaction(); |
||
305 | |||
306 | try { |
||
307 | // Get the record |
||
308 | $blockTypeRecord = $this->_getBlockTypeRecord($uid); |
||
309 | |||
310 | // Prep the record with the new data |
||
311 | $blockTypeRecord->fieldId = $fieldId; |
||
312 | $blockTypeRecord->matrixBlockTypeId = $matrixBlockTypeId; |
||
313 | $blockTypeRecord->groupName = $data['groupName']; |
||
314 | $blockTypeRecord->context = $data['context']; |
||
315 | $blockTypeRecord->groupSortOrder = $data['groupSortOrder']; |
||
316 | $blockTypeRecord->sortOrder = $data['sortOrder']; |
||
317 | $blockTypeRecord->uid = $uid; |
||
318 | |||
319 | // Handle the field layout |
||
320 | if (!empty($data['fieldLayout'])) { |
||
321 | // Save the field layout |
||
322 | $layout = FieldLayout::createFromConfig(reset($data['fieldLayout'])); |
||
323 | $layout->id = $blockTypeRecord->fieldLayoutId; |
||
324 | $layout->type = BlockType::class; |
||
325 | $layout->uid = key($data['fieldLayout']); |
||
326 | $fieldsService->saveLayout($layout); |
||
327 | $blockTypeRecord->fieldLayoutId = $layout->id; |
||
328 | } else if ($blockTypeRecord->fieldLayoutId) { |
||
329 | // Delete the field layout |
||
330 | $fieldsService->deleteLayoutById($blockTypeRecord->fieldLayoutId); |
||
331 | $blockTypeRecord->fieldLayoutId = null; |
||
332 | } |
||
333 | |||
334 | // Save the record |
||
335 | $blockTypeRecord->save(false); |
||
336 | |||
337 | $transaction->commit(); |
||
338 | } catch (\Throwable $e) { |
||
339 | $transaction->rollBack(); |
||
340 | throw $e; |
||
341 | } |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Handles a deleted block type and removes it from the database |
||
346 | * |
||
347 | * @param ConfigEvent $event |
||
348 | * |
||
349 | * @throws \Throwable |
||
350 | */ |
||
351 | public function handleDeletedBlockType(ConfigEvent $event) |
||
352 | { |
||
353 | $uid = $event->tokenMatches[0]; |
||
354 | $blockTypeRecord = $this->_getBlockTypeRecord($uid); |
||
355 | |||
356 | if (!$blockTypeRecord->id) { |
||
357 | return; |
||
358 | } |
||
359 | |||
360 | $db = Craft::$app->getDb(); |
||
361 | $transaction = $db->beginTransaction(); |
||
362 | try { |
||
363 | // Delete the block type record |
||
364 | $db->createCommand() |
||
365 | ->delete('{{%spoon_blocktypes}}', ['id' => $blockTypeRecord->id]) |
||
366 | ->execute(); |
||
367 | |||
368 | $transaction->commit(); |
||
369 | } catch (\Throwable $e) { |
||
370 | $transaction->rollBack(); |
||
371 | throw $e; |
||
372 | } |
||
373 | } |
||
374 | |||
375 | // Public Methods for FLDs on our Block Types |
||
376 | // ========================================================================= |
||
377 | |||
378 | /** |
||
379 | * Saves a field layout into the project config nested under the block type config |
||
380 | * |
||
381 | * @param BlockType $blockType |
||
382 | * |
||
383 | * @return bool |
||
384 | * @throws \Exception |
||
385 | */ |
||
386 | public function saveFieldLayout(BlockType $blockType): bool |
||
387 | { |
||
388 | /** @var FieldLayout $fieldLayout */ |
||
389 | $fieldLayout = $blockType->getFieldLayout(); |
||
390 | // $oldFieldLayoutId = $blockType->fieldLayoutId; |
||
391 | |||
392 | if ($fieldLayout->uid) { |
||
393 | $layoutUid = $fieldLayout->uid; |
||
394 | } else { |
||
395 | $layoutUid = StringHelper::UUID(); |
||
396 | $fieldLayout->uid = $layoutUid; |
||
397 | } |
||
398 | |||
399 | $fieldLayoutConfig = $fieldLayout->getConfig(); |
||
400 | |||
401 | $configPath = self::CONFIG_BLOCKTYPE_KEY . '.' . $blockType->uid . '.fieldLayout'; |
||
402 | |||
403 | Craft::$app->projectConfig->set($configPath, [ |
||
404 | $layoutUid => $fieldLayoutConfig |
||
405 | ]); |
||
406 | |||
407 | return true; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Returns an array of fieldLayoutIds indexed by matrixBlockTypeIds |
||
412 | * for the given context and fieldId combination |
||
413 | * |
||
414 | * @param string $context required |
||
415 | * @param bool|integer $fieldId required |
||
416 | * @return false|array |
||
417 | */ |
||
418 | public function getFieldLayoutIds($context, $fieldId = false) |
||
419 | { |
||
420 | |||
421 | if (!$fieldId) |
||
422 | { |
||
423 | return false; |
||
424 | } |
||
425 | |||
426 | $blockTypeRecords = BlockTypeRecord::findAll([ |
||
427 | 'context' => $context, |
||
428 | 'fieldId' => $fieldId |
||
429 | ]); |
||
430 | |||
431 | $return = array(); |
||
432 | foreach ($blockTypeRecords as $blockTypeRecord) |
||
433 | { |
||
434 | $return[$blockTypeRecord->matrixBlockTypeId] = $blockTypeRecord->fieldLayoutId; |
||
435 | } |
||
436 | return $return; |
||
437 | |||
438 | } |
||
439 | |||
440 | |||
441 | // Private Methods |
||
442 | // ========================================================================= |
||
443 | |||
444 | /** |
||
445 | * Gets a block type's record by uid. |
||
446 | * |
||
447 | * @param string $uid |
||
448 | * |
||
449 | * @return BlockTypeRecord |
||
450 | */ |
||
451 | private function _getBlockTypeRecord(string $uid): BlockTypeRecord |
||
452 | { |
||
453 | $record = BlockTypeRecord::findOne(['uid' => $uid]); |
||
454 | return $record ?? new BlockTypeRecord(); |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Populates a BlockTypeModel with attributes from a BlockTypeRecord. |
||
459 | * |
||
460 | * @param BlockTypeRecord $blockTypeRecord |
||
461 | * |
||
462 | * @return BlockType|null |
||
463 | */ |
||
464 | private function _populateBlockTypeFromRecord(BlockTypeRecord $blockTypeRecord) |
||
546 | } |
||
547 | |||
548 | } |
||
549 |