CpFieldInspectBundle   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 98
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAssetFiles() 0 21 3
A _getData() 0 27 4
A _registerTranslations() 0 8 2
1
<?php
2
3
namespace mmikkel\cpfieldinspect\web;
4
5
use Craft;
6
use craft\fieldlayoutelements\CustomField;
7
use craft\helpers\App;
8
use craft\helpers\Json;
9
use craft\web\AssetBundle;
10
use craft\web\assets\cp\CpAsset;
11
use craft\web\View;
12
13
use mmikkel\cpfieldinspect\helpers\CpFieldInspectHelper;
14
15
class CpFieldInspectBundle extends AssetBundle
16
{
17
18
    public $sourcePath = '@mmikkel/cpfieldinspect/web/assets';
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public $depends = [
24
        CpAsset::class,
25
    ];
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public $css = [
31
        'cpfieldinspect.css',
32
    ];
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public $js = [
38
        'cpfieldinspect.js',
39
    ];
40
41
    /**
42
     * @param $view
43
     * @return void
44
     */
45
    public function registerAssetFiles($view): void
46
    {
47
        parent::registerAssetFiles($view);
48
49
        if ($view instanceof View) {
50
            $this->_registerTranslations($view);
51
        }
52
53
        try {
54
            $data = Json::encode($this->_getData());
55
        } catch (\Throwable $e) {
56
            Craft::error($e, __METHOD__);
57
            return;
58
        }
59
60
        $js = <<<JS
61
            if (window.Craft.CpFieldInspectPlugin) {
62
                window.Craft.CpFieldInspectPlugin.init($data);
63
            }
64
        JS;
65
        $view->registerJs($js, View::POS_END);
66
67
    }
68
69
    /**
70
     * @param View $view
71
     * @return void
72
     */
73
    private function _registerTranslations(View $view): void
74
    {
75
        $translations = @include App::parseEnv('@mmikkel/cpfieldinspect') . DIRECTORY_SEPARATOR . 'translations' . DIRECTORY_SEPARATOR . 'en' . DIRECTORY_SEPARATOR . 'cp-field-inspect.php';
76
        if (empty($translations)) {
77
            Craft::error('Unable to register translations', __METHOD__);
78
            return;
79
        }
80
        $view->registerTranslations('cp-field-inspect', array_keys($translations));
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    private function _getData(): array
87
    {
88
        try {
89
            $fieldLayouts = Craft::$app->getFields()->getAllLayouts();
90
            $customFieldElements = [];
91
            foreach ($fieldLayouts as $fieldLayout) {
92
                $customFieldElements += array_reduce(
93
                    $fieldLayout->getCustomFieldElements(),
94
                    static function (array $carry, CustomField $fieldElement) {
95
                        $fieldId = (int)$fieldElement->getField()?->id;
96
                        if (!$fieldId) {
97
                            return $carry;
98
                        }
99
                        $carry[$fieldElement->uid] = $fieldId;
100
                        return $carry;
101
                    },
102
                    []
103
                );
104
            }
105
            return [
106
                'redirectUrl' => CpFieldInspectHelper::getRedirectUrl(),
107
                'customFieldElements' => $customFieldElements,
108
            ];
109
        } catch (\Throwable $e) {
110
            Craft::error($e, __METHOD__);
111
        }
112
        return [];
113
    }
114
115
}
116