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

ImportStateTrait::getImport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 20
loc 20
ccs 11
cts 11
cp 1
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
}