Completed
Push — master ( 412120...5af970 )
by CodexShaper
05:12
created

ObjectController::getObject()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 29
ccs 0
cts 23
cp 0
crap 20
rs 9.6666
1
<?php
2
3
namespace CodexShaper\DBM\Http\Controllers;
4
5
use CodexShaper\DBM\Database\Drivers\MongoDB\Type;
6
use CodexShaper\DBM\Database\Schema\Table;
7
use CodexShaper\DBM\Facades\Driver;
8
use DBM;
0 ignored issues
show
Bug introduced by
The type DBM was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Str;
11
12
class ObjectController extends Controller
13
{
14
15
    public function all(Request $request)
16
    {
17
        if ($request->ajax()) {
18
19
            if (($response = DBM::authorize('crud.browse')) !== true) {
20
                return $response;
21
            }
22
23
            $perPage = (int) $request->perPage;
24
            $query   = $request->q;
25
26
            $userPermissions = DBM::userPermissions();
27
            $tables          = Table::paginate($perPage, null, [], $query);
28
            $newTables       = $this->filterCrudTables($tables);
29
30
            return response()->json([
31
                'success'         => true,
32
                'tables'          => $newTables,
33
                'userPermissions' => $userPermissions,
34
                'pagination'      => $tables,
35
            ]);
36
        }
37
38
        return response()->json(['success' => false]);
39
    }
40
41
    public function filterCrudTables($tables)
42
    {
43
        $objects   = DBM::Object()->all();
44
        $newTables = [];
45
46
        foreach ($tables as $table) {
47
            foreach ($objects as $object) {
48
                if ($table == $object->name) {
49
                    $newTables[] = [
50
                        'name'   => $table,
51
                        'isCrud' => true,
52
                    ];
53
                    continue 2;
54
                }
55
            }
56
57
            $newTables[] = [
58
                'name'   => $table,
59
                'isCrud' => false,
60
            ];
61
        }
62
63
        return $newTables;
64
    }
65
66
    public function getObjectDetails(Request $request)
67
    {
68
        if ($request->ajax()) {
69
70
            try
71
            {
72
                if (!Table::exists($request->table)) {
73
                    throw new \Exception("Sorry! There is no table", 1);
74
                }
75
76
                $tableName           = $request->table;
77
                $relationship_tables = Table::all();
78
79
                $details    = $this->getObject($tableName);
80
                $permission = $details['isCrudExists'] ? 'update' : 'create';
81
82
                if (($response = DBM::authorize("crud.{$permission}")) !== true) {
83
                    return $response;
84
                }
85
86
                $relationshipDetails = (object) [
87
                    'type'                => 'hasOne',
88
                    'foreignTableDetails' => Table::getTable($relationship_tables[0]),
89
                    'localTableDetails'   => Table::getTable($tableName),
90
                ];
91
92
                return response()->json([
93
                    'success'              => true,
94
                    'relationship_tables'  => $relationship_tables,
95
                    'relationship_details' => $relationshipDetails,
96
                    'object'               => $details['object'],
97
                    'fields'               => $details['fields'],
98
                    'isCrudExists'         => $details['isCrudExists'],
99
                    'userPermissions'      => DBM::userPermissions(),
100
                    'driver'               => Driver::getConnectionName(),
101
                ]);
102
103
            } catch (\Exception $e) {
104
                return $this->generateError([$e->getMessage()]);
105
            }
106
        }
107
108
        return response()->json(['success' => false]);
109
    }
110
111
    public function getObject($tableName)
112
    {
113
        $isCrudExists = false;
114
        $fields       = [];
115
116
        if ($object = DBM::Object()->where('name', $tableName)->first()) {
117
            $isCrudExists = true;
118
            if (!$object->model) {
119
                $object->model = DBM::generateModelName($object->name);
120
            }
121
            $fields = $object->fields()->orderBy('order', 'ASC')->get();
122
        }
123
124
        if (!$object) {
125
            $table = Table::getTable($tableName);
126
127
            $object               = new \stdClass;
128
            $object->name         = $table['name'];
129
            $object->slug         = Str::slug($table['name']);
130
            $object->display_name = ucfirst($table['name']);
131
            $object->model        = DBM::generateModelName($table['name']);
132
            $object->controller   = '';
133
134
            $fields = $this->prepareFields($table);
135
        }
136
137
        $object->makeModel = false;
138
139
        return ['object' => $object, 'fields' => $fields, 'isCrudExists' => $isCrudExists];
140
141
    }
142
143
    public function prepareFields($table)
144
    {
145
        $fields = [];
146
        $order  = 1;
147
148
        foreach ($table['columns'] as $column) {
149
150
            $fields[] = (object) [
151
                'name'          => $column->name,
152
                'display_name'  => ucfirst($column->name),
153
                'type'          => DatabaseController::getInputType($column->type['name']),
154
                'create'        => ($column->autoincrement) ? false : true,
155
                'read'          => ($column->autoincrement) ? false : true,
156
                'edit'          => ($column->autoincrement) ? false : true,
157
                'delete'        => ($column->autoincrement) ? false : true,
158
                'function_name' => '',
159
                'order'         => $order,
160
                'settings'      => '{ }',
161
            ];
162
163
            $order++;
164
        }
165
166
        return $fields;
167
    }
168
169
    protected function generateError($errors)
170
    {
171
        return response()->json([
172
            'success' => false,
173
            'errors'  => $errors,
174
        ], 400);
175
    }
176
}
177