Settings::getOverrides()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Settings.php
9
 */
10
namespace JaegerApp;
11
12
/**
13
 * Jaeger - Settings Object
14
 *
15
 * Abstract object to manage system settings
16
 *
17
 * @package Settings
18
 * @author Eric Lamb <[email protected]>
19
 */
20
abstract class Settings
21
{
22
    /**
23
     * The Settings array
24
     * 
25
     * @var array
26
     */
27
    protected $settings = null;
28
29
    /**
30
     * The name of the Settings table
31
     * 
32
     * @var string
33
     */
34
    protected $table = '';
35
36
    /**
37
     * Global JaegerApp product settings
38
     * 
39
     * @var array
40
     */
41
    protected $_global_defaults = array(
42
        'date_format' => 'M d, Y, h:i:sA',
43
        'relative_time' => '1',
44
        'enable_rest_api' => '0',
45
        'api_key' => '',
46
        'api_secret' => '',     
47
        'api_debug' => 0,
48
        'license_number' => '',
49
        'license_check' => 0,
50
        'license_status' => ''
51
    );
52
53
    /**
54
     * The settings keys that should be serialized for storage
55
     * 
56
     * @var array
57
     */
58
    protected $serialized = array();
59
60
    /**
61
     * The settings keys that have a custom option available
62
     * 
63
     * @var array
64
     */
65
    protected $custom_options = array();
66
67
    /**
68
     * The settings, if any, that should be encrypted for storage
69
     * 
70
     * @var array
71
     */
72
    protected $encrypted = array();
73
74
    /**
75
     * The default settings storage variable
76
     * 
77
     * @var array
78
     */
79
    protected $defaults = array();
80
81
    /**
82
     * The settings, if any, that should be exploded into arrays on storage
83
     * 
84
     * @var array
85
     */
86
    protected $new_lines = array();
87
88
    /**
89
     * The file system based overrides if any
90
     *
91
     * Used to set configuration and settings through file based configuration
92
     * 
93
     * @var array
94
     */
95
    protected $overrides = array();
96
97
    /**
98
     * The encryption object to use for storage
99
     * 
100
     * @var \JaegerApp\Encrypt
101
     */
102
    protected $encrypt = null;
103
104
    /**
105
     * The lanaguage instance
106
     * 
107
     * @var \JaegerApp\Language
108
     */
109
    protected $lang = null;
110
111
    /**
112
     * The database instance
113
     * 
114
     * @var \JaegerApp\Db
115
     */
116
    protected $db = null;
117
118
    /**
119
     * Sets it up
120
     * 
121
     * @param \JaegerApp\Db $db            
122
     * @param \JaegerApp\Language $lang            
123
     */
124
    public function __construct(\JaegerApp\Db $db, \JaegerApp\Language $lang) 
125
    {
126
        $this->db = $db;
127
        $this->lang = $lang;
128
    }
129
130
    /**
131
     * Will validate the passed setting data for errors
132
     * 
133
     * @param array $data The data to valiate
134
     * @param array $extra Any extra data to provide context for the $data        
135
     */
136
    abstract public function validate(array $data, array $extra = array());
137
138
    /**
139
     * Sets the default settings values
140
     * 
141
     * @param array $defaults            
142
     * @return \JaegerApp\Settings
143
     */
144
    public function setDefaults(array $defaults)
145
    {
146
        $this->defaults = array_merge($this->_global_defaults, $defaults, $this->defaults);
147
        return $this;
148
    }
149
150
    /**
151
     * Returns the default settings
152
     * 
153
     * @return array
154
     */
155
    public function getDefaults()
156
    {
157
        return $this->defaults;
158
    }
159
160
    /**
161
     * Sets the table we're using
162
     * 
163
     * @param string $table            
164
     * @return \JaegerApp\Settings
165
     */
166
    public function setTable($table)
167
    {
168
        $this->table = $table;
169
        return $this;
170
    }
171
172
    /**
173
     * Returns the Settings table name
174
     * 
175
     * @return string
176
     */
177
    public function getTable()
178
    {
179
        return $this->table;
180
    }
181
182
    /**
183
     * Returns the settings that have custom options
184
     * 
185
     * @return array
186
     */
187
    public function getCustomOptions()
188
    {
189
        return $this->custom_options;
190
    }
191
192
    /**
193
     * Returns the available overrides
194
     * 
195
     * @return array
196
     */
197
    public function getOverrides()
198
    {
199
        return $this->overrides;
200
    }
201
202
    /**
203
     * Sets the overrides to use
204
     * 
205
     * @param array $overrides            
206
     * @return \JaegerApp\Settings
207
     */
208
    public function setOverrides(array $overrides = array())
209
    {
210
        $this->overrides = $overrides;
211
        return $this;
212
    }
213
214
    /**
215
     * Retrns the encryption object
216
     * 
217
     * @return \JaegerApp\Encrypt
218
     */
219
    public function getEncrypt()
220
    {
221
        return $this->encrypt;
222
    }
223
224
    /**
225
     * Sets the encryption object we're using
226
     * 
227
     * @param \JaegerApp\Encrypt $encrypt            
228
     * @return \JaegerApp\Settings
229
     */
230
    public function setEncrypt(\JaegerApp\Encrypt $encrypt)
231
    {
232
        $this->encrypt = $encrypt;
233
        return $this;
234
    }
235
236
    /**
237
     * Sets the encrypted setting keys
238
     * 
239
     * @param array $encrypted            
240
     * @return \JaegerApp\Settings
241
     */
242
    public function setEncrypted(array $encrypted = array())
243
    {
244
        $this->encrypted = $encrypted;
245
        return $this;
246
    }
247
248
    /**
249
     * Returns the settings keys to be encrypted
250
     * 
251
     * @return \JaegerApp\array
252
     */
253
    public function getEncrypted()
254
    {
255
        return $this->encrypted;
256
    }
257
258
    /**
259
     * Returns the serialized setting options
260
     * 
261
     * @return array
262
     */
263
    public function getSerialized()
264
    {
265
        return $this->serialized;
266
    }
267
268
    /**
269
     * Sets the serialized setting options
270
     * 
271
     * @param array $serialized            
272
     * @return \JaegerApp\Settings
273
     */
274
    public function setSerialized(array $serialized = array())
275
    {
276
        $this->serialized = $serialized;
277
        return $this;
278
    }
279
280
    /**
281
     * Returns the new lined system settings
282
     * 
283
     * @return array
284
     */
285
    public function getNewLines()
286
    {
287
        return $this->new_lines;
288
    }
289
290
    /**
291
     * Sets the new lined system settings
292
     * 
293
     * @param array $nl            
294
     * @return \mithra62\Settings
295
     */
296
    public function setNewLines(array $nl = array())
297
    {
298
        $this->new_lines = $nl;
299
        return $this;
300
    }
301
302
    /**
303
     * Saves the settings to the database
304
     * 
305
     * @param array $data            
306
     */
307
    public function update(array $data)
308
    {
309
        // setup the custom options
310
        foreach ($this->getCustomOptions() as $key => $value) {
311
            if (isset($data[$value]) && $data[$value] == 'custom' && $data[$value . '_custom'] != '') {
312
                $data['_form_' . $value] = $data[$value];
313
                $data[$value] = $data[$value . '_custom'];
314
            }
315
        }
316
        
317
        foreach ($data as $key => $value) {
318
            if (in_array($key, $this->getSerialized())) {
319
                $value = (is_array($value) ? serialize($value) : serialize(array(
320
                    $value
321
                )));
322
            }
323
            
324
            if (in_array($key, $this->getEncrypted()) && $value != '') {
325
                $value = $this->getEncrypt()->encode($value);
326
            }
327
            
328
            $this->updateSetting($key, $value);
329
        }
330
        
331
        return true;
332
    }
333
334
    /**
335
     * Updates the value of a setting
336
     * 
337
     * @param string $key            
338
     * @param string $value            
339
     */
340
    public function updateSetting($key, $value)
341
    {
342
        if (! $this->checkSetting($key)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->checkSetting($key) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
343
            return false;
344
        }
345
        
346
        $data = array();
347
        if (is_array($value)) {
348
            $value = serialize($value);
349
            $data['serialized '] = '1';
350
        }
351
        
352
        $data['setting_value'] = $value;
353
        return $this->db->update($this->getTable(), $data, array(
0 ignored issues
show
Documentation introduced by
array('setting_key' => $key) is of type array<string,string,{"setting_key":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
354
            'setting_key' => $key
355
        ));
356
    }
357
358
    /**
359
     * Verifies that a submitted setting is valid and exists.
360
     * If it's valid but doesn't exist it is created.
361
     * 
362
     * @param string $setting            
363
     */
364
    private function checkSetting($setting)
365
    {
366
        if (array_key_exists($setting, $this->getDefaults())) {
367
            if (! $this->getSetting($setting)) {
368
                $this->addSetting($setting);
369
            }
370
            
371
            return true;
372
        }
373
    }
374
375
    /**
376
     * Returns the system settings
377
     * 
378
     * @return array
379
     */
380
    public function get($force = false)
381
    {
382
        if (is_null($this->settings) || $force) {
383
            $_settings = $this->db->select()
384
                ->from($this->getTable())
385
                ->get();
386
            $settings = array();
387
            foreach ($_settings as $setting) {
388
                // decrypt the value if needed
389
                if (in_array($setting['setting_key'], $this->getEncrypted()) && ! empty($setting['setting_value'])) {
390
                    $settings[$setting['setting_key']] = $setting['setting_value'] = $this->getEncrypt()->decode($setting['setting_value']);
391
                }
392
                
393
                // unserialize the value
394
                if (in_array($setting['setting_key'], $this->getSerialized()) && ! empty($setting['setting_value'])) {
395
                    $settings[$setting['setting_key']] = $setting['setting_value'] = unserialize($setting['setting_value']);
396
                    foreach ($settings[$setting['setting_key']] as $key => $value) {
397
                        if ($value == '') {
398
                            unset($settings[$setting['setting_key']][$key]); // remove blank entries
399
                        }
400
                    }
401
                }
402
                
403
                // sort out new line values
404
                if (in_array($setting['setting_key'], $this->getNewLines())) {
405
                    $settings[$setting['setting_key']] = $setting['setting_value'] = explode("\n", $setting['setting_value']);
406
                    foreach ($settings[$setting['setting_key']] as $key => $value) {
407
                        $value = trim($value);
408
                        if ($value == '') {
409
                            unset($settings[$setting['setting_key']][$key]); // remove blank entries
410
                        } else {
411
                            $settings[$setting['setting_key']][$key] = $value;
412
                        }
413
                    }
414
                }
415
                
416
                $settings[$setting['setting_key']] = $setting['setting_value'];
417
            }
418
            
419
            $ignore = array(
420
                'license_check',
421
                'license_status'
422
            );
423
            foreach ($this->getDefaults() as $key => $value) {
424
                // setup the override check
425
                if (isset($this->overrides[$key]) && ! in_array($key, $ignore)) {
426
                    $settings[$key] = $this->overrides[$key];
427
                }
428
                
429
                if (! isset($settings[$key])) {
430
                    $settings[$key] = $value;
431
                }
432
            }
433
            
434
            $this->settings = $settings;
435
            
436
            if($this->settings['api_key'] == '')
437
            {
438
                $this->settings['api_key'] = $this->getEncrypt()->guid();
439
            }
440
            
441
            if($this->settings['api_secret'] == '')
442
            {
443
                $this->settings['api_secret'] = $this->getEncrypt()->guid();
444
            }            
445
        }
446
        
447
        return $this->settings;
448
    }
449
450
    /**
451
     * Checks the database for a setting key
452
     * 
453
     * @param string $setting            
454
     */
455
    public function getSetting($setting)
456
    {
457
        return $this->db->select()
458
            ->from($this->getTable())
459
            ->where(array(
460
            'setting_key' => $setting
461
        ))
462
            ->get();
463
    }
464
465
    /**
466
     * Adds a setting to the databse
467
     * 
468
     * @param string $setting            
469
     */
470
    public function addSetting($setting)
471
    {
472
        $data = array(
473
            'setting_key' => $setting
474
        );
475
        
476
        if (in_array($setting, $this->getSerialized())) {
477
            $data['serialized'] = '1';
478
        }
479
        
480
        return $this->db->insert($this->getTable(), $data);
481
    }
482
}