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

ExportStateTrait::postExport()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 34
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
ccs 12
cts 14
cp 0.8571
rs 8.5806
cc 4
eloc 14
nc 5
nop 1
crap 4.0466
1
<?php namespace Distilleries\Expendable\States;
2
3
use \FormBuilder;
4
use Illuminate\Http\Request;
5
6
trait ExportStateTrait {
7
8
    protected $export_form = 'Distilleries\Expendable\Http\Forms\Export\ExportForm';
9
    // ------------------------------------------------------------------------------------------------
10
    // ------------------------------------------------------------------------------------------------
11
    // ------------------------------------------------------------------------------------------------
12
13 4 View Code Duplication
    public function getExport()
14
    {
15 4
        $form = FormBuilder::create($this->export_form, [
16 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...
17
        ]);
18
19 4
        $form_content = view('expendable::admin.form.components.formgenerator.export', [
20 4
            'form' => $form
21
        ]);
22 4
        $content      = view('expendable::admin.form.state.form', [
23
24 4
        ]);
25
26 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...
27 4
            'form'=>$form_content,
28 4
            'content'=>$content,
29
        ]);
30
31 4
        return $this->layoutManager->render();
32
    }
33
34
    // ------------------------------------------------------------------------------------------------
35
36 8
    public function postExport(Request $request)
37
    {
38
39 8
        $form = FormBuilder::create($this->export_form, [
40 8
            'model' => $this->model
41
        ]);
42
43
44 8
        if ($form->hasError())
45
        {
46 4
            return $form->validateAndRedirectBack();
47
        }
48
49
50 4
        $data = $request->all();
51
52 4
        foreach ($data['range'] as $key => $date)
53
        {
54 4
            $data['range'][$key] = date('Y-m-d', strtotime($date));
55
        }
56
57 4
        $result = $this->model->betweenCreate($data['range']['start'], $data['range']['end'])->get();
58
59 4
        if (!$result->isEmpty())
60
        {
61 4
            $exporter = app($data['type']);
62 4
            $file     = $exporter->export($result->toArray(), $data['range']['start'] . ' ' . $data['range']['end']);
63
64
            return $file;
65
        }
66
67
        return redirect()->to(action('\\' . get_class($this) . '@getExport'));
68
69
    }
70
}