Passed
Push — master ( 832faf...d5b1c7 )
by Iman
03:52
created

AdminApiGeneratorController::getDownloadPostman()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 54
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
nc 5
nop 0
dl 0
loc 54
c 0
b 0
f 0
cc 9
rs 7.255

2 Methods

Rating   Name   Duplication   Size   Complexity  
B AdminApiGeneratorController::postSaveApiCustom() 0 27 1
A AdminApiGeneratorController::getEditApi() 0 14 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ApiGeneratorModule;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
use Illuminate\Support\Facades\Request;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Route;
9
use crocodicstudio\crudbooster\helpers\CRUDBooster;
10
11
class AdminApiGeneratorController extends CBController
12
{
13
    public function cbInit()
14
    {
15
        $this->table = 'cms_apicustom';
16
        $this->primaryKey = "id";
17
        $this->title_field = "nama";
18
        $this->buttonShow = false;
19
        $this->button_new = false;
0 ignored issues
show
Bug Best Practice introduced by
The property button_new does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        $this->deleteBtn = false;
21
        $this->buttonAdd = false;
22
        $this->button_import = false;
23
        $this->buttonExport = false;
24
    }
25
26
    public function getIndex()
27
    {
28
        $this->cbLoader();
29
30
        $data = [];
31
32
        $data['page_title'] = 'API Generator';
33
        $data['apis'] = $this->table()->orderby('nama', 'asc')->get();
34
35
        return view('CbApiGen::api_documentation', $data);
36
    }
37
38
    public function apiDocumentation()
39
    {
40
        $this->cbLoader();
41
        $data = [];
42
43
        $data['apis'] = $this->table()->orderby('nama', 'asc')->get();
44
45
        return view('CbApiGen::api_documentation_public', $data);
46
    }
47
48
    public function getGenerator()
49
    {
50
        $this->cbLoader();
51
52
        $data['page_title'] = 'API Generator';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
53
        $data['tables'] = CRUDBooster::listCbTables();
54
55
        return view('CbApiGen::api_generator', $data);
56
    }
57
58
    public function getEditApi($id)
59
    {
60
        $this->cbLoader();
61
62
        $row = $this->findRow($id)->first();
63
64
        $data['row'] = $row;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
65
        $data['parameters'] = json_encode(unserialize($row->parameters));
66
        $data['responses'] = json_encode(unserialize($row->responses));
67
        $data['page_title'] = 'API Generator';
68
69
        $data['tables'] = CRUDBooster::listCbTables();
70
71
        return view('CbApiGen::api_generator', $data);
72
    }
73
74
    public function postSaveApiCustom()
75
    {
76
        $this->cbLoader();
77
        $posts = request()->all();
78
79
        $_data = [];
80
81
        $_data['nama'] = g('nama');
82
        $_data['tabel'] = $posts['tabel'];
83
        $_data['aksi'] = $posts['aksi'];
84
        $_data['permalink'] = g('permalink');
85
        $_data['method_type'] = g('method_type');
86
87
        $json = $this->json();
88
89
        $_data['parameters'] = serialize(array_filter($json));
90
91
        $_data['sql_where'] = g('sql_where');
92
93
        $json = $this->json2();
94
        $json = array_filter($json);
95
        $_data['responses'] = serialize($json);
96
        $_data['keterangan'] = g('keterangan');
97
98
        $this->saveToDB($_data);
99
100
        return redirect(CRUDBooster::mainpath())->with(['message' => 'Yeay, your api has been saved successfully !', 'message_type' => 'success']);
101
    }
102
103
    public function getDeleteApi($id)
104
    {
105
        $this->cbLoader();
106
        $row = $this->findRow($id)->first();
107
        $this->findRow($id)->delete();
108
109
        $controllername = ucwords(str_replace('_', ' ', $row->permalink));
110
        $controllername = str_replace(' ', '', $controllername);
111
        @unlink((controllers_dir()."Api".$controllername."Controller.php"));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

111
        /** @scrutinizer ignore-unhandled */ @unlink((controllers_dir()."Api".$controllername."Controller.php"));

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
112
113
        return response()->json(['status' => 1]);
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

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

113
        return response()->/** @scrutinizer ignore-call */ json(['status' => 1]);
Loading history...
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    private function json()
120
    {
121
        $params_name = g('params_name');
122
        $params_type = g('params_type');
123
        $params_config = g('params_config');
124
        $params_required = g('params_required');
125
        $params_used = g('params_used');
126
127
        $json = [];
128
        for ($i = 0, $_count = count($params_name); $i <= $_count; $i++) {
129
            if (! $params_name[$i]) {
130
                continue;
131
            }
132
            $json[] = [
133
                'name' => $params_name[$i],
134
                'type' => $params_type[$i],
135
                'config' => $params_config[$i],
136
                'required' => $params_required[$i],
137
                'used' => $params_used[$i],
138
            ];
139
        }
140
141
        return $json;
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    private function json2()
148
    {
149
        $responses_name = g('responses_name');
150
        $responses_type = g('responses_type');
151
        $responses_subquery = g('responses_subquery');
152
        $responses_used = g('responses_used');
153
154
        $json = [];
155
        for ($i = 0, $_count = count($responses_name); $i <= $_count; $i++) {
156
            if (! $responses_name[$i]) {
157
                continue;
158
            }
159
            $json[] = [
160
                'name' => $responses_name[$i],
161
                'type' => $responses_type[$i],
162
                'subquery' => $responses_subquery[$i],
163
                'used' => $responses_used[$i],
164
            ];
165
        }
166
167
        return $json;
168
    }
169
170
    /**
171
     * @param $a
172
     */
173
    private function saveToDB($a)
174
    {
175
        if (request('id')) {
176
            return $this->findRow(g('id'))->update($a);
177
        }
178
179
        $controllerName = ucwords(str_replace('_', ' ', $a['permalink']));
180
        $controllerName = str_replace(' ', '', $controllerName);
181
        $this->generateAPI($controllerName, $a['tabel'], $a['permalink'], $a['method_type']);
182
183
        return $this->table()->insert($a);
184
    }
185
186
    private function generateAPI($controller_name, $table_name, $permalink, $method_type = 'post')
187
    {
188
        $php = view('CbApiGen::api_stub', compact('controller_name', 'table_name', 'permalink', 'method_type'))->render();
189
        file_put_contents(controllers_dir().'Api'.$controller_name.'Controller.php', $php);
190
    }
191
}
192