HelperConfig::getNavigation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
HCSF - A multilingual CMS and Shopsystem
5
Copyright (C) 2014  Marcus Haase - [email protected]
6
7
This program is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20
21
namespace HaaseIT\HCSF;
22
23
use Symfony\Component\Yaml\Yaml;
24
use Zend\ServiceManager\ServiceManager;
25
26
/**
27
 * Class HelperConfig
28
 * @package HaaseIT\HCSF
29
 */
30
class HelperConfig
31
{
32
    /**
33
     * @var array
34
     */
35
    protected $core = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $secrets = [];
41
42
    /**
43
     * @var array
44
     */
45
    protected $countries = [];
46
47
    /**
48
     * @var array
49
     */
50
    protected $shop = [];
51
52
    /**
53
     * @var array
54
     */
55
    protected $customer = [];
56
57
    /**
58
     * @var array
59
     */
60
    protected $navigation = [];
61
62
    /**
63
     * @var string
64
     */
65
    protected $lang;
66
67
    /**
68
     * @var array
69
     */
70
    protected $customization = [];
71
72
    /**
73
     *
74
     */
75
    public function __construct()
76
    {
77
        $this->loadCore();
78
        $this->loadCountries();
79
80
        $this->lang = $this->getLanguage();
81
82
        $this->loadSecrets();
83
84
        if ($this->core['enable_module_customer']) {
85
            $this->loadCustomer();
86
        }
87
88
        if ($this->core['enable_module_shop']) {
89
            $this->loadShop();
90
        }
91
92
        $this->loadCustomization();
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getLang()
99
    {
100
        return $this->lang;
101
    }
102
103
    /**
104
     *
105
     */
106
    protected function loadCore()
107
    {
108
        $core = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/core.yml'));
109
        if (is_file(PATH_BASEDIR.'config/core.yml')) {
110
            $core = array_merge($core, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/core.yml')));
111
        }
112
113
        $core['directory_images'] = trim($core['directory_images'], " \t\n\r\0\x0B/"); // trim this
114
115
        if (!empty($core['maintenancemode']) && $core['maintenancemode']) {
116
            $core['enable_module_customer'] = false;
117
            $core['enable_module_shop'] = false;
118
            $core['templatecache_enable'] = false;
119
            $core['debug'] = false;
120
            $core['textcatsverbose'] = false;
121
        } else {
122
            $core['maintenancemode'] = false;
123
        }
124
125
        if ($core['enable_module_shop']) {
126
            $core['enable_module_customer'] = true;
127
        }
128
129
        $this->core = $core;
130
    }
131
132
    /**
133
     * @param string|false $setting
134
     * @return mixed
135
     */
136 View Code Duplication
    public function getCore($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
139
            return $this->core;
140
        }
141
142
        return !empty($this->core[$setting]) ? $this->core[$setting] : false;
143
    }
144
145
    /**
146
     *
147
     */
148 View Code Duplication
    protected function loadCountries()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $countries = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/countries.yml'));
151
        if (is_file(PATH_BASEDIR.'config/countries.yml')) {
152
            $countries = array_merge($countries, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/countries.yml')));
153
        }
154
155
        $this->countries = $countries;
156
    }
157
158
    /**
159
     * @param string|false $setting
160
     * @return mixed
161
     */
162 View Code Duplication
    public function getCountries($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
165
            return $this->countries;
166
        }
167
168
        return !empty($this->countries[$setting]) ? $this->countries[$setting] : false;
169
    }
170
171
    /**
172
     *
173
     */
174 View Code Duplication
    protected function loadSecrets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        $secrets = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/secrets.yml'));
177
        if (is_file(PATH_BASEDIR.'config/secrets.yml')) {
178
            $secrets = array_merge($secrets, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/secrets.yml')));
179
        }
180
181
        $this->secrets = $secrets;
182
    }
183
184
    /**
185
     * @param string|false $setting
186
     * @return mixed
187
     */
188 View Code Duplication
    public function getSecret($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
191
            return $this->secrets;
192
        }
193
194
        return !empty($this->secrets[$setting]) ? $this->secrets[$setting] : false;
195
    }
196
197
    /**
198
     *
199
     */
200 View Code Duplication
    protected function loadCustomer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
    {
202
        $customer = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/customer.yml'));
203
        if (is_file(PATH_BASEDIR.'/config/customer.yml')) {
204
            $customer = array_merge($customer, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/customer.yml')));
205
        }
206
207
        $this->customer = $customer;
208
    }
209
210
    /**
211
     * @param string|bool $setting
212
     * @return mixed
213
     */
214 View Code Duplication
    public function getCustomer($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        if (!$setting) {
217
            return $this->customer;
218
        }
219
220
        return !empty($this->customer[$setting]) ? $this->customer[$setting] : false;
221
    }
222
223
    /**
224
     *
225
     */
226
    protected function loadShop()
227
    {
228
        $shop = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/shop.yml'));
229
        if (is_file(PATH_BASEDIR.'config/shop.yml')) {
230
            $shop = array_merge($shop, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/shop.yml')));
231
        }
232
        if (isset($shop['vat_disable']) && $shop['vat_disable']) {
233
            $shop['vat'] = ['full' => 0, 'reduced' => 0];
234
        }
235
236
        $this->shop = $shop;
237
    }
238
239
    /**
240
     * @param string|false $setting
241
     * @return mixed
242
     */
243 View Code Duplication
    public function getShop($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
    {
245
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
246
            return $this->shop;
247
        }
248
249
        return !empty($this->shop[$setting]) ? $this->shop[$setting] : false;
250
    }
251
252
    /**
253
     * @param ServiceManager $serviceManager
254
     */
255
    public function loadNavigation(ServiceManager $serviceManager)
256
    {
257
        if (is_file(PATH_BASEDIR.'config/navigation.yml')) {
258
            $navstruct = Yaml::parse(file_get_contents(PATH_BASEDIR.'config/navigation.yml'));
259
        } else {
260
            $navstruct = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/navigation.yml'));
261
        }
262
263
        if (!empty($navstruct) && $this->core['navigation_fetch_text_from_textcats']) {
264
            $textcats = $serviceManager->get('textcats');
265
            $TMP = [];
266
267
            foreach ($navstruct as $key => $item) {
0 ignored issues
show
Bug introduced by
The expression $navstruct of type object<Symfony\Component...|array|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
268
                foreach ($item as $subkey => $subitem) {
269
                    if (!empty($textcats->T($subkey))) {
270
                        $TMP[$key][$textcats->T($subkey)] = $subitem;
271
                    } else {
272
                        $TMP[$key][$subkey] = $subitem;
273
                    }
274
                }
275
            }
276
            $navstruct = $TMP;
277
            unset($TMP);
278
        }
279
280
        if (isset($navstruct['admin'])) {
281
            unset($navstruct['admin']);
282
        }
283
284
        $hardcodedtextcats = $serviceManager->get('hardcodedtextcats');
285
286
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_home')] = '/_admin/index.html';
287
288
        if ($this->core['enable_module_shop']) {
289
            $navstruct['admin'][$hardcodedtextcats->get('admin_nav_orders')] = '/_admin/shopadmin.html';
290
            $navstruct['admin'][$hardcodedtextcats->get('admin_nav_items')] = '/_admin/itemadmin.html';
291
            $navstruct['admin'][$hardcodedtextcats->get('admin_nav_itemgroups')] = '/_admin/itemgroupadmin.html';
292
        }
293
294
        if ($this->core['enable_module_customer']) {
295
            $navstruct['admin'][$hardcodedtextcats->get('admin_nav_customers')] = '/_admin/customeradmin.html';
296
        }
297
298
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_pages')] = '/_admin/pageadmin.html';
299
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_textcats')] = '/_admin/textcatadmin.html';
300
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_cleartemplatecache')] = '/_admin/cleartemplatecache.html';
301
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_clearimagecache')] = '/_admin/clearimagecache.html';
302
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_phpinfo')] = '/_admin/phpinfo.html';
303
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_dbstatus')] = '/_admin/dbstatus.html';
304
305
        $this->navigation = $navstruct;
306
    }
307
308
    /**
309
     * @param string|false $setting
310
     * @return mixed
311
     */
312 View Code Duplication
    public function getNavigation($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
313
    {
314
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
315
            return $this->navigation;
316
        }
317
318
        return !empty($this->navigation[$setting]) ? $this->navigation[$setting] : false;
319
    }
320
321
    /**
322
     * @return int|mixed|string
323
     */
324
    public function getLanguage()
325
    {
326
        $langavailable = $this->core['lang_available'];
327
        if (
328
            $this->core['lang_detection_method'] === 'domain'
329
            && isset($this->core['lang_by_domain'])
330
            && is_array($this->core['lang_by_domain'])
331
        ) { // domain based language detection
332
            $serverservername = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL);
333
            foreach ($this->core['lang_by_domain'] as $sKey => $sValue) {
334
                if ($serverservername == $sValue || $serverservername == 'www.'.$sValue) {
335
                    $sLang = $sKey;
336
                    break;
337
                }
338
            }
339
        } elseif ($this->core['lang_detection_method'] === 'legacy') { // legacy language detection
340
            $sLang = key($langavailable);
341
            $getlanguage = filter_input(INPUT_GET, 'language', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
342
            $cookielanguage = filter_input(INPUT_COOKIE, 'language', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
343
            $serverhttpaccepptlanguage = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
344
            if ($getlanguage !== null && array_key_exists($getlanguage, $langavailable)) {
345
                $sLang = strtolower($getlanguage);
346
                setcookie('language', strtolower($getlanguage), 0, '/');
347
            } elseif ($cookielanguage !== null && array_key_exists($cookielanguage, $langavailable)) {
348
                $sLang = strtolower($cookielanguage);
349
            } elseif ($serverhttpaccepptlanguage !== null && array_key_exists(substr($serverhttpaccepptlanguage, 0, 2), $langavailable)) {
350
                $sLang = substr($serverhttpaccepptlanguage, 0, 2);
351
            }
352
        }
353
        if (!isset($sLang)) {
354
            $sLang = key($langavailable);
355
        }
356
357
        return $sLang;
358
    }
359
360
    protected function loadCustomization()
361
    {
362
        $customization = [];
363
364
        if (is_file(PATH_BASEDIR.'config/customization.yml')) {
365
            $customization = array_merge($customization, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/customization.yml')));
366
        }
367
368
        $this->customization = $customization;
369
    }
370
371
    /**
372
     * @param bool $setting
373
     * @return array|bool|mixed
374
     */
375 View Code Duplication
    public function getCustomization($setting = false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
376
        if (!$setting) {
377
            return $this->customization;
378
        }
379
380
        return !empty($this->customization[$setting]) ? $this->customization[$setting] : false;
381
    }
382
}
383