Completed
Push — master ( 5af970...ffb252 )
by CodexShaper
04:49
created

Template::saveTemplate()   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 1
Metric Value
cc 4
eloc 25
c 1
b 0
f 1
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\Traits;
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
trait Template
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
    public function saveTemplate(Request $request)
35
    {
36
        $field = $request->template;
37
        try
38
        {
39
            if (DBM::Template()->where('name', $field['name'])->first()) {
40
                return response()->json([
41
                    'success' => false,
42
                    'errors'  => [" The template name must be unique. " . $field['name'] . " already exist."],
43
                ], 400);
44
            }
45
46
            $template                 = DBM::Template();
47
            $template->name           = $field['name'];
48
            $template->old_name       = $field['name'];
49
            $template->type           = $field['type']['name'];
50
            $template->length         = $field['length'];
51
            $template->index          = $field['index'];
52
            $template->default        = $field['default'];
53
            $template->notnull        = $field['notnull'];
54
            $template->unsigned       = $field['unsigned'];
55
            $template->auto_increment = $field['autoincrement'];
56
57
            if ($template->save()) {
58
                return response()->json(['success' => true, 'templates' => DBM::templates()]);
59
            }
60
61
        } catch (\Exception $e) {
62
            return response()->json([
63
                'success' => false,
64
                'errors'  => [$e->getMessage()],
65
            ], 400);
66
        }
67
        return response()->json(['success' => true, 'template' => $request->all()]);
68
    }
69
70
    public function removeTemplate(Request $request)
71
    {
72
        if ($template = DBM::Template()->where('name', $request->name)->first()) {
73
            if ($template->delete()) {
74
                return response()->json(['success' => true, 'templates' => DBM::templates()]);
75
            }
76
        }
77
        return response()->json([
78
            'success' => false,
79
            'errors'  => ['The template '+$request->name . " not found"],
80
        ], 400);
81
    }
82
}
83