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

BaseExport::setRange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.9332
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Distilleries\Expendable\Exports;
3
4
use Carbon\Carbon;
5
use Distilleries\Expendable\Contracts\ExportContract;
6
use Maatwebsite\Excel\Concerns\Exportable;
7
use Maatwebsite\Excel\Concerns\FromQuery;
8
use Maatwebsite\Excel\Concerns\WithHeadings;
9
10
class BaseExport implements FromQuery, WithHeadings, ExportContract
11
{
12
    use Exportable;
13
14
    /**
15
     * @var \Distilleries\Expendable\Models\BaseModel
16
     */
17
    private $model;
18
19
    /**
20
     * @var Carbon
21
     */
22
    private $start;
23
24
    /**
25
     * @var Carbon
26
     */
27
    private $end;
28
29 4
    public function __construct($model, $data)
30
    {
31 4
        $this->setModel($model);
32 4
        $this->setRange($data);
33
    }
34
35 4
    public function export($fileName)
36
    {
37 4
        return $this->download($fileName);
38
    }
39
40
    /**
41
     * @return \Illuminate\Database\Query\Builder
42
     */
43 4
    public function query()
44
    {
45 4
        return $this->model->betweenCreate($this->start, $this->end);
0 ignored issues
show
Bug introduced by
The method betweenCreate() does not exist on Distilleries\Expendable\Models\BaseModel. Did you maybe mean scopeBetweenCreate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
    }
47
48
    /**
49
     * @param mixed $model
50
     * @return $this
51
     */
52 4
    public function setModel($model)
53
    {
54 4
        $this->model = $model;
55
56 4
        return $this;
57
    }
58
59
    /**
60
     * @param array $data
61
     * @return $this
62
     */
63 4
    public function setRange($data)
64
    {
65 4
        foreach ($data['range'] as $key => $date)
66
        {
67 4
            Carbon::parse($date);
68 4
            $this->$key = Carbon::parse($date);
69
        }
70
71 4
        return $this;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 4
    public function headings(): array
78
    {
79 4
        return \Illuminate\Support\Facades\Schema::getColumnListing($this->model->getTable());
80
    }
81
}