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

ExportStateTrait::getExport()   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 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
}