Passed
Push — master ( 1ca40c...54695a )
by Josh
07:44 queued 11s
created

ProjectConfig::rebuildProjectConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 52
rs 9.408
cc 4
nc 4
nop 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
namespace angellco\spoon\helpers;
3
4
use Craft;
5
use craft\db\Query;
6
7
class ProjectConfig
8
{
9
10
    /**
11
     * Rebuilds the project config data.
12
     *
13
     * @return array
14
     */
15
    public static function rebuildProjectConfig(): array
16
    {
17
        $fields = Craft::$app->getFields();
18
        $configData = [];
19
20
        $blockTypes = (new Query())
21
            ->select([
22
                'b.uid',
23
                'b.fieldId',
24
                'b.matrixBlockTypeId',
25
                'b.fieldLayoutId',
26
                'b.groupName',
27
                'b.context',
28
                'b.uid',
29
                'f.uid AS fieldUid',
30
                'mbt.uid AS matrixBlockUid',
31
            ])
32
            ->from(['{{%spoon_blocktypes}} b'])
33
            ->innerJoin('{{%fields}} f', '[[b.fieldId]] = [[f.id]]')
34
            ->innerJoin('{{%matrixblocktypes}} mbt', '[[b.matrixBlockTypeId]] = [[mbt.id]]')
35
            ->all();
36
37
        foreach ($blockTypes as $blockType) {
38
39
            $data = [
40
                'groupName' => $blockType['groupName'],
41
                'context' => $blockType['context'],
42
                'field' => $blockType['fieldUid'],
43
                'matrixBlockType' => $blockType['matrixBlockUid'],
44
            ];
45
46
            if ($blockType['fieldLayoutId']) {
47
48
                $layout = $fields->getLayoutById($blockType['fieldLayoutId']);
49
50
                if ($layout) {
51
52
53
                    $layoutConfig = $layout->getConfig();
54
55
                    $layoutUid = $layout->uid;
56
57
                    $data['fieldLayout'] = [
58
                        $layoutUid => $layoutConfig
59
                    ];
60
                }
61
            }
62
63
            $configData[$blockType['uid']] = $data;
64
        }
65
66
        return $configData;
67
    }
68
}