Passed
Push — master ( 7c9290...724959 )
by Nicolaas
09:00
created

TinyMCEConfigExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 24
c 1
b 0
f 0
dl 0
loc 47
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse_yaml_formats() 0 45 8
1
<?php
2
3
namespace Sunnysideup\CleanerTinyMCEConfig\Formats;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
7
8
class TinyMCEConfigExtension extends Extension
9
{
10
    public static function parse_yaml_formats(string $identifier = 'cms'): array
11
    {
12
        $yamlFormats = TinyMCEConfig::config()->get('formats');
13
14
        if (!isset($yamlFormats[$identifier])) {
15
            user_error('no editor formats for ' . $identifier);
16
            return [];
17
        }
18
19
        $parsedFormats = [];
20
21
        foreach ($yamlFormats[$identifier] as $sectionTitle => $sectionFormats) {
22
            $formats = [];
23
            $sort = 100;
24
25
            foreach ($sectionFormats as $sTitle => $sFormat) {
26
                if (isset($sFormat['disabled']) && $sFormat['disabled']) {
27
                    continue;
28
                }
29
30
                $title = $sTitle;
31
32
                if (isset($sFormat['title'])) {
33
                    $title = $sFormat['title'];
34
                }
35
36
                if (!isset($sFormat['sort'])) {
37
                    $sFormat['sort'] = $sort;
38
                }
39
40
                $formats[] = ['title' => $title] + $sFormat;
41
            }
42
43
44
            usort($formats, function ($x, $y) {
45
                return $x['sort'] <=> $y['sort'];
46
            });
47
48
            $parsedFormats[] = [
49
                'title' => $sectionTitle,
50
                'items' => $formats
51
            ];
52
        }
53
54
        return $parsedFormats;
55
    }
56
}
57