FormStateTrait   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Test Coverage

Coverage 54%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 36
eloc 93
c 9
b 0
f 0
dl 0
loc 248
ccs 54
cts 100
cp 0.54
rs 9.52

13 Methods

Rating   Name   Duplication   Size   Complexity  
A postTranslation() 0 30 5
A isTranslatableModel() 0 3 1
A dataToSave() 0 3 1
A getView() 0 21 3
A getControllerNameForAction() 0 6 1
A afterSave() 0 3 1
A getEdit() 0 26 5
A beforeSave() 0 3 1
A save() 0 12 3
A getTranslation() 0 28 4
A saveTranslation() 0 3 1
A postEdit() 0 30 6
A findAutoDetectTranslation() 0 17 4
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 30
    protected function isTranslatableModel()
19
    {
20 30
        return method_exists($this->model, 'withoutTranslation');
21
    }
22
23 20
    protected function findAutoDetectTranslation($id, $orfail = true)
24
    {
25 20
        if ($orfail) {
26 20
            if ($this->isTranslatableModel()) {
27
                return $this->model->withoutTranslation()->findOrFail($id);
28
            } else {
29 20
                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 is not 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 14
    public function getEdit($id = '')
46
    {
47 14
        $model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model;
48 14
        $form  = FormBuilder::create(get_class($this->form), [
49 14
            'model' => $model
50
        ]);
51
52
53 14
        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 14
        $form_content = view('form-builder::form.components.formgenerator.full', [
61 14
            'form' => $form
62
        ]);
63
64 14
        $this->layoutManager->add([
65 14
            'content' => view('expendable::admin.form.state.form', [
66 14
                'form' => $form_content
67
            ])
68
        ]);
69
70 14
        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() targeting 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) {
0 ignored issues
show
introduced by
The condition $result != null is always false.
Loading history...
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) targeting 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) {
0 ignored issues
show
introduced by
The condition $result != null is always false.
Loading history...
133
            return $result;
134
        }
135
136
        return redirect()->to(action($this->getControllerNameForAction().'@getIndex'));
137
    }
138
139
    // ------------------------------------------------------------------------------------------------
140
141 18
    public function postEdit(Request $request)
142
    {
143
144 18
        $form = FormBuilder::create(get_class($this->form), [
145 18
            'model' => $this->model
146
        ]);
147
148
149 18
        if ($form->hasError()) {
150 10
            return $form->validateAndRedirectBack();
151
        }
152
153 8
        $result = $this->beforeSave();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->beforeSave() targeting 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 8
        if ($result != null) {
0 ignored issues
show
introduced by
The condition $result != null is always false.
Loading history...
156
            return $result;
157
        }
158
159 8
        $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) targeting 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 8
        if ($this->isTranslatableModel() && !$this->model->hasTranslation($this->model->getTable(), $this->model->getKey())) {
163
            $this->saveTranslation(app()->getLocale());
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

163
            $this->saveTranslation(app()->/** @scrutinizer ignore-call */ getLocale());
Loading history...
164
        }
165
166 8
        if ($result != null) {
0 ignored issues
show
introduced by
The condition $result != null is always false.
Loading history...
167
            return $result;
168
        }
169
170 8
        return redirect()->to(action($this->getControllerNameForAction().'@getIndex'));
171
172
    }
173
174
    // ------------------------------------------------------------------------------------------------
175
176 8
    protected function dataToSave(Request $request)
177
    {
178 8
        return $request->only($this->model->getFillable());
179
    }
180
181
    // ------------------------------------------------------------------------------------------------
182
183 8
    protected function beforeSave()
184
    {
185 8
        return null;
186
    }
187
188
    // ------------------------------------------------------------------------------------------------
189
190 8
    protected function afterSave()
191
    {
192 8
        return null;
193
    }
194
195
196
    // ------------------------------------------------------------------------------------------------
197
198 8
    protected function save($data, Request $request)
199
    {
200
201 8
        $primary = $request->get($this->model->getKeyName());
202 8
        if ($primary !== "0" && empty($primary)) {
203 8
            $this->model = $this->model->create($data);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
204
        } else {
205
            $this->model = $this->findAutoDetectTranslation($primary, false);
206
            $this->model->update($data);
207
        }
208
209 8
        return $this->afterSave();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->afterSave() targeting Distilleries\Expendable\...StateTrait::afterSave() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
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 8
    public function getView($id)
223
    {
224
225 8
        $model = (!empty($id) || $id === "0") ? $this->findAutoDetectTranslation($id) : $this->model;
226 8
        $form  = FormBuilder::create(get_class($this->form), [
227 8
            'model' => $model
228
        ]);
229
230 8
        $form_content = view('form-builder::form.components.formgenerator.info', [
231 8
            'form'  => $form,
232 8
            'id'    => $id,
233 8
            'route' => $this->getControllerNameForAction().'@',
234
        ]);
235
236 8
        $this->layoutManager->add([
237 8
            'content' => view('expendable::admin.form.state.form', [
238 8
                'form' => $form_content
239
            ])
240
        ]);
241
242 8
        return $this->layoutManager->render();
243
244
    }
245
246
    // ------------------------------------------------------------------------------------------------
247
248 16
    protected function getControllerNameForAction()
249
    {
250
251 16
        $action = explode('@', \Route::currentRouteAction());
252
253 16
        return '\\'.$action[0];
254
    }
255
}