Completed
Push — manifest ( ff1e4e...10b4c8 )
by Michael
08:53
created

Manifest::run()   D

Complexity

Conditions 14
Paths 192

Size

Total Lines 85
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 57
nc 192
nop 0
dl 0
loc 85
rs 4.6283
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
3
if (!defined('DOKU_INC')) {
4
    define('DOKU_INC', __DIR__ . '/../../');
5
}
6
require_once(DOKU_INC . 'inc/init.php');
7
8
class Manifest {
9
    public function run() {
10
        $manifest = retrieveConfig('manifest', [$this, 'jsonToArray']);
11
12
        global $conf;
13
14
        $manifest['scope'] = DOKU_REL;
15
16
        if (empty($manifest['name'])) {
17
            $manifest['name'] = $conf['title'];
18
        }
19
20
        if (empty($manifest['short_name'])) {
21
            $manifest['short_name'] = $conf['title'];
22
        }
23
24
        if (empty($manifest['description'])) {
25
            $manifest['description'] = $conf['tagline'];
26
        }
27
28
        if (empty($manifest['start_url'])) {
29
            $manifest['start_url'] = DOKU_REL;
30
        }
31
32
        $styleUtil = new \dokuwiki\StyleUtils();
33
        $styleIni = $styleUtil->cssStyleini($conf['template']);
34
        $replacements = $styleIni['replacements'];
35
36
        if (empty($manifest['background_color'])) {
37
            $manifest['background_color'] = $replacements['__background__'];
38
        }
39
40
        if (empty($manifest['theme_color'])) {
41
            $manifest['theme_color'] = !empty($replacements['__theme_color__']) ? $replacements['__theme_color__'] : $replacements['__background_alt__'];
42
        }
43
44
        if (empty($manifest['icons'])) {
45
            $manifest['icons'] = [];
46
            $look = [
47
                ':wiki:logo.png',
48
                ':logo.png',
49
                'images/logo.png',
50
                ':wiki:apple-touch-icon.png',
51
                ':apple-touch-icon.png',
52
                'images/apple-touch-icon.png',
53
                ':wiki:favicon.svg',
54
                ':favicon.svg',
55
                'images/favicon.svg',
56
                ':wiki:favicon.ico',
57
                ':favicon.ico',
58
                'images/favicon.ico',
59
                ':wiki:logo',
60
            ];
61
62
            $abs = true;
63
            foreach($look as $img) {
64
                if($img[0] === ':') {
65
                    $file    = mediaFN($img);
66
                    $ismedia = true;
67
                } else {
68
                    $file    = tpl_incdir().$img;
69
                    $ismedia = false;
70
                }
71
72
                if (file_exists($file)) {
73
                    $imginfo = getimagesize($file);
74
                    if($ismedia) {
75
                        $url = ml($img, '', true, '', $abs);
76
                    } else {
77
                        $url = tpl_basedir().$img;
78
                        if($abs) $url = DOKU_URL.substr($url, strlen(DOKU_REL));
79
                    }
80
                    $manifest['icons'][] = [
81
                        'src' => $url,
82
                        'sizes' => $imginfo[0] . 'x' . $imginfo[1],
83
                        'type' => $imginfo['mime'],
84
                    ];
85
                };
86
            }
87
        }
88
89
        trigger_event('MANIFEST_SEND', $manifest);
90
91
        header('Content-Type: application/manifest+json');
92
        echo json_encode($manifest);
93
    }
94
95
    public function jsonToArray($file)
96
    {
97
        $json = file_get_contents($file);
98
99
        $conf = json_decode($json, true);
100
101
        $jsonError = json_last_error();
102
        if (!is_array($conf) && $jsonError !== JSON_ERROR_NONE) {
103
104
            switch ($jsonError) {
105
                case JSON_ERROR_DEPTH:
106
                    $jsonErrorText = 'The maximum stack depth has been exceeded';
107
                    break;
108
                case JSON_ERROR_STATE_MISMATCH:
109
                    $jsonErrorText = 'Invalid or malformed JSON';
110
                    break;
111
                case JSON_ERROR_CTRL_CHAR:
112
                    $jsonErrorText = 'Control character error, possibly incorrectly encoded';
113
                    break;
114
                case JSON_ERROR_SYNTAX:
115
                    $jsonErrorText = 'Syntax error';
116
                    break;
117
                case JSON_ERROR_UTF8:
118
                    $jsonErrorText = 'Malformed UTF-8 characters, possibly incorrectly encoded';
119
                    break;
120
                case JSON_ERROR_RECURSION:
121
                    $jsonErrorText = 'One or more recursive references in the value to be encoded';
122
                    break;
123
                case JSON_ERROR_INF_OR_NAN:
124
                    $jsonErrorText = 'One or more NAN or INF values in the value to be encoded';
125
                    break;
126
                case JSON_ERROR_UNSUPPORTED_TYPE:
127
                    $jsonErrorText = 'A value of a type that cannot be encoded was given';
128
                    break;
129
                case JSON_ERROR_INVALID_PROPERTY_NAME:
130
                    $jsonErrorText = 'A property name that cannot be encoded was given';
131
                    break;
132
                case JSON_ERROR_UTF16:
133
                    $jsonErrorText = 'Malformed UTF-16 characters, possibly incorrectly encoded';
134
                    break;
135
                default:
136
                    $jsonErrorText = 'Unknown Error Code';
137
            }
138
139
            trigger_error('JSON decoding error "' . $jsonErrorText . '" for file ' . $file, E_USER_WARNING);
140
            return [];
141
        }
142
143
        return $conf;
144
    }
145
}
146
147
$manifest = new Manifest();
148
$manifest->run();
149