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

BaseImport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 43
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
ccs 12
cts 12
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A importFromFile() 0 4 1
A model() 0 9 2
A setModel() 0 6 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
}