Completed
Push — master ( ffb252...e3f027 )
by CodexShaper
04:15
created

TemplateController::save()   A

Complexity

Conditions 4
Paths 17

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 25
c 1
b 0
f 0
nc 17
nop 1
dl 0
loc 34
ccs 0
cts 31
cp 0
crap 20
rs 9.52
1
<?php
2
3
namespace CodexShaper\DBM\Http\Controllers;
4
5
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...
6
use Illuminate\Http\Request;
7
8
class TemplateController extends Controller
9
{
10
11
    public function updateTemplates($templates)
12
    {
13
        if (is_array($templates) && count($templates) > 0) {
14
15
            foreach ($templates as $field) {
16
17
                if ($template = DBM::Template()->where('old_name', $field['oldName'])->first()) {
18
19
                    $template->name           = $field['name'];
20
                    $template->old_name       = $field['name'];
21
                    $template->type           = $field['type']['name'];
22
                    $template->length         = $field['length'];
23
                    $template->index          = $field['index'];
24
                    $template->default        = $field['default'];
25
                    $template->notnull        = $field['notnull'];
26
                    $template->unsigned       = $field['unsigned'];
27
                    $template->auto_increment = $field['autoincrement'];
28
29
                    $template->update();
30
                }
31
            }
32
        }
33
    }
34
35
    public function save(Request $request)
36
    {
37
        $field = $request->template;
38
        try
39
        {
40
            if (DBM::Template()->where('name', $field['name'])->first()) {
41
                return response()->json([
42
                    'success' => false,
43
                    'errors'  => [" The template name must be unique. " . $field['name'] . " already exist."],
44
                ], 400);
45
            }
46
47
            $template                 = DBM::Template();
48
            $template->name           = $field['name'];
49
            $template->old_name       = $field['name'];
50
            $template->type           = $field['type']['name'];
51
            $template->length         = $field['length'];
52
            $template->index          = $field['index'];
53
            $template->default        = $field['default'];
54
            $template->notnull        = $field['notnull'];
55
            $template->unsigned       = $field['unsigned'];
56
            $template->auto_increment = $field['autoincrement'];
57
58
            if ($template->save()) {
59
                return response()->json(['success' => true, 'templates' => DBM::templates()]);
60
            }
61
62
        } catch (\Exception $e) {
63
            return response()->json([
64
                'success' => false,
65
                'errors'  => [$e->getMessage()],
66
            ], 400);
67
        }
68
        return response()->json(['success' => true, 'template' => $request->all()]);
69
    }
70
71
    public function remove(Request $request)
72
    {
73
        if ($template = DBM::Template()->where('name', $request->name)->first()) {
74
            if ($template->delete()) {
75
                return response()->json(['success' => true, 'templates' => DBM::templates()]);
76
            }
77
        }
78
        return response()->json([
79
            'success' => false,
80
            'errors'  => ['The template '+$request->name . " not found"],
81
        ], 400);
82
    }
83
}
84