Passed
Push — master ( b8f118...fdb9c9 )
by CodexShaper
04:09
created

RelationController::delete()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 19
ccs 0
cts 14
cp 0
crap 20
rs 9.9666
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 RelationController extends Controller
13
{
14
    public function get(Request $request)
15
    {
16
        if ($request->ajax()) {
17
18
            if (($response = DBM::authorize('relationship.update')) !== true) {
19
                return $response;
20
            }
21
22
            $tableName = $request->table;
23
            $object    = DBM::Object()->where('name', $tableName)->first();
24
            $fields    = $object->fields;
25
            $field     = $this->prepareRelationshipField($fields, json_decode($request->field));
26
27
            return response()->json(['success' => true, 'field' => $field]);
28
        }
29
30
        return response()->json(['success' => false]);
31
    }
32
33
    public function prepareRelationshipField($fields, $field)
34
    {
35
        $prefix = (Driver::isMongoDB()) ? "_" : "";
36
37
        foreach ($fields as $fld) {
38
            if ($fld->id == $field->{$prefix . "id"}) {
39
40
                $relationship = $fld->settings;
41
                $localTable   = $relationship['localTable'];
42
                $foreignTable = $relationship['foreignTable'];
43
                $pivotTable   = $relationship['pivotTable'];
44
45
                $field->localFields   = Table::getTable($localTable);
46
                $field->foreignFields = Table::getTable($foreignTable);
47
                $field->pivotFields   = Table::getTable($pivotTable);
48
                $field->relationship  = $relationship;
49
            }
50
        }
51
52
        return $field;
53
    }
54
55
    public function add(Request $request)
56
    {
57
        if ($request->ajax()) {
58
59
            if (($response = DBM::authorize('relationship.create')) !== true) {
60
                return $response;
61
            }
62
63
            $relationship = $request->relationship;
64
65
            if (!class_exists($relationship['localModel'])) {
66
67
                $error = "{$relationship['localModel']} Model not found. Please create the {$relationship['localModel']} model first";
68
                return $this->generateError([$error]);
69
            }
70
71
            if (!class_exists($relationship['foreignModel'])) {
72
73
                $error = $relationship['foreignModel'] . " Model not found. Please create the " . $relationship['foreignModel'] . " model first";
74
                return $this->generateError([$error]);
75
            }
76
77
            $fieldName = $this->getFieldName($relationship);
78
            $settings  = $this->prepareSettings($relationship);
79
80
            $object = DBM::Object()->where('name', $relationship['localTable'])->first();
81
            $order  = DBM::Field()->where('dbm_object_id', $object->id)->max('order');
82
83
            $field                = DBM::Field();
84
            $field->dbm_object_id = $object->id;
85
            $field->name          = $fieldName;
86
            $field->type          = 'relationship';
87
            $field->display_name  = ucfirst($relationship['foreignTable']);
88
            $field->order         = $order + 1;
89
            $field->settings      = $settings;
90
            if ($field->save()) {
91
                return response()->json(['success' => true]);
92
            }
93
        }
94
95
        return response()->json(['success' => false]);
96
    }
97
98
    public function getFieldName($relationship)
99
    {
100
        $localTable   = Str::singular($relationship['localTable']);
101
        $foreignTable = Str::singular($relationship['foreignTable']);
102
        $relationType = $relationship['type'];
103
104
        return strtolower("{$localTable}_{$relationType}_{$foreignTable}_relationship");
105
    }
106
107
    public function prepareSettings($relationship)
108
    {
109
        return [
110
            'relationType'    => $relationship['type'],
111
            'localModel'      => $relationship['localModel'],
112
            'localTable'      => $relationship['localTable'],
113
            'localKey'        => $relationship['localKey'],
114
            'foreignModel'    => $relationship['foreignModel'],
115
            'foreignTable'    => $relationship['foreignTable'],
116
            'foreignKey'      => $relationship['foreignKey'],
117
            'displayLabel'    => $relationship['displayLabel'],
118
            'pivotTable'      => $relationship['pivotTable'],
119
            'parentPivotKey'  => $relationship['parentPivotKey'],
120
            'relatedPivotKey' => $relationship['relatedPivotKey'],
121
        ];
122
    }
123
124
    public function update(Request $request)
125
    {
126
        if ($request->ajax()) {
127
128
            if (($response = DBM::authorize('relationship.update')) !== true) {
129
                return $response;
130
            }
131
132
            $relationship = $request->relationship;
133
            $field        = $request->field;
134
135
            $settings = $this->prepareSettings($relationship);
136
137
            $field           = DBM::Field()::find($field['id']);
138
            $field->settings = $settings;
139
            if ($field->update()) {
140
                return response()->json(['success' => true]);
141
            }
142
        }
143
144
        return response()->json(['success' => false]);
145
    }
146
147
    public function delete(Request $request)
148
    {
149
        if ($request->ajax()) {
150
151
            if (($response = DBM::authorize('relationship.delete')) !== true) {
152
                return $response;
153
            }
154
155
            $tableName = $request->table;
0 ignored issues
show
Unused Code introduced by
The assignment to $tableName is dead and can be removed.
Loading history...
156
            $data      = json_decode($request->field);
157
158
            $field = DBM::Field()::find($data->id);
159
160
            if ($field->delete()) {
161
                return response()->json(['success' => true]);
162
            }
163
        }
164
165
        return response()->json(['success' => false]);
166
    }
167
168
    protected function generateError($errors)
169
    {
170
        return response()->json([
171
            'success' => false,
172
            'errors'  => $errors,
173
        ], 400);
174
    }
175
}
176