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

ProjectConfig   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 33
c 1
b 0
f 0
dl 0
loc 60
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A rebuildProjectConfig() 0 52 4
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
}