Options   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A load() 0 20 2
B save() 0 30 3
1
<?php
2
3
/**
4
 * Handle options of plugins
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Datamapper
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\datamapper;
17
18
/**
19
 * Handle options of plugins
20
 *
21
 * @category  Core
22
 * @package   Datamapper
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Options
30
{
31
    /**
32
     * Name of target plugin
33
     **/
34
    private $_plugin;
35
36
    /**
37
     * Database service object
38
     **/
39
    private $_database = null;
40
41
    /**
42
     * Prepare values that are needed for later usage
43
     *
44
     * @param string $plugin Plugin name
45
     *
46
     * @return \csphere\core\datamapper\Options
47
     **/
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...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
48
49
    public function __construct($plugin)
50
    {
51
        // Set plugin name
52
        $this->_plugin = $plugin;
53
54
        // Get database service object
55
        $loader = \csphere\core\service\Locator::get();
56
57
        $this->_database = $loader->load('database');
58
    }
59
60
    /**
61
     * Load all options of a plugin
62
     *
63
     * @return array
64
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
65
66
    public function load()
67
    {
68
        // Construct query and fetch result
69
        $columns = 'option_name, option_value';
70
        $where   = ['option_plugin', '=', $this->_plugin];
71
72
        $sql = \csphere\core\sql\DML::select('options', '', $columns, $where);
73
74
        $all = $this->_database->query($sql['statement'], $sql['input'], 0, 0);
75
76
        // Format array for easier usage
77
        $options = [];
78
79
        foreach ($all AS $one) {
80
81
            $options[$one['option_name']] = $one['option_value'];
82
        }
83
84
        return $options;
85
    }
86
87
    /**
88
     * Save all options of a plugin
89
     *
90
     * @param array $options Options as an array
91
     *
92
     * @return boolean
93
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
94
95
    public function save(array $options)
96
    {
97
        // Construct query and fetch serials
98
        $columns = 'option_id, option_name';
99
        $where   = ['option_plugin', '=', $this->_plugin];
100
101
        $sql = \csphere\core\sql\DML::select('options', '', $columns, $where);
102
103
        $all = $this->_database->query($sql['statement'], $sql['input'], 0, 0);
104
105
        // Update plugin options
106
        foreach ($all AS $one) {
107
108
            $name = $one['option_name'];
109
110
            // Check if option was given
111
            if (isset($options[$name])) {
112
113
                $assoc = ['option_value' => $options[$name]];
114
115
                $sql = \csphere\core\sql\DML::update(
116
                    'options', $assoc, 'option_id', $one['option_id']
117
                );
118
119
                $this->_database->exec($sql['statement'], $sql['input']);
120
            }
121
        }
122
123
        return true;
124
    }
125
}
126