saveSampleData()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 19
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
*/
12
13
/**
14
 * Module: cardealer
15
 *
16
 * @category        Module
17
 * @package         cardealer
18
 * @author          XOOPS Development Team <[email protected]> - <https://xoops.org>
19
 * @copyright       {@link https://xoops.org/ XOOPS Project}
20
 * @license         GPL 2.0 or later
21
 * @link            https://xoops.org/
22
 * @since           1.0.0
23
 */
24
25
use XoopsModules\Cardealer;
26
use XoopsModules\Cardealer\Common;
27
28
require_once dirname(dirname(dirname(__DIR__))) . '/mainfile.php';
29
30
require_once dirname(__DIR__) . '/preloads/autoloader.php';
31
32
$op = \Xmf\Request::getCmd('op', '');
33
34
switch ($op) {
35
    case 'load':
36
        loadSampleData();
37
        break;
38
    case 'save':
39
        saveSampleData();
40
        break;
41
}
42
43
// XMF TableLoad for SAMPLE data
44
45
function loadSampleData()
46
{
47
    $moduleDirName = basename(dirname(__DIR__));
48
    $helper        = Cardealer\Helper::getInstance();
49
    $utility       = new Cardealer\Utility();
50
    $configurator  = new Common\Configurator();
51
52
    // Load language files
53
    $helper->loadLanguage('admin');
54
    $helper->loadLanguage('modinfo');
55
    $helper->loadLanguage('common');
56
57
    $tables = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
58
59
    foreach ($tables as $table) {
60
        $tabledata = \Xmf\Yaml::readWrapped($table . '.yml');
61
        \Xmf\Database\TableLoad::truncateTable($table);
62
        \Xmf\Database\TableLoad::loadTableFromArray($table, $tabledata);
0 ignored issues
show
Bug introduced by
It seems like $tabledata can also be of type boolean; however, parameter $data of Xmf\Database\TableLoad::loadTableFromArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        \Xmf\Database\TableLoad::loadTableFromArray($table, /** @scrutinizer ignore-type */ $tabledata);
Loading history...
63
    }
64
65
    //  ---  COPY test folder files ---------------
66
    if (count($configurator->copyTestFolders) > 0) {
67
        foreach (array_keys($configurator->copyTestFolders) as $i) {
68
            $src  = $configurator->copyTestFolders[$i][0];
69
            $dest = $configurator->copyTestFolders[$i][1];
70
            $utility::xcopy($src, $dest);
71
        }
72
    }
73
    redirect_header('../admin/index.php', 1, AM_CARDEALER_SAMPLEDATA_SUCCESS);
74
}
75
76
function saveSampleData()
77
{
78
    $moduleDirName      = basename(dirname(__DIR__));
79
    $moduleDirNameUpper = strtoupper($moduleDirName);
80
81
    $tables = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getInfo('tables');
82
83
    $exportFolder = __DIR__ . '/Exports-' . date("Y-m-d-H-i-s") . '/';
84
    \XoopsModules\Cardealer\Utility::createFolder($exportFolder);
85
86
    foreach ($tables as $table) {
87
        \Xmf\Database\TableLoad::saveTableToYamlFile($table, $exportFolder . $table . '.yml');
88
    }
89
90
    foreach ($tables as $table) {
91
        \Xmf\Database\TableLoad::saveTableToYamlFile($moduleDirName . '_' . $table, $table . '.yml');
92
    }
93
94
    redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'SAMPLEDATA_SUCCESS'));
95
}
96
97
function exportSchema()
98
{
99
    $moduleDirName      = basename(dirname(__DIR__));
100
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
101
102
    try {
103
        // TODO set exportSchema
104
        //        $migrate = new Cardealer\Migrate($moduleDirName);
105
        //        $migrate->saveCurrentSchema();
106
        //
107
        //        redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS'));
108
    } catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
109
        exit(constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_ERROR'));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
110
    }
111
112
}
113