Completed
Push — master ( 593857...cd1c2d )
by Michael
29s queued 20s
created

Configuration   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 60.71%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 70
rs 10
c 0
b 0
f 0
ccs 17
cts 28
cp 0.6071
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A readSanitizerPreferences() 0 27 6
A saveSanitizerPrefrences() 0 8 3
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Text\Sanitizer;
13
14
use Xmf\Yaml;
15
16
/**
17
 * Provide a standard mechanism for a runtime registry for key/value pairs, useful
18
 * for attributes and parameters.
19
 *
20
 * @category  Sanitizer
21
 * @package   Xoops\Core\Text
22
 * @author    Richard Griffith <[email protected]>
23
 * @copyright 2013-2015 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 */
27
class Configuration extends ConfigurationAbstract
28
{
29
    /**
30
     * @var string config file with sanitizer prefs
31
     */
32
    private $sanitizerPrefsFilename = 'var/configs/system_sanitizer_prefs.yml';
33
34
    /**
35
     * Get the sanitizer configuration.
36
     */
37 3
    public function __construct()
38
    {
39 3
        $sanitizerConfiguration = $this->readSanitizerPreferences();
40 3
        parent::__construct($sanitizerConfiguration);
41 3
    }
42
43
    /**
44
     * readSanitizerPreferences - read configured sanitizer preferences
45
     *
46
     * If configuration file does not exist, create it.
47
     *
48
     * If configurable extensions exist that are not in the configuration
49
     * file, add them, and rewrite the configuration file.
50
     *
51
     * @return array of sanitizer preferences
52
     */
53 3
    protected function readSanitizerPreferences()
54
    {
55 3
        $xoops = \Xoops::getInstance();
56
57 3
        $sanitizerPrefs = array();
58
59
        try {
60 3
            $file = $xoops->path($this->sanitizerPrefsFilename);
61 3
            $sanitizerPrefs = Yaml::read($file);
62
        } catch (\Exception $e) {
63
            $xoops->events()->triggerEvent('core.exception', $e);
64
        }
65 3
        if (!is_array($sanitizerPrefs)) {
66
            $sanitizerPrefs = array();
67
        }
68 3
        $changed = false;
69 3
        $defaultPrefs = new DefaultConfiguration();
70 3
        foreach ($defaultPrefs as $name => $value) {
71 3
            if (!array_key_exists($name, $sanitizerPrefs)) {
0 ignored issues
show
Bug introduced by
It seems like $sanitizerPrefs can also be of type boolean; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            if (!array_key_exists($name, /** @scrutinizer ignore-type */ $sanitizerPrefs)) {
Loading history...
72
                $sanitizerPrefs[$name] = $defaultPrefs[$name];
73 3
                $changed = true;
74
            }
75
        }
76 3
        if ($changed) {
77
            $this->saveSanitizerPrefrences($sanitizerPrefs);
0 ignored issues
show
Bug introduced by
It seems like $sanitizerPrefs can also be of type boolean; however, parameter $sanitizerPrefs of Xoops\Core\Text\Sanitize...veSanitizerPrefrences() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $this->saveSanitizerPrefrences(/** @scrutinizer ignore-type */ $sanitizerPrefs);
Loading history...
78
        }
79 3
        return $sanitizerPrefs;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $sanitizerPrefs also could return the type boolean which is incompatible with the documented return type array.
Loading history...
80
    }
81
82
    /**
83
     * saveSanitizerPreferences - record array of sanitizer preferences in config file
84
     *
85
     * @param array $sanitizerPrefs array of sanitizer preferences to save
86
     *
87
     * @return void
88
     */
89
    protected function saveSanitizerPrefrences($sanitizerPrefs)
90
    {
91
        if (is_array($sanitizerPrefs)) {
0 ignored issues
show
introduced by
The condition is_array($sanitizerPrefs) is always true.
Loading history...
92
            $xoops = \Xoops::getInstance();
93
            try {
94
                Yaml::save($sanitizerPrefs, $xoops->path($this->sanitizerPrefsFilename));
95
            } catch (\Exception $e) {
96
                $xoops->events()->triggerEvent('core.exception', $e);
97
            }
98
        }
99
    }
100
}
101