Passed
Push — master ( 284e72...816a06 )
by Iman
04:12
created

Step4Handler::handleFormSubmit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
nc 1
nop 0
dl 0
loc 15
c 0
b 0
f 0
cc 1
rs 9.4285
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator;
4
5
class Step4Handler
6
{
7
    public function showForm($id)
8
    {
9
        $controller = ModulesRepo::getControllerName($id);;
10
11
        $data = [];
12
        $data['id'] = $id;
13
        if (file_exists(controller_path($controller))) {
14
            $fileContent = FileManipulator::readCtrlContent($controller);
15
            $data['config'] = ControllerConfigParser::parse($fileContent);
16
        }
17
18
        return view('CbModulesGen::step4', $data);
19
    }
20
21
    public function handleFormSubmit()
22
    {
23
        $id = request('id');
24
        $module = ModulesRepo::find($id);
25
26
        $data = request()->all();
27
28
        $data['table'] = $module->table_name;
29
30
        $newCode = $this->getScriptConfig($data);
31
        $this->replaceInFile($module->controller, 'CONFIGURATION', $newCode);
32
33
        return redirect()->route('AdminModulesControllerGetIndex')->with([
34
            'message' => cbTrans('alert_update_data_success'),
35
            'message_type' => 'success',
36
        ]);
37
    }
38
39
    /**
40
     * @param $data
41
     * @return array
42
     */
43
    private function getScriptConfig($data)
44
    {
45
        $scriptConfig = [];
46
        $i = 0;
47
        $data = array_diff_key($data, array_flip(['_token', 'id', 'submit'])); // remove keys
48
        foreach ($data as $key => $val) {
49
            if ($val == 'true' || $val == 'false') {
50
                $value = $val;
51
            } else {
52
                $value = "'$val'";
53
            }
54
55
            $scriptConfig[$i] = str_repeat(' ', 12).'$this->'.$key.' = '.$value.';';
56
            $i++;
57
        }
58
59
        return implode("\n", $scriptConfig);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(' ', $scriptConfig) returns the type string which is incompatible with the documented return type array.
Loading history...
60
    }
61
62
    /**
63
     * @param $phpCode
64
     * @param $mark
65
     * @param $newCode
66
     * @return string
67
     */
68
/*    private function replaceConfigSection($phpCode, $mark, $newCode)
69
    {
70
        list($before, $_middle, $after) = FileManipulator::extractBetween($phpCode, $mark);
71
72
        $_code = $before."\n\n";
73
        $_code .= "            # START $mark DO NOT REMOVE THIS LINE\n";
74
        $_code .= $newCode."\n";
75
        $_code .= "            # END $mark DO NOT REMOVE THIS LINE\n\n";
76
        $_code .= '            '.$after;
77
78
        return $_code;
79
    }*/
80
81
    /**
82
     * @param $controller
83
     * @param $mark
84
     * @param $newCode
85
     */
86
    private function replaceInFile($controller, $mark, $newCode)
87
    {
88
        $rawCode = FileManipulator::readCtrlContent($controller);
89
        $fileController = FileManipulator::replaceBetweenMark($rawCode, $mark, $newCode);
90
        FileManipulator::putCtrlContent($controller, $fileController);
91
    }
92
}