Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 125
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Installer
0.00% covered (danger)
0.00%
0 / 125
0.00% covered (danger)
0.00%
0 / 7
1892
0.00% covered (danger)
0.00%
0 / 1
 install
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 installDefaultSettings
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
 installCountries
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
 installLanguages
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
90
 installCurrencies
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
90
 installExternal
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
110
 createSettings
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   Modules\Admin\Admin
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace Modules\Admin\Admin;
16
17use Model\Setting;
18use Model\SettingMapper;
19use Modules\Admin\Models\LocalizationMapper;
20use Modules\Admin\Models\SettingsEnum;
21use phpOMS\Application\ApplicationAbstract;
22use phpOMS\Config\SettingsInterface;
23use phpOMS\DataStorage\Database\Connection\SQLiteConnection;
24use phpOMS\DataStorage\Database\DatabasePool;
25use phpOMS\DataStorage\Database\Query\Builder;
26use phpOMS\Localization\Localization;
27use phpOMS\Message\Http\HttpRequest;
28use phpOMS\Message\Http\HttpResponse;
29use phpOMS\Module\InstallerAbstract;
30use phpOMS\Module\ModuleInfo;
31use phpOMS\System\File\PathException;
32use phpOMS\System\OperatingSystem;
33use phpOMS\System\SystemType;
34use phpOMS\Uri\HttpUri;
35
36/**
37 * Installer class.
38 *
39 * @package Modules\Admin\Admin
40 * @license OMS License 2.0
41 * @link    https://jingga.app
42 * @since   1.0.0
43 */
44final class Installer extends InstallerAbstract
45{
46    /**
47     * Path of the file
48     *
49     * @var string
50     * @since 1.0.0
51     */
52    public const PATH = __DIR__;
53
54    /**
55     * {@inheritdoc}
56     */
57    public static function install(ApplicationAbstract $app, ModuleInfo $info, SettingsInterface $cfgHandler) : void
58    {
59        parent::install($app, $info, $cfgHandler);
60
61        $sqlite = new SQLiteConnection([
62            'db'       => 'sqlite',
63            'database' => __DIR__ . '/../../../phpOMS/Localization/Defaults/localization.sqlite',
64        ]);
65
66        $sqlite->connect();
67
68        self::installCountries($sqlite, $app->dbPool);
69        self::installLanguages($sqlite, $app->dbPool);
70        self::installCurrencies($sqlite, $app->dbPool);
71        self::installDefaultSettings();
72
73        $sqlite->close();
74
75        $settings = include __DIR__ . '/Install/settings.php';
76        foreach ($settings as $setting) {
77            self::createSettings($app, $setting);
78        }
79    }
80
81    /**
82     * Install settings
83     *
84     * @return void
85     *
86     * @since 1.0.0
87     **/
88    private static function installDefaultSettings() : void
89    {
90        $cmdResult = \shell_exec(
91            (OperatingSystem::getSystem() === SystemType::WIN
92                ? 'php.exe'
93                : 'php'
94            ) . ' ' . __DIR__ . '/../../../../cli.php -v'
95        );
96        $cmdResult = $cmdResult === null || $cmdResult === false ? '' : $cmdResult;
97
98        SettingMapper::create()->execute(
99            new Setting(
100                0,
101                SettingsEnum::CLI_ACTIVE,
102                (string) (\stripos($cmdResult, 'Version:') !== false)
103            )
104        );
105
106        $l11n = Localization::fromLanguage('en');
107        LocalizationMapper::create()->execute($l11n);
108    }
109
110    /**
111     * Install countries
112     *
113     * @param SQLiteConnection $sqlite SQLLite database connection of the source data
114     * @param DatabasePool     $dbPool Database pool to save data to
115     *
116     * @return void
117     *
118     * @since 1.0.0
119     */
120    private static function installCountries(SQLiteConnection $sqlite, DatabasePool $dbPool) : void
121    {
122        $con = $dbPool->get();
123
124        $query = new Builder($con);
125        $query->insert('country_name', 'country_code2', 'country_code3', 'country_numeric', 'country_developed')
126            ->into('country');
127
128        $querySqlite = new Builder($sqlite);
129        $countries   = $querySqlite->select('*')->from('country')->execute();
130
131        if ($countries === null) {
132            return;
133        }
134
135        foreach ($countries as $country) {
136            $query->values(
137                $country['country_name'] === null ? null : \trim((string) $country['country_name']),
138                $country['country_code2'] === null ? null : \trim((string) $country['country_code2']),
139                $country['country_code3'] === null ? null : \trim((string) $country['country_code3']),
140                $country['country_numeric'],
141                (int) $country['country_developed']
142            );
143        }
144
145        $query->execute();
146    }
147
148    /**
149     * Install languages
150     *
151     * @param SQLiteConnection $sqlite SQLLite database connection of the source data
152     * @param DatabasePool     $dbPool Database pool to save data to
153     *
154     * @return void
155     *
156     * @since 1.0.0
157     */
158    private static function installLanguages(SQLiteConnection $sqlite, DatabasePool $dbPool) : void
159    {
160        $con = $dbPool->get();
161
162        $query = new Builder($con);
163        $query->insert('language_name', 'language_native', 'language_639_1', 'language_639_2T', 'language_639_2B', 'language_639_3')
164            ->into('language');
165
166        $querySqlite = new Builder($sqlite);
167        $languages   = $querySqlite->select('*')->from('language')->execute();
168
169        if ($languages === null) {
170            return;
171        }
172
173        foreach ($languages as $language) {
174            $query->values(
175                $language['language_name'] === null ? null : \trim((string) $language['language_name']),
176                $language['language_native'] === null ? null : \trim((string) $language['language_native']),
177                $language['language_639_1'] === null ? null : \trim((string) $language['language_639_1']),
178                $language['language_639_2T'] === null ? null : \trim((string) $language['language_639_2T']),
179                $language['language_639_2B'] === null ? null : \trim((string) $language['language_639_2B']),
180                $language['language_639_3'] === null ? null : \trim((string) $language['language_639_3'])
181            );
182        }
183
184        $query->execute();
185    }
186
187    /**
188     * Install currencies
189     *
190     * @param SQLiteConnection $sqlite SQLLite database connection of the source data
191     * @param DatabasePool     $dbPool Database pool to save data to
192     *
193     * @return void
194     *
195     * @since 1.0.0
196     */
197    private static function installCurrencies(SQLiteConnection $sqlite, DatabasePool $dbPool) : void
198    {
199        $con = $dbPool->get();
200
201        $query = new Builder($con);
202        $query->insert('currency_id', 'currency_name', 'currency_code', 'currency_number', 'currency_symbol', 'currency_subunits', 'currency_decimal', 'currency_countries')
203            ->into('currency');
204
205        $querySqlite = new Builder($sqlite);
206        $currencies  = $querySqlite->select('*')->from('currency')->execute();
207
208        if ($currencies === null) {
209            return;
210        }
211
212        foreach ($currencies as $currency) {
213            $query->values(
214                $currency['currency_id'],
215                $currency['currency_name'] === null ? null : \trim((string) $currency['currency_name']),
216                $currency['currency_code'] === null ? null : \trim((string) $currency['currency_code']),
217                $currency['currency_number'] === null ? null : \trim((string) $currency['currency_number']),
218                $currency['currency_symbol'] === null ? null : \trim((string) $currency['currency_symbol']),
219                $currency['currency_subunits'],
220                $currency['currency_decimal'] === null ? null : \trim((string) $currency['currency_decimal']),
221                $currency['currency_countries'] === null ? null : \trim((string) $currency['currency_countries'])
222            );
223        }
224
225        $query->execute();
226    }
227
228    /**
229     * Install data from providing modules.
230     *
231     * @param ApplicationAbstract $app  Application
232     * @param array               $data Additional data
233     *
234     * @return array
235     *
236     * @throws PathException
237     * @throws \Exception
238     *
239     * @since 1.0.0
240     */
241    public static function installExternal(ApplicationAbstract $app, array $data) : array
242    {
243        if (!\is_file($data['path'] ?? '') && !isset($data['data'])) {
244            throw new PathException($data['path'] ?? '');
245        }
246
247        $adminData = [];
248
249        if (isset($data['path'])) {
250            $adminFile = \file_get_contents($data['path'] ?? '');
251            if ($adminFile === false) {
252                throw new PathException($data['path'] ?? ''); // @codeCoverageIgnore
253            }
254
255            $adminData = \json_decode($adminFile, true) ?? [];
256        } elseif (isset($data['data'])) {
257            $adminData = $data['data'];
258        }
259
260        if (!\is_array($adminData)) {
261            throw new \Exception(); // @codeCoverageIgnore
262        }
263
264        $result = [
265            'settings' => [],
266        ];
267
268        foreach ($adminData as $admin) {
269            switch ($admin['type']) {
270                case 'setting':
271                    $result['settings'][] = self::createSettings($app, $admin);
272                    break;
273                default:
274            }
275        }
276
277        return $result;
278    }
279
280    /**
281     * Create settings.
282     *
283     * @param ApplicationAbstract $app  Database instance
284     * @param array               $data Setting data
285     *
286     * @return array
287     *
288     * @since 1.0.0
289     */
290    private static function createSettings(ApplicationAbstract $app, array $data) : array
291    {
292        /** @var \Modules\Admin\Controller\ApiController $module */
293        $module = $app->moduleManager->get('Admin');
294
295        $response = new HttpResponse();
296        $request  = new HttpRequest(new HttpUri(''));
297
298        $request->header->account = 1;
299        $request->setData('id', $data['id'] ?? 0);
300        $request->setData('name', $data['name'] ?? '');
301        $request->setData('content', $data['content'] ?? '');
302        $request->setData('pattern', $data['pattern'] ?? '');
303        $request->setData('unit', $data['unit'] ?? null);
304        $request->setData('app', $data['app'] ?? null);
305        $request->setData('module', $data['module'] ?? null);
306        $request->setData('group', $data['group'] ?? null);
307        $request->setData('account', $data['account'] ?? null);
308        $request->setData('encrypted', $data['encrypted'] ?? false);
309
310        $module->apiSettingsCreate($request, $response);
311
312        $responseData = $response->get('');
313        if (!\is_array($responseData)) {
314            return [];
315        }
316
317        return \is_array($responseData['response'])
318            ? $responseData['response']
319            : $responseData['response']->toArray();
320    }
321}