ImportStateTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 22
c 1
b 0
f 0
dl 0
loc 56
ccs 22
cts 23
cp 0.9565
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getImport() 0 19 1
A postImport() 0 25 3
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
    public function getImport()
17
    {
18 2
        $form = FormBuilder::create($this->import_form, [
19 2
            'model' => $this->model
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([
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
}