Completed
Push — master ( 85914a...2ce878 )
by Harald
06:39 queued 03:01
created

AppAbstract::definitionsExist()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 12
nop 2
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright Copyright (c) 2015 osCommerce; http://www.oscommerce.com
6
  * @license GPL; http://www.oscommerce.com/gpllicense.txt
7
  */
8
9
namespace OSC\OM;
10
11
use OSC\OM\OSCOM;
12
use OSC\OM\Registry;
13
14
abstract class AppAbstract
15
{
16
    public $code;
17
    public $title;
18
    public $vendor;
19
    public $version;
20
    public $modules = [];
21
22
    public $db;
23
    public $lang;
24
25
    abstract protected function init();
26
27
    final public function __construct() {
28
        $this->setInfo();
29
30
        $this->db = Registry::get('Db');
31
        $this->lang = Registry::get('Language');
32
33
        $this->init();
34
    }
35
36 View Code Duplication
    final public function link()
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...
37
    {
38
        $args = func_get_args();
39
40
        $parameters = 'A&' . $this->vendor . '\\' . $this->code;
41
42
        if (isset($args[0])) {
43
            $args[0] = $parameters .= '&' . $args[0];
44
        } else {
45
            $args[0] = $parameters;
46
        }
47
48
        array_unshift($args, 'index.php');
49
50
        return forward_static_call_array([
51
            'OSC\OM\OSCOM',
52
            'link'
53
        ], $args);
54
    }
55
56 View Code Duplication
    final public function redirect()
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...
57
    {
58
        $args = func_get_args();
59
60
        $parameters = 'A&' . $this->vendor . '\\' . $this->code;
61
62
        if (isset($args[0])) {
63
            $args[0] = $parameters .= '&' . $args[0];
64
        } else {
65
            $args[0] = $parameters;
66
        }
67
68
        array_unshift($args, 'index.php');
69
70
        return forward_static_call_array([
71
            'OSC\OM\OSCOM',
72
            'redirect'
73
        ], $args);
74
    }
75
76
    final public function getCode()
77
    {
78
        return $this->code;
79
    }
80
81
    final public function getVendor()
82
    {
83
        return $this->vendor;
84
    }
85
86
    final public function getTitle()
87
    {
88
        return $this->title;
89
    }
90
91
    final public function getVersion()
92
    {
93
        return $this->version;
94
    }
95
96
    final public function getModules()
97
    {
98
        return $this->modules;
99
    }
100
101
    final public function hasModule($module, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
    }
104
105
    final private function setInfo()
106
    {
107
        $r = new \ReflectionClass($this);
108
109
        $this->code = $r->getShortName();
110
        $this->vendor = array_slice(explode('\\', $r->getNamespaceName()), -2, 1)[0];
111
112
        $metafile = OSCOM::BASE_DIR . 'Apps/' . $this->vendor . '/' . $this->code . '/oscommerce.json';
113
114
        if (!is_file($metafile) || (($json = json_decode(file_get_contents($metafile), true)) === null)) {
115
            trigger_error('OSC\OM\AppAbstract::setInfo(): ' . $this->vendor . '\\' . $this->code . ' - Could not read App information in ' . $metafile . '.');
116
117
            return false;
118
        }
119
120
        $this->title = $json['title'];
121
        $this->version = $json['version'];
122
        $this->modules = $json['modules'];
123
    }
124
125
    final public function getDef()
126
    {
127
        $args = func_get_args();
128
129
        if (!isset($args[0])) {
130
            $args[0] = null;
131
        }
132
133
        if (!isset($args[1])) {
134
            $args[1] = null;
135
        }
136
137
        if (!isset($args[2])) {
138
            $args[2] = $this->vendor . '-' . $this->code;
139
        }
140
141
        return call_user_func_array([$this->lang, 'getDef'], $args);
142
    }
143
144
    final public function definitionsExist($group, $language_code = null)
145
    {
146
        $language_code = isset($language_code) && $this->lang->exists($language_code) ? $language_code : $this->lang->get('code');
147
148
        $pathname = OSCOM::BASE_DIR . 'Apps/' . $this->vendor . '/' . $this->code . '/languages/' . $this->lang->get('directory', $language_code) . '/' . $group . '.txt';
149
150
        if (is_file($pathname)) {
151
            return true;
152
        }
153
154
        if ($language_code != 'en') {
155
            return call_user_func([$this, __FUNCTION__], $group, 'en');
156
        }
157
158
        return false;
159
    }
160
161
    final public function loadDefinitions($group, $language_code = null)
162
    {
163
        $language_code = isset($language_code) && $this->lang->exists($language_code) ? $language_code : $this->lang->get('code');
164
165
        if ($language_code != 'en') {
166
            $this->loadDefinitions($group, 'en');
167
        }
168
169
        $pathname = OSCOM::BASE_DIR . 'Apps/' . $this->vendor . '/' . $this->code . '/languages/' . $this->lang->get('directory', $language_code) . '/' . $group . '.txt';
170
171
        $group = 'Apps/' . $this->vendor . '/' . $this->code . '/' . $group;
172
173
        $defs = $this->lang->getDefinitions($group, $language_code, $pathname);
174
175
        $this->lang->injectDefinitions($defs, $this->vendor . '-' . $this->code);
176
    }
177
178
    final public function saveCfgParam($key, $value, $title = null, $description = null, $set_func = null)
179
    {
180
        if (is_null($value)) {
181
            $value = '';
182
        }
183
184
        if (!defined($key)) {
185
            if (!isset($title)) {
186
                $title = 'Parameter [' . $this->getTitle() . ']';
187
            }
188
189
            if (!isset($description)) {
190
                $description = 'Parameter [' . $this->getTitle() . ']';
191
            }
192
193
            $data = [
194
                'configuration_title' => $title,
195
                'configuration_key' => $key,
196
                'configuration_value' => $value,
197
                'configuration_description' => $description,
198
                'configuration_group_id' => '6',
199
                'sort_order' => '0',
200
                'date_added' => 'now()'
201
            ];
202
203
            if (isset($set_func)) {
204
                $data['set_function'] = $set_func;
205
            }
206
207
            $this->db->save('configuration', $data);
208
209
            define($key, $value);
210
        } else {
211
            $this->db->save('configuration', [
212
                'configuration_value' => $value
213
            ], [
214
                'configuration_key' => $key
215
            ]);
216
        }
217
    }
218
219
    final public function deleteCfgParam($key)
220
    {
221
        $this->db->delete('configuration', [
222
            'configuration_key' => $key
223
        ]);
224
    }
225
}
226