Passed
Push — master ( 62919d...1f65fa )
by Iman
04:23
created

DownloadPostman   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processParams() 0 18 5
A httpBuilder() 0 6 3
A getDownloadPostman() 0 19 1
A cbInit() 0 5 1
A makeItems() 0 22 3
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->titleField = "name";
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('name', 'asc')->get() as $api) {
45
            list($httpbuilder, $formdata) = $this->processParams($api->parameters);
46
47
            $items[] = [
48
                'name' => $api->name,
49
                'request' => [
50
                    'url' => url('api/'.$api->permalink).$this->httpBuilder($api, $httpbuilder),
51
                    'method' => $api->method_type ?: 'GET',
52
                    'header' => [],
53
                    'body' => [
54
                        'mode' => 'formdata',
55
                        'formdata' => $formdata,
56
                    ],
57
                    'description' => $api->keterangan,
58
                ],
59
            ];
60
        }
61
62
        return $items;
63
    }
64
65
    /**
66
     * @param $api
67
     * @param $httpbuilder
68
     * @return string
69
     */
70
    private function httpBuilder($api, $httpbuilder)
71
    {
72
        if (strtolower($api->method_type) == 'get' && $httpbuilder) {
73
            return "?".http_build_query($httpbuilder);
74
        }
75
        return '';
76
    }
77
78
    /**
79
     * @param $parameters
80
     * @return array
81
     */
82
    private function processParams($parameters)
83
    {
84
        $parameters = unserialize($parameters);
85
        $formdata = [];
86
        $httpbuilder = [];
87
        if (! $parameters) {
88
            return [$httpbuilder, $formdata];
89
        }
90
        foreach ($parameters as $p) {
91
            $enabled = ($p['used'] == 0) ? false : true;
92
            $name = $p['name'];
93
            $httpbuilder[$name] = '';
94
            if ($enabled) {
95
                $formdata[] = ['key' => $name, 'value' => '', 'type' => 'text', 'enabled' => $enabled];
96
            }
97
        }
98
99
        return [$httpbuilder, $formdata];
100
    }
101
}
102