AppConfig   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 114
ccs 0
cts 51
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseConfig() 0 5 1
A getConfig() 0 8 2
A registerNavigation() 0 22 2
A registerHooks() 0 12 2
A __construct() 0 6 1
A loadConfig() 0 7 2
A registerAll() 0 8 1
1
<?php
2
/**
3
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\News\Config;
15
16
use SimpleXMLElement;
17
18
use OCP\INavigationManager;
19
use OCP\IURLGenerator;
20
use OCP\Util;
21
use OCP\App;
22
23
// Used to parse app.json file, should be in core at some point
24
class AppConfig {
25
26
    private $config;
27
    private $navigationManager;
28
    private $urlGenerator;
29
30
    /**
31
     * TODO: External deps that are needed:
32
     * - add jobs
33
     * - connect to hooks
34
     */
35
    public function __construct(INavigationManager $navigationManager,
36
                                IURLGenerator $urlGenerator) {
37
        $this->navigationManager = $navigationManager;
38
        $this->urlGenerator = $urlGenerator;
39
        $this->config = [];
40
    }
41
42
43
    /**
44
     * Parse an xml config
45
     */
46
    private function parseConfig($string) {
47
        // no need to worry about XXE since local file
48
        $xml = simplexml_load_string($string, 'SimpleXMLElement');
49
        return json_decode(json_encode((array)$xml), true);
50
    }
51
52
53
    /**
54
     * @param string|array $data path to the config file or an array with the
55
     * config
56
     */
57
    public function loadConfig($data) {
58
        if(is_array($data)) {
59
            $this->config = $data;
60
        } else {
61
            $this->config = $this->parseConfig($data);
62
        }
63
    }
64
65
66
    /**
67
     * @param string $key if given returns the value of the config at index $key
68
     * @return array|mixed the config
69
     */
70
    public function getConfig($key=null) {
71
        // FIXME: is this function interface a good idea?
72
        if($key !== null) {
73
            return $this->config[$key];
74
        } else {
75
            return $this->config;
76
        }
77
    }
78
79
80
    /**
81
     * Registers all config options
82
     */
83
    public function registerAll() {
84
        $this->registerNavigation();
85
        $this->registerHooks();
86
        // IJob API is fucked up, so silence the code checker
87
        $class = '\OCP\BackgroundJob';
88
        $class::addRegularTask($this->config['cron']['job'], 'run');
89
        App::registerAdmin($this->config['id'], $this->config['admin']);
90
    }
91
92
93
    /**
94
     * Parses the navigation and creates a navigation entry if needed
95
     */
96
    public function registerNavigation() {
97
        if (array_key_exists('navigation', $this->config)) {
98
            $this->navigationManager->add(function () {
99
                $nav =& $this->config['navigation'];
100
101
                $navConfig = [
102
                    'id' => $this->config['id'],
103
                    'order' => $nav['order'],
104
                    'name' => $nav['name']
105
                ];
106
107
                $navConfig['href'] = $this->urlGenerator->linkToRoute(
108
                    $nav['route']
109
                );
110
                $navConfig['icon'] = $this->urlGenerator->imagePath(
111
                    $this->config['id'], $nav['icon']
112
                );
113
114
                return $navConfig;
115
            });
116
        }
117
    }
118
119
120
    /**
121
     * Registers all hooks in the config
122
     */
123
    public function registerHooks() {
124
        // FIXME: this is temporarily static because core emitters are not
125
        // future proof, therefore legacy code in here
126
        foreach ($this->config['hooks'] as $hook) {
127
            $listener = explode('::', $hook['channel']);
128
            $reaction = explode('::', $hook['subscriber']);
129
130
            // config is written like HookNamespace::method => Class::method
131
            Util::connectHook($listener[0], $listener[1], $reaction[0],
132
                                   $reaction[1]);
133
        }
134
    }
135
136
137
}
138