Passed
Push — master ( d63a45...8016ac )
by Iman
09:10
created

FormSubmitHandlers   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setTimeStamps() 0 4 2
A postEditSave() 0 18 1
A postAddSave() 0 16 1
C inputAssignment() 0 31 12
A sendResponseForSave() 0 13 5
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Controllers\CBController;
4
5
use Crocodicstudio\Crudbooster\CBCoreModule\RelationHandler;
6
use Crocodicstudio\Crudbooster\Controllers\FormValidator;
7
use Crocodicstudio\Crudbooster\Helpers\CRUDBooster;
8
use Crocodicstudio\Crudbooster\Helpers\DbInspector;
9
use Illuminate\Support\Facades\Request;
10
use Illuminate\Support\Facades\Schema;
11
12
trait FormSubmitHandlers
13
{
14
    public $arr = [];
15
16
    public $return_url = null;
17
18
    public function inputAssignment($id = null)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
    public function inputAssignment(/** @scrutinizer ignore-unused */ $id = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20
        $hide_form = (request('hide_form')) ? unserialize(request('hide_form')) : [];
0 ignored issues
show
Bug introduced by
It seems like request('hide_form') can also be of type array; however, parameter $str of unserialize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
        $hide_form = (request('hide_form')) ? unserialize(/** @scrutinizer ignore-type */ request('hide_form')) : [];
Loading history...
21
22
        foreach ($this->form as $form) {
23
            $name = $form['name'];
24
            $type = $form['type'] ?: 'text';
25
            $inputdata = request($name);
26
27
            if (! $name || in_array($name, $hide_form) || $form['exception']) {
28
                continue;
29
            }
30
31
            $hookPath = CbComponentsPath($type).DIRECTORY_SEPARATOR.'hookInputAssignment.php';
32
            if (file_exists($hookPath)) {
33
                require_once($hookPath);
34
            }
35
            unset($hookPath);
36
37
            if (Request::hasFile($name)) {
38
                continue;
39
            }
40
41
            if ($inputdata == '' && DbInspector::isNullableColumn($this->table, $name)) {
42
                continue;
43
            }
44
45
            $this->arr[$name] = '';
46
47
            if ($inputdata != '') {
48
                $this->arr[$name] = $inputdata;
49
            }
50
        }
51
    }
52
53
    public function postAddSave()
54
    {
55
        $this->genericLoader();
0 ignored issues
show
Bug introduced by
It seems like genericLoader() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $this->/** @scrutinizer ignore-call */ 
56
               genericLoader();
Loading history...
56
57
        app(FormValidator::class)->validate(null, $this->form, $this);
58
        $this->inputAssignment();
59
60
        $this->setTimeStamps('created_at');
61
62
        $this->arr = $this->hookBeforeAdd($this->arr);
0 ignored issues
show
Bug introduced by
It seems like hookBeforeAdd() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        /** @scrutinizer ignore-call */ 
63
        $this->arr = $this->hookBeforeAdd($this->arr);
Loading history...
63
        $id = (int) $this->table()->insertGetId($this->arr);
0 ignored issues
show
Bug introduced by
It seems like table() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        $id = (int) $this->/** @scrutinizer ignore-call */ table()->insertGetId($this->arr);
Loading history...
64
        app(RelationHandler::class)->save($this->table, $id, $this->data_inputan);
65
        $this->hookAfterAdd($id);
0 ignored issues
show
Bug introduced by
It seems like hookAfterAdd() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        $this->/** @scrutinizer ignore-call */ 
66
               hookAfterAdd($id);
Loading history...
66
67
        event('cb.dataInserted', [$this->table, $id, YmdHis(), cbUser()]);
68
        $this->sendResponseForSave('alert_add_data_success');
69
    }
70
71
    public function postEditSave($id)
72
    {
73
        $id = (int) $id;
74
        $this->genericLoader();
75
76
        app(FormValidator::class)->validate($id, $this->form, $this);
77
        $this->inputAssignment($id);
78
79
        $this->setTimeStamps('updated_at');
80
81
        $this->arr = $this->hookBeforeEdit($this->arr, $id);
0 ignored issues
show
Bug introduced by
It seems like hookBeforeEdit() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        /** @scrutinizer ignore-call */ 
82
        $this->arr = $this->hookBeforeEdit($this->arr, $id);
Loading history...
82
        $this->findRow($id)->update($this->arr);
0 ignored issues
show
Bug introduced by
It seems like findRow() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        $this->/** @scrutinizer ignore-call */ 
83
               findRow($id)->update($this->arr);
Loading history...
83
        app(RelationHandler::class)->save($this->table, $id, $this->data_inputan);
84
        $this->hookAfterEdit($id);
0 ignored issues
show
Bug introduced by
It seems like hookAfterEdit() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $this->/** @scrutinizer ignore-call */ 
85
               hookAfterEdit($id);
Loading history...
85
86
        event('cb.dataUpdated', [$this->table, $id, YmdHis(), cbUser()]);
87
88
        $this->sendResponseForSave('alert_update_data_success');
89
    }
90
91
    private function sendResponseForSave($msg)
92
    {
93
        $this->return_url = $this->return_url ?: request('return_url');
94
        if ($this->return_url) {
95
            if (request('submit') == cbTrans('button_save_more')) {
96
                backWithMsg(cbTrans($msg), 'success');
97
            }
98
            CRUDBooster::redirect($this->return_url, cbTrans($msg), 'success');
99
        }
100
        if (request('submit') == cbTrans('button_save_more')) {
101
            CRUDBooster::redirect(CRUDBooster::mainpath('add'), cbTrans($msg), 'success');
102
        }
103
        CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans($msg), 'success');
104
    }
105
106
    private function setTimeStamps($col)
107
    {
108
        if (Schema::hasColumn($this->table, $col)) {
109
            $this->arr[$col] = YmdHis();
110
        }
111
    }
112
}