Completed
Push — master ( 49d50b...c2310d )
by Maxime
11s
created

ImportStateTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 33.9 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
dl 20
loc 59
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4
ccs 22
cts 23
cp 0.9565
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getImport() 20 20 1
A postImport() 0 27 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Distilleries\Expendable\States;
2
3
use Distilleries\Expendable\Formatter\Message;
4
use Distilleries\Expendable\Imports\BaseImport;
5
use Illuminate\Http\Request;
6
use \View, \FormBuilder, \Input, \Redirect, \Lang, \File, \App;
7
8
trait ImportStateTrait {
9
10
    protected $import_form = 'Distilleries\Expendable\Http\Forms\Import\ImportForm';
11
12
    // ------------------------------------------------------------------------------------------------
13
    // ------------------------------------------------------------------------------------------------
14
    // ------------------------------------------------------------------------------------------------
15
16 2 View Code Duplication
    public function getImport()
17
    {
18 2
        $form = FormBuilder::create($this->import_form, [
19 2
            'model' => $this->model
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...
20
        ]);
21
22 2
        $form_content = view('expendable::admin.form.components.formgenerator.import', [
23 2
            'form' => $form
24
        ]);
25 2
        $content      = view('expendable::admin.form.state.form', [
26
27 2
        ]);
28
29 2
        $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...
30 2
        'form'=>$form_content,
31 2
        'content'=>$content,
32
        ]);
33
34 2
        return $this->layoutManager->render();
35
    }
36
37
    // ------------------------------------------------------------------------------------------------
38
39 6
    public function postImport(Request $request)
40
    {
41
42 6
        $form = FormBuilder::create($this->import_form, [
43 6
            'model' => $this->model
44
        ]);
45
46
47 6
        if ($form->hasError())
48
        {
49
            return $form->validateAndRedirectBack();
50
        }
51
52
53 6
        $file = $request->get('file');
54 6
        $file = urldecode($file);
55 6
        if (!app('files')->exists($file))
56
        {
57 2
            return redirect()->back()->with(Message::WARNING, [trans('expendable::errors.file_not_found')]);
58
        }
59 4
        $file = str_replace(storage_path('app/'), '', $file);
60
61 4
        (new BaseImport($this->model))->importFromFile($file);
62
63 4
        return redirect()->back()->with(Message::MESSAGE, [trans('expendable::success.imported')]);
64
65
    }
66
}