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

DownloadPostman   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
D makeItems() 0 40 9
A getDownloadPostman() 0 19 1
A cbInit() 0 5 1
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ApiGeneratorModule;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
7
class DownloadPostman extends CBController
8
{
9
    public function cbInit()
10
    {
11
        $this->table = 'cms_apicustom';
12
        $this->primaryKey = "id";
13
        $this->title_field = "nama";
14
    }
15
16
    public function getDownloadPostman()
17
    {
18
        $this->cbLoader();
19
        $data = [];
20
        $data['variables'] = [];
21
        $data['info'] = [
22
            'name' => cbGetsetting('appname').' - API',
23
            '_postman_id' => "1765dd11-73d1-2978-ae11-36921dc6263d",
24
            'description' => '',
25
            'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json',
26
        ];
27
28
        $data['item'] = $this->makeItems();
29
30
        $json = json_encode($data);
31
32
        return \Response::make($json, 200, [
33
            'Content-Type' => 'application/json',
34
            'Content-Disposition' => 'attachment; filename='.cbGetsetting('appname').' - API For POSTMAN.json',
35
        ]);
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    private function makeItems()
42
    {
43
        $items = [];
44
        foreach ($this->table()->orderby('nama', 'asc')->get() as $api) {
45
            $parameters = unserialize($api->parameters);
46
            $formdata = [];
47
            $httpbuilder = [];
48
            if ($parameters) {
49
                foreach ($parameters as $p) {
50
                    $enabled = ($p['used'] == 0) ? false : true;
51
                    $name = $p['name'];
52
                    $httpbuilder[$name] = '';
53
                    if ($enabled) {
54
                        $formdata[] = ['key' => $name, 'value' => '', 'type' => 'text', 'enabled' => $enabled];
55
                    }
56
                }
57
            }
58
59
            if (strtolower($api->method_type) == 'get' && $httpbuilder) {
60
                $httpbuilder = "?".http_build_query($httpbuilder);
61
            } else {
62
                $httpbuilder = '';
63
            }
64
65
            $items[] = [
66
                'name' => $api->nama,
67
                'request' => [
68
                    'url' => url('api/'.$api->permalink).$httpbuilder,
69
                    'method' => $api->method_type ?: 'GET',
70
                    'header' => [],
71
                    'body' => [
72
                        'mode' => 'formdata',
73
                        'formdata' => $formdata,
74
                    ],
75
                    'description' => $api->keterangan,
76
                ],
77
            ];
78
        }
79
80
        return $items;
81
    }
82
}
83