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

ImportStateTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 26.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 20
loc 76
ccs 29
cts 31
cp 0.9355
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getImport() 20 20 1
B postImport() 0 44 6

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 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
}