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

ImportStateTrait::getImport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 20
loc 20
ccs 11
cts 11
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php namespace Distilleries\Expendable\States;
2
3
use Distilleries\Expendable\Formatter\Message;
4
use Illuminate\Http\Request;
5
use \View, \FormBuilder, \Input, \Redirect, \Lang, \File, \App;
6
7
trait ImportStateTrait {
8
9
    protected $import_form = 'Distilleries\Expendable\Http\Forms\Import\ImportForm';
10
11
    // ------------------------------------------------------------------------------------------------
12
    // ------------------------------------------------------------------------------------------------
13
    // ------------------------------------------------------------------------------------------------
14
15 4 View Code Duplication
    public function getImport()
16
    {
17 4
        $form = FormBuilder::create($this->import_form, [
18 4
            '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...
19
        ]);
20
21 4
        $form_content = view('expendable::admin.form.components.formgenerator.import', [
22 4
            'form' => $form
23
        ]);
24 4
        $content      = view('expendable::admin.form.state.form', [
25
26 4
        ]);
27
28 4
        $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...
29 4
        'form'=>$form_content,
30 4
        'content'=>$content,
31
        ]);
32
33 4
        return $this->layoutManager->render();
34
    }
35
36
    // ------------------------------------------------------------------------------------------------
37
38 12
    public function postImport(Request $request)
39
    {
40
41 12
        $form = FormBuilder::create($this->import_form, [
42 12
            'model' => $this->model
43
        ]);
44
45
46 12
        if ($form->hasError())
47
        {
48
            return $form->validateAndRedirectBack();
49
        }
50
51
52 12
        $file = $request->get('file');
53 12
        $file = urldecode(preg_replace('/\/app\//', '', $file));
54
55 12
        if (!app('files')->exists($file))
56
        {
57 4
            return redirect()->back()->with(Message::WARNING, [trans('expendable::errors.file_not_found')]);
58
        }
59
60 8
        $contract = ucfirst(app('files')->extension($file)) . 'ImporterContract';
61 8
        $exporter = app($contract);
62 8
        $data     = $exporter->getArrayDataFromFile($file);
63
64 8
        foreach ($data as $item)
65
        {
66 8
            $primary = isset($item[$this->model->getKeyName()]) ? $item[$this->model->getKeyName()] : '';
67 8
            if (empty($primary))
68
            {
69 8
                $this->model = new $this->model;
70 8
                $this->model = $this->model->create($item);
71
            } else
72
            {
73
                $this->model = $this->model->find($primary);
74 8
                $this->model->update($item);
75
            }
76
77
        }
78
79 8
        return redirect()->back()->with(Message::MESSAGE, [trans('expendable::success.imported')]);
80
81
    }
82
}