Passed
Push — master ( c54f6e...06ab92 )
by Josh
11:09 queued 11s
created

ProjectConfig   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 42
c 4
b 1
f 0
dl 0
loc 71
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B rebuildProjectConfig() 0 63 7
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
        $projectConfig = Craft::$app->getProjectConfig();
18
        $schemaVersion = $projectConfig->get('plugins.spoon.schemaVersion', true);
19
        $oldSchema = version_compare($schemaVersion, '3.4.0', '<');
20
21
        $fields = Craft::$app->getFields();
22
        $configData = [];
23
24
        $selectArray = [
25
            'b.uid',
26
            'b.fieldId',
27
            'b.matrixBlockTypeId',
28
            'b.fieldLayoutId',
29
            'b.groupName',
30
            'b.context',
31
            'b.uid',
32
            'f.uid AS fieldUid',
33
            'mbt.uid AS matrixBlockUid',
34
        ];
35
36
        if (!$oldSchema) {
37
            $selectArray[] = 'b.groupSortOrder';
38
            $selectArray[] = 'b.sortOrder';
39
        }
40
41
        $blockTypes = (new Query())
42
            ->select($selectArray)
43
            ->from(['{{%spoon_blocktypes}} b'])
44
            ->innerJoin('{{%fields}} f', '[[b.fieldId]] = [[f.id]]')
45
            ->innerJoin('{{%matrixblocktypes}} mbt', '[[b.matrixBlockTypeId]] = [[mbt.id]]')
46
            ->all();
47
48
        foreach ($blockTypes as $blockType) {
49
50
            $data = [
51
                'groupName' => $blockType['groupName'],
52
                'context' => $blockType['context'],
53
                'groupSortOrder' => $oldSchema ? null : $blockType['groupSortOrder'],
54
                'sortOrder' => $oldSchema ? null : $blockType['sortOrder'],
55
                'field' => $blockType['fieldUid'],
56
                'matrixBlockType' => $blockType['matrixBlockUid'],
57
            ];
58
59
            if ($blockType['fieldLayoutId']) {
60
61
                $layout = $fields->getLayoutById($blockType['fieldLayoutId']);
62
63
                if ($layout) {
64
                    $layoutConfig = $layout->getConfig();
65
66
                    $layoutUid = $layout->uid;
67
68
                    $data['fieldLayout'] = [
69
                        $layoutUid => $layoutConfig
70
                    ];
71
                }
72
            }
73
74
            $configData[$blockType['uid']] = $data;
75
        }
76
77
        return $configData;
78
    }
79
}