Completed
Push — master ( 5dab40...ca534d )
by Maxime
120:18 queued 114:35
created

FormStateTrait::postEdit()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.3541

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 32
ccs 11
cts 14
cp 0.7856
rs 8.439
cc 6
eloc 14
nc 6
nop 1
crap 6.3541
1
<?php namespace Distilleries\Expendable\States;
2
3
use \FormBuilder;
4
use Illuminate\Http\Request;
5
6
trait FormStateTrait {
7
8
    /**
9
     * @var \Kris\LaravelFormBuilder\Form $form
10
     * Injected by the constructor
11
     */
12
    protected $form;
13
14
15
    // ------------------------------------------------------------------------------------------------
16
    // ------------------------------------------------------------------------------------------------
17
    // ------------------------------------------------------------------------------------------------
18 60
    protected function isTranslatableModel()
19
    {
20 60
        return method_exists($this->model, 'withoutTranslation');
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
23 40
    protected function findAutoDetectTranslation($id, $orfail = true)
24
    {
25 40
        if ($orfail) {
26 40
            if ($this->isTranslatableModel()) {
27
                return $this->model->withoutTranslation()->findOrFail($id);
28
            } else {
29 40
                return $this->model->findOrFail($id);
30
            }
31
        } else {
32
            if ($this->isTranslatableModel()) {
33
                return $this->model->withoutTranslation()->find($id);
34
            } else {
35
                return $this->model->find($id);
36
            }
37
        }
38
39
        return null;
0 ignored issues
show
Unused Code introduced by
return null; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
40
41
    }
42
43
    // ------------------------------------------------------------------------------------------------
44
45 28
    public function getEdit($id = '')
46
    {
47 28
        $model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model;
48 28
        $form  = FormBuilder::create(get_class($this->form), [
49 28
            'model' => $model
50
        ]);
51
52
53 28
        if ($this->isTranslatableModel()) {
54
            $local_overide = $this->model->getIso($model->getTable(), $model->id);
55
            if (!empty($local_overide)) {
56
                $form->add('local_override', 'hidden', ['default_value' => $local_overide]);
57
            }
58
        }
59
60 28
        $form_content = view('form-builder::form.components.formgenerator.full', [
61 28
            'form' => $form
62
        ]);
63
64 28
        $this->layoutManager->add([
0 ignored issues
show
Bug introduced by
The property layoutManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65 28
            'content' => view('expendable::admin.form.state.form', [
66 28
                'form' => $form_content
67
            ])
68
        ]);
69
70 28
        return $this->layoutManager->render();
71
    }
72
73
    // ------------------------------------------------------------------------------------------------
74
75
    public function getTranslation($iso, $id)
76
    {
77
78
        $id_element = $this->model->hasBeenTranslated($this->model->getTable(), $id, $iso);
79
        if (!empty($id_element)) {
80
            return redirect()->to(action($this->getControllerNameForAction() . '@getEdit', $id_element));
81
        }
82
83
        $model = (!empty($id) || $id === "0") ? $this->model->withoutTranslation()->findOrFail($id) : $this->model;
84
        $form  = FormBuilder::create(get_class($this->form), [
85
            'model' => $model
86
        ])
87
            ->remove('id')
88
            ->add('translation_iso', 'hidden', ['default_value' => $iso])
89
            ->add('translation_id_source', 'hidden', ['default_value' => $id])
90
            ->add('local_override', 'hidden', ['default_value' => $iso]);
91
92
        $form_content = view('form-builder::form.components.formgenerator.full', [
93
            'form' => $form
94
        ]);
95
96
        $this->layoutManager->add([
97
            'content' => view('expendable::admin.form.state.form', [
98
                'form' => $form_content
99
            ])
100
        ]);
101
102
        return $this->layoutManager->render();
103
    }
104
105
    // ------------------------------------------------------------------------------------------------
106
107
    public function postTranslation(Request $request)
108
    {
109
110
111
        $form = FormBuilder::create(get_class($this->form), [
112
            'model' => $this->model
113
        ]);
114
115
116
        if ($form->hasError()) {
117
            return $form->validateAndRedirectBack();
118
        }
119
120
        $result = $this->beforeSave();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->beforeSave() (which targets Distilleries\Expendable\...tateTrait::beforeSave()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
121
122
        if ($result != null) {
123
            return $result;
124
        }
125
126
        $result = $this->save($this->dataToSave($request), $request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->save($this->dataT...ve($request), $request) (which targets Distilleries\Expendable\...\FormStateTrait::save()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
127
128
        if ($this->isTranslatableModel()) {
129
            $this->saveTranslation($request->get('translation_iso'), $request->get('translation_id_source'));
130
        }
131
132
        if ($result != null) {
133
            return $result;
134
        }
135
136
        return redirect()->to(action($this->getControllerNameForAction() . '@getIndex'));
137
    }
138
139
    // ------------------------------------------------------------------------------------------------
140
141 36
    public function postEdit(Request $request)
142
    {
143
144 36
        $form = FormBuilder::create(get_class($this->form), [
145 36
            'model' => $this->model
146
        ]);
147
148
149 36
        if ($form->hasError()) {
150 20
            return $form->validateAndRedirectBack();
151
        }
152
153 16
        $result = $this->beforeSave();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->beforeSave() (which targets Distilleries\Expendable\...tateTrait::beforeSave()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
154
155 16
        if ($result != null) {
156
            return $result;
157
        }
158
159 16
        $result = $this->save($this->dataToSave($request), $request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->save($this->dataT...ve($request), $request) (which targets Distilleries\Expendable\...\FormStateTrait::save()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
160
161
162 16
        if ($this->isTranslatableModel() && !$this->model->hasTranslation($this->model->getTable(), $this->model->getKey())) {
163
            $this->saveTranslation(app()->getLocale());
164
        }
165
166 16
        if ($result != null) {
167
            return $result;
168
        }
169
170 16
        return redirect()->to(action($this->getControllerNameForAction() . '@getIndex'));
171
172
    }
173
174
    // ------------------------------------------------------------------------------------------------
175
176 16
    protected function dataToSave(Request $request)
177
    {
178 16
        return $request->only($this->model->getFillable());
179
    }
180
181
    // ------------------------------------------------------------------------------------------------
182
183 16
    protected function beforeSave()
184
    {
185 16
        return null;
186
    }
187
188
    // ------------------------------------------------------------------------------------------------
189
190 16
    protected function afterSave()
191
    {
192 16
        return null;
193
    }
194
195
196
    // ------------------------------------------------------------------------------------------------
197
198 16
    protected function save($data, Request $request)
199
    {
200
201 16
        $primary = $request->get($this->model->getKeyName());
202 16
        if ($primary !== "0" && empty($primary)) {
203 16
            $this->model = $this->model->create($data);
204
        } else {
205
            $this->model = $this->findAutoDetectTranslation($primary, false);
206
            $this->model->update($data);
207
        }
208
209 16
        return $this->afterSave();
210
    }
211
212
    // ------------------------------------------------------------------------------------------------
213
214
    protected function saveTranslation($translation_iso, $translation_id_source = 0)
215
    {
216
        $this->model->setTranslation($this->model->getKey(), $this->model->getTable(), $translation_id_source, $translation_iso);
217
    }
218
219
220
    // ------------------------------------------------------------------------------------------------
221
222 16
    public function getView($id)
223
    {
224
225 16
        $model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model;
226 16
        $form  = FormBuilder::create(get_class($this->form), [
227 16
            'model' => $model
228
        ]);
229
230 16
        $form_content = view('form-builder::form.components.formgenerator.info', [
231 16
            'form'  => $form,
232 16
            'id'    => $id,
233 16
            'route' => $this->getControllerNameForAction() . '@',
234
        ]);
235
236 16
        $this->layoutManager->add([
237 16
            'content' => view('expendable::admin.form.state.form', [
238 16
                'form' => $form_content
239
            ])
240
        ]);
241
242 16
        return $this->layoutManager->render();
243
244
    }
245
246
    // ------------------------------------------------------------------------------------------------
247
248 32
    protected function getControllerNameForAction()
249
    {
250
251 32
        $action = explode('@', \Route::currentRouteAction());
252
253 32
        return '\\' . $action[0];
254
    }
255
}