Options::deleteOption()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Options class file
4
 *
5
 * @package EBloodBank
6
 * @since   1.0
7
 */
8
namespace EBloodBank;
9
10
use InvalidArgumentException;
11
use EBloodBank\Models\Variable;
12
13
/**
14
 * Options class
15
 *
16
 * @since 1.0
17
 */
18
class Options
19
{
20
    /**
21
     * @access private
22
     * @since 1.0
23
     */
24
    private function __construct()
25
    {
26
    }
27
28
    /**
29
     * Get an option.
30
     *
31
     * @return mixed
32
     * @since 1.0
33
     * @static
34
     */
35
    public static function getOption($name)
36
    {
37
        $em = Main::getInstance()->getEntityManager();
38
39
        if (getInstallationStatus($em->getConnection()) !== DATABASE_INSTALLED) {
40
            return;
41
        }
42
43
        $var = $em->find('Entities:Variable', $name);
44
45
        if (! empty($var)) {
46
            return $var->get('value');
47
        }
48
    }
49
50
    /**
51
     * Add a new option.
52
     *
53
     * @return bool
54
     * @since 1.0
55
     * @static
56
     */
57
    public static function addOption($name, $value, bool $sanitize = false, bool $validate = true)
58
    {
59
        if ($sanitize) {
60
            $value = self::sanitizeOption($name, $value);
61
        }
62
63
        if ($validate && ! self::validateOption($name, $value)) {
64
            return false;
65
        }
66
67
        $em = Main::getInstance()->getEntityManager();
68
69
        if (getInstallationStatus($em->getConnection()) !== DATABASE_INSTALLED) {
70
            return false;
71
        }
72
73
        $var = $em->find('Entities:Variable', $name);
74
75
        if (! empty($var)) {
76
            return false;
77
        }
78
79
        $var = new Variable();
80
        $var->set('name', $name, $sanitize, $validate);
81
        $var->set('value', $value, $sanitize, $validate);
82
83
        $em->persist($var);
84
        $em->flush();
85
86
        return true;
87
    }
88
89
    /**
90
     * Update an existing option.
91
     *
92
     * @return bool
93
     * @since 1.0
94
     * @static
95
     */
96
    public static function updateOption($name, $value, bool $sanitize = false, bool $validate = true)
97
    {
98
        if ($sanitize) {
99
            $value = self::sanitizeOption($name, $value);
100
        }
101
102
        if ($validate && ! self::validateOption($name, $value)) {
103
            return false;
104
        }
105
106
        $em = Main::getInstance()->getEntityManager();
107
108
        if (getInstallationStatus($em->getConnection()) !== DATABASE_INSTALLED) {
109
            return false;
110
        }
111
112
        $var = $em->find('Entities:Variable', $name);
113
114
        if (empty($var)) {
115
            return false;
116
        }
117
118
        $var->set('value', $value, $sanitize, $validate);
119
120
        $em->flush($var);
121
122
        return true;
123
    }
124
125
    /**
126
     * Submit an option.
127
     *
128
     * @return bool
129
     * @since 1.0
130
     * @static
131
     */
132
    public static function submitOption($name, $value, bool $sanitize = false, bool $validate = true)
133
    {
134
        if ('' === strval($value)) {
135
            return self::deleteOption($name);
136
        } else {
137
            $currentOption = self::getOption($name);
138
            if (is_null($currentOption)) {
139
                return self::addOption($name, $value, $sanitize, $validate);
140
            } else {
141
                return self::updateOption($name, $value, $sanitize, $validate);
142
            }
143
        }
144
    }
145
146
    /**
147
     * Delete an option.
148
     *
149
     * @return bool
150
     * @since 1.0
151
     * @static
152
     */
153
    public static function deleteOption($name)
154
    {
155
        $em = Main::getInstance()->getEntityManager();
156
157
        if (getInstallationStatus($em->getConnection()) !== DATABASE_INSTALLED) {
158
            return false;
159
        }
160
161
        $var = $em->getReference('Entities:Variable', $name);
162
163
        if (empty($var)) {
164
            return false;
165
        }
166
167
        $em->remove($var);
168
        $em->flush();
169
170
        return true;
171
    }
172
173
    /**
174
     * Sanitize an option value.
175
     *
176
     * @return mixed
177
     * @since 1.0
178
     * @static
179
     */
180
    public static function sanitizeOption($name, $value)
181
    {
182
        switch ($name) {
183
            case 'site_url':
184
                $value = sanitizeURL($value);
185
                break;
186
            case 'site_name':
187
            case 'site_slogan':
188
            case 'site_locale':
189
            case 'site_theme':
190
                $value = sanitizeTitle($value);
191
                break;
192
            case 'site_email':
193
                $value = sanitizeEmail($value);
194
                break;
195
            case 'self_registration':
196
                break;
197
            case 'new_user_role':
198
                break;
199
            case 'new_user_status':
200
                break;
201
            case 'entities_per_page':
202
                $value = sanitizeInteger($value);
203
                break;
204
        }
205
        return $value;
206
    }
207
208
    /**
209
     * Validate an option value.
210
     *
211
     * @return bool
212
     * @since 1.0
213
     * @static
214
     */
215
    public static function validateOption($name, $value)
216
    {
217
        switch ($name) {
218
            /* Site Options */
219
220
            case 'site_url':
221
                if (empty($value) || ! isValidURL($value)) {
222
                    throw new InvalidArgumentException(__('Invalid site URL.'));
223
                }
224
                break;
225
            case 'site_name':
226
                if (empty($value) || ! is_string($value)) {
227
                    throw new InvalidArgumentException(__('Invalid site name.'));
228
                }
229
                break;
230
            case 'site_slogan':
231
                break;
232
            case 'site_email':
233
                if (empty($value) || ! isValidEmail($value)) {
234
                    throw new InvalidArgumentException(__('Invalid site e-mail address.'));
235
                }
236
                break;
237
            case 'site_locale':
238
                if (! empty($value)) {
239
                    $locale = Locales::findLocale($value);
240
                    if (empty($locale)) {
241
                        throw new InvalidArgumentException(__('Could not find the locale.'));
242
                    }
243
                }
244
                break;
245
            case 'site_theme':
246
                if (! empty($value)) {
247
                    $theme = Themes::findTheme($value);
248
                    if (empty($theme)) {
249
                        throw new InvalidArgumentException(__('Could not find the theme.'));
250
                    }
251
                }
252
                break;
253
254
            /* Users Options */
255
256
            case 'self_registration':
257
                if (empty($value) || ! in_array($value, ['on', 'off'])) {
258
                    throw new InvalidArgumentException(__('Invalid self-registration status.'));
259
                }
260
                break;
261
            case 'new_user_role':
262
                if (empty($value)) {
263
                    throw new InvalidArgumentException(__('Invalid new user role.'));
264
                }
265
                break;
266
            case 'new_user_status':
267
                if (empty($value) || ! in_array($value, ['pending', 'activated'])) {
268
                    throw new InvalidArgumentException(__('Invalid new user status.'));
269
                }
270
                break;
271
272
            /* Donors Options */
273
274
            case 'default_donor_email_visibility':
275
                if (! empty($value) && ! array_key_exists($value, getVisibilities())) {
276
                    throw new InvalidArgumentException(__('Invalid donor e-mail address visibility.'));
277
                }
278
                break;
279
280
            case 'default_donor_phone_visibility':
281
                if (! empty($value) && ! array_key_exists($value, getVisibilities())) {
282
                    throw new InvalidArgumentException(__('Invalid donor phone number visibility.'));
283
                }
284
                break;
285
286
            /* Reading Options */
287
288
            case 'site_publication':
289
                if (empty($value) || ! in_array($value, ['on', 'off'])) {
290
                    throw new InvalidArgumentException(__('Invalid site publication status.'));
291
                }
292
                break;
293
            case 'entities_per_page':
294
                if (empty($value) || ! isValidInteger($value) || $value < 1) {
295
                    throw new InvalidArgumentException(__('Invalid entities per page count.'));
296
                }
297
                break;
298
        }
299
        return true;
300
    }
301
}
302