Intraface_Setting::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 1
1
<?php
2
/**
3
 * Handles Settings in the system
4
 *
5
 * Tabel fields: id, intranet_id, user_id, setting, value, sub_id
6
 * Settings levels: System, Intranet, User
7
 *
8
 * @author Sune Jensen <[email protected]>
9
 */
10
require_once 'Intraface/functions.php';
11
12
class Intraface_Setting
13
{
14
    /**
15
     * @var object
16
     */
17
    private $db;
18
19
    /**
20
     * @var array
21
     */
22
    private $system;
23
24
    /**
25
     * @var integer
26
     */
27
    private $user_id;
28
29
    /**
30
     * @var integer
31
     */
32
    private $intranet_id;
33
34
    /**
35
     * @var array
36
     */
37
    protected $settings = array();
38
39
    /**
40
     * @var boolean
41
     */
42
    protected $is_loaded = false;
43
44
    /**
45
     * Checks whether setting is in system
46
     *
47
     * @param integer $intranet_id
48
     * @param integer $user_id
49
     *
50
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
51
     */
52 14
    function __construct($intranet_id, $user_id = 0)
53
    {
54 14
        global $_setting;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
55
56 14
        require_once dirname(__FILE__) . '/config/setting_kernel.php';
57
58 14
        $this->db = new DB_Sql;
59 14
        $this->system = &$_setting; // don't remove the & - otherwise it will not work
60 14
        $this->user_id = (int)$user_id;
61 14
        $this->intranet_id = (int)$intranet_id;
62 14
    }
63
64
    /**
65
     * Checks whether setting is in system
66
     *
67
     * @param string $setting to test
68
     *
69
     * @return boolean or throws exception
70
     */
71 13
    private function checkSystem($setting)
72
    {
73 13
        if (!empty($setting) && is_array($this->system) && isset($this->system[$setting])) {
74 12
            return true;
75
        } else {
76 1
            throw new Exception('Setting "'.$setting.'" is not defined');
77
        }
78
    }
79
80
    /**
81
     * Checks whether type is a valid type
82
     *
83
     * @param string $type to test
84
     *
85
     * @return boolean or throws exception
86
     */
87 12
    private function checkType($type)
88
    {
89 12
        if ($type == 'system' || $type == 'intranet' || $type == 'user') {
90 12
            return true;
91
        } else {
92
            throw new Exception('Ugyldig type setting "'.$type.'"');
93
        }
94
    }
95
96
    /**
97
     * Checks whether the user is logged in
98
     *
99
     * @return boolean
100
     */
101 12
    private function checkLogin()
102
    {
103 12
        if ($this->user_id != 0) {
104 12
            return true;
105
        } else {
106
            throw new Exception('This action cannot be performed when no user has logged in');
107
        }
108
    }
109
110
    /**
111
     * Sets a certain setting
112
     *
113
     * @access protected, however to be tested we kept it public
114
     *
115
     * @param string  $type    Can be either system, intranet, user
116
     * @param string  $setting The actual setting
117
     * @param integer $sub_id  @todo What is this exactly
118
     *
119
     * @return boolean
120
     */
121 12
    function set($type, $setting, $value, $sub_id = 0)
122
    {
123
124 12
        if ($this->checkSystem($setting) && $this->checkType($type) && $this->checkLogin()) {
125
            switch ($type) {
126 11
                case 'system':
127 1
                    throw new Exception('Du kan ikke ændre på systemsetting');
128
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
129 10
                case 'intranet':
130 4
                    $this->db->query("SELECT id FROM setting WHERE setting = ".$this->db->quote($setting, 'text')." AND intranet_id = ".$this->intranet_id." AND user_id = 0 AND sub_id = ".intval($sub_id));
131 4 View Code Duplication
                    if ($this->db->nextRecord()) {
132 1
                        $this->db->query("UPDATE setting SET value = ".$this->db->quote($value, 'text')." WHERE id = ".$this->db->quote($this->db->f("id"), 'integer'));
133 1
                    } else {
134 4
                        $this->db->query("INSERT INTO setting SET value = ".$this->db->quote($value, 'text').", setting = ".$this->db->quote($setting, 'text').", intranet_id = ".$this->db->quote($this->intranet_id, 'integer').", user_id = 0, sub_id = ".intval($sub_id));
135
                    }
136 4
                    $this->settings[$this->intranet_id][0][$setting][$sub_id] = $value;
137 4
                    break;
138 9
                case 'user':
139 9
                    if ($this->checkSystem($setting)) {
140 9
                        $this->db->query("SELECT id FROM setting WHERE setting = ".$this->db->quote($setting, 'text')." AND intranet_id = ".$this->db->quote($this->intranet_id, 'integer')." AND user_id = ".$this->db->quote($this->user_id, 'integer')." AND sub_id = ".intval($sub_id));
141 9 View Code Duplication
                        if ($this->db->nextRecord()) {
142 1
                            $this->db->query("UPDATE setting SET value = ".$this->db->quote($value, 'text')." WHERE id = ".$this->db->quote($this->db->f("id"), 'integer'));
143 1
                        } else {
144 9
                            $this->db->query("INSERT INTO setting SET value = ".$this->db->quote($value, 'text').", setting = ".$this->db->quote($setting, 'text').", intranet_id = ".$this->intranet_id.", user_id = ".$this->db->quote($this->user_id, 'integer').", sub_id = ".intval($sub_id));
145
                        }
146 9
                    }
147 9
                    $this->settings[$this->intranet_id][$this->user_id][$setting][$sub_id] = $value;
148 9
                    break;
149
            }
150 10
            return true;
151
        }
152
        return false;
153
    }
154
155
    /**
156
     * Checks whether a setting is set
157
     *
158
     * @return array
159
     */
160 1
    public function getSettings()
161
    {
162 1
        return $this->settings;
163
    }
164
165
    /**
166
     * Loads settings
167
     *
168
     * @return void
169
     */
170 7
    private function loadSettings()
171
    {
172 7
        $this->settings = array();
173 7
        $this->db->query("SELECT setting, value, sub_id, user_id FROM setting WHERE intranet_id = " . $this->db->quote($this->intranet_id, 'integer')." AND (user_id = ".$this->db->quote($this->user_id, 'integer')." OR user_id = 0)");
174 7
        while ($this->db->nextRecord()) {
175 6
            $this->settings[$this->intranet_id][$this->db->f('user_id')][$this->db->f('setting')][$this->db->f('sub_id')] = $this->db->f('value');
176 6
        }
177 7
        $this->is_loaded = true;
178 7
    }
179
180
    /**
181
     * Returns whether the settings has already been loaded
182
     *
183
     * @return boolean
184
     */
185 7
    private function isLoaded()
186
    {
187 7
        return $this->is_loaded;
188
    }
189
190
    /**
191
     * Gets a certain setting
192
     *
193
     * @access protected, however to be tested we kept it public
194
     *
195
     * @param string  $type    Can be either system, intranet, user
196
     * @param string  $setting The actual setting
197
     * @param integer $sub_id  @todo What is this exactly
198
     *
199
     * @return boolean
200
     */
201 7
    public function get($type, $setting, $sub_id = 0)
202
    {
203 7
        if (!$this->isLoaded()) {
204 6
            $this->loadSettings();
205 6
        }
206
207 7
        if ($this->checkSystem($setting) && $this->checkType($type)) {
208
            switch ($type) {
209 7
                case 'user':
210 7
                    if ($this->checkLogin()) {
211
                        // hvis der ikke er nogen intranet-indstillinger på posten vil den stadig
212
                        // blive ved med at lave opslaget. Hvordan undgør vi lige det på en god og sikker måde?
213
                        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
214
                        if (!isset($this->settings['user'])) {
215
                            $this->settings['user'] = array();
216
                            $this->db->query("SELECT setting, value, sub_id FROM setting WHERE intranet_id = ".$this->intranet_id." AND user_id = ".$this->user_id);
217
                            while ($this->db->nextRecord()) {
218
                                $this->settings['user'][$this->db->f('setting')][$this->db->f('sub_id')] = $this->db->f('value');
219
                            }
220
221
                        }
222
                        */
223 7
                        if (isset($this->settings[$this->intranet_id][$this->user_id][$setting][intval($sub_id)])) {
224 5
                            return $this->settings[$this->intranet_id][$this->user_id][$setting][intval($sub_id)];
225
                        }
226 2
                    }
227
                    // no break because it has to fall through if user is not set
228 5
                case 'intranet':
229
                    // hvis der ikke er nogen intranet-indstillinger p� posten vil den stadig
230
                    // blive ved med at lave opslaget. Hvordan undg�r vi lige det.
231
                    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
232
                    if (!isset($this->settings['intranet'])) {
233
                        $this->settings['intranet'] = array();
234
                        $this->db->query("SELECT setting, value, sub_id FROM setting WHERE intranet_id = ".$this->intranet_id." AND user_id = 0");
235
236
                        while ($this->db->nextRecord()) {
237
                            $this->settings['intranet'][$this->db->f('setting')][intval($this->db->f('sub_id'))] = $this->db->f('value');
238
                        }
239
240
                    }
241
                    */
242 5
                    if (isset($this->settings[$this->intranet_id][0][$setting][intval($sub_id)])) {
243 4
                        return $this->settings[$this->intranet_id][0][$setting][intval($sub_id)];
244
                    }
245
                    // no break because it has to fall through if intranet is not set
246 3
                default:
247 3
                    return $this->system[$setting];
248
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
249 2
            }
250
        }
251
    }
252
253
    /**
254
     * Checks whether a setting is set
255
     *
256
     * @access protected, however to be tested we kept it public
257
     *
258
     * @param string  $type    Can be either system, intranet, user
259
     * @param string  $setting The actual setting
260
     * @param integer $sub_id  @todo What is this exactly
261
     *
262
     * @return boolean
263
     */
264 3
    function isSettingSet($type, $setting, $sub_id = 0)
265
    {
266 3
        if ($this->checkSystem($setting) && $this->checkType($type)) {
267
            switch ($type) {
268 3
                case 'user':
269 3
                    if ($this->checkLogin()) {
270 3
                        $this->db->query("SELECT value FROM setting WHERE setting = \"".$setting."\" AND intranet_id = ".$this->intranet_id." AND user_id = ".$this->user_id." AND sub_id = ".intval($sub_id));
271 3
                        if ($this->db->nextRecord()) {
272 1
                            return true;
273
                        }
274 2
                    }
275 2
                    break;
276
277
                case 'intranet':
278
                    $this->db->query("SELECT value FROM setting WHERE setting = \"".$setting."\" AND intranet_id = ".$this->intranet_id." AND user_id = 0 AND sub_id = ".intval($sub_id));
279
                    if ($this->db->nextRecord()) {
280
                        return true;
281
                    }
282
                    break;
283
284
                default:
285
                    return true;
286
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
287
            }
288 2
        }
289 2
        return false;
290
    }
291
292
    /**
293
     * Deletes a setting
294
     *
295
     * @access protected, however to be tested we kept it public
296
     *
297
     * @param string  $type    Can be either system, intranet, user
298
     * @param string  $setting The actual setting
299
     * @param integer $sub_id  @todo What is this exactly
300
     *
301
     * @return boolean
302
     */
303 1
    function delete($type, $setting, $sub_id = 0)
304
    {
305
306 1
        if ($this->checkSystem($setting) && $this->checkType($type) && $this->checkLogin()) {
307 1
            if ($sub_id == 'ALL') {
308 1
                $sql_sub = '';
309 1
            } else {
310
                $sql_sub = "AND sub_id = ".intval($sub_id);
311
            }
312
313
            switch ($type) {
314 1 View Code Duplication
                case 'user':
315 1
                    $this->db->query("DELETE FROM setting WHERE setting = \"".$setting."\" AND intranet_id = ".$this->intranet_id." AND user_id = ".$this->user_id." ".$sql_sub);
316 1
                    $return = true;
317 1
                    break;
318
319 View Code Duplication
                case 'intranet':
320
                    $this->db->query("DELETE FROM setting WHERE setting = \"".$setting."\" AND intranet_id = ".$this->intranet_id." ".$sql_sub);
321
                    $return = true;
322
                    break;
323
324
                default:
325
                    throw new Exception('Du kan ikke slette en system setting');
326
                    return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
327
            }
328 1
        }
329
330 1
        $this->loadSettings();
331
332 1
        return $return;
0 ignored issues
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
333
    }
334
}
335