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

BaseImport::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Distilleries\Expendable\Imports;
3
4
use Distilleries\Expendable\Contracts\ImportContract;
5
use Maatwebsite\Excel\Concerns\Importable;
6
use Maatwebsite\Excel\Concerns\ToModel;
7
use Maatwebsite\Excel\Concerns\WithHeadingRow;
8
9
class BaseImport implements ToModel, WithHeadingRow, ImportContract
10
{
11
    use Importable;
12
13
    /**
14
     * @var \Distilleries\Expendable\Models\BaseModel
15
     */
16
    private $model;
17
18 4
    public function __construct($model)
19
    {
20 4
        $this->setModel($model);
21
    }
22
23 4
    public function importFromFile($filepath)
24
    {
25 4
        return $this->import($filepath);
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31 4
    public function model(array $row)
32
    {
33 4
        $data = [];
34 4
        foreach($this->model->getFillable() as $value)
35
        {
36 4
            $data[$value] = $row[$value];
37
        }
38 4
        return new $this->model($data);
39
    }
40
41
    /**
42
     * @param mixed $model
43
     * @return $this
44
     */
45 4
    public function setModel($model)
46
    {
47 4
        $this->model = $model;
48
49 4
        return $this;
50
    }
51
}