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

ExportStateTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 37.74 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 20
loc 53
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getExport() 20 20 1
B postExport() 0 22 7

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 Carbon\Carbon;
4
use \FormBuilder;
5
use Illuminate\Http\Request;
6
use Distilleries\Expendable\Exports\BaseExport;
7
8
trait ExportStateTrait {
9
10
    protected $export_form = 'Distilleries\Expendable\Http\Forms\Export\ExportForm';
11
    // ------------------------------------------------------------------------------------------------
12
    // ------------------------------------------------------------------------------------------------
13
    // ------------------------------------------------------------------------------------------------
14
15 2 View Code Duplication
    public function getExport()
16
    {
17 2
        $form = FormBuilder::create($this->export_form, [
18 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...
19
        ]);
20
21 2
        $form_content = view('expendable::admin.form.components.formgenerator.export', [
22 2
            'form' => $form
23
        ]);
24 2
        $content      = view('expendable::admin.form.state.form', [
25
26 2
        ]);
27
28 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...
29 2
            'form'=>$form_content,
30 2
            'content'=>$content,
31
        ]);
32
33 2
        return $this->layoutManager->render();
34
    }
35
36
    // ------------------------------------------------------------------------------------------------
37
38 10
    public function postExport(Request $request)
39
    {
40
41 10
        $form = FormBuilder::create($this->export_form, [
42 10
            'model' => $this->model
43
        ]);
44
45
46 10
        if ($form->hasError())
47
        {
48 6
            return $form->validateAndRedirectBack();
49
        }
50
51
52 4
        $data = $request->all();
53 4
        $dateStart = !empty($data['range']) && !empty($data['range']['start']) ? $data['range']['start'] : Carbon::now()->format('Y-m-d');
54 4
        $dateEnd = !empty($data['range']) && !empty($data['range']['end']) ? $data['range']['end'] : Carbon::now()->format('Y-m-d');
55 4
        $type = !empty($data['type']) ? $data['type'] : 'csv';
56 4
        $filename = $dateStart . ' ' . $dateEnd . '.' . $type;
57
58 4
        return (new BaseExport($this->model, $data))->export($filename);
59
    }
60
}