Passed
Push — master ( e97712...5fe6af )
by Yuichi
02:30
created

AbstractImport::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace Sonar\Common\Imports;
3
4
use Symfony\Component\Yaml\Yaml;
5
use Symfony\Component\Yaml\Exception\ParseException;
6
use Illuminate\Filesystem\Filesystem;
7
8
abstract class AbstractImport
9
{
10
    protected $config;
11
    private $filesystem;
12
13 8
    public function __construct(Filesystem $filesystem=null)
14
    {
15 8
        $this->config = [];
16 8
        $this->filesystem = $filesystem ? $filesystem : new Filesystem;
17 8
    }
18
19 5
    public function setConfig($config_file,$is_force=false)
20
    {
21 5
        $config = Yaml::parse($config_file);
22 5
        if ( is_array($config) === false || $is_force ) {
23 1
            $config = Yaml::parse($this->filesystem->get($config_file));
24 1
        }
25 5
        if ( is_array($config) ) {
26 5
            $this->config = array_merge($this->config,$config);
27 5
        }
28 5
    }
29 1
    public function getConfig()
30
    {
31 1
        return $this->config;
32
    }
33 5
    public function setModels($models,$csv)
34
    {
35 5
        foreach ( $models as $table => $model ) {
36 5
            if ( is_array($model) ) {
37 5
                $total = count($model);
38 5
                for($i=0;$i<$total;$i++) {
39 5
                    if ( isset($this->config[$table][$i]) ) {
40 4
                        $this->setModel($model[$i],$this->config[$table][$i],$csv,$table);
41 2
                    } else {
42 1
                        throw new \Exception('設定ファイルが正しくないか、構成が異なっています。table=' . $table);
43
                    }
44 2
                }
45 2
            }
46 2
        }
47 2
        return true;
48
    }
49
50 4
    public function setModel($model,$config,$csv,$table)
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52 4
        foreach ( $config as $key => $rec ) {
53 4
            if ( isset($rec['func']) === true && $rec['func'] ) {
54 2
                $func = $rec['func'];
55 2
                $col = isset($rec['csv']) ? $rec['csv'] : null;
56 2
                if ( strpos($col,",") !== false ) {
57 2
                    $col = explode(",",$col);
58 2
                }
59 2
                if ( method_exists($this,$func) === true ) {
60 1
                    $this->$func($model,$key,$csv,$col);
61 1
                } else {
62 2
                    throw new \Exception(get_class($this) . 'に関数=' . $func . 'が実装されていません。');
63
                }
64 4
            } elseif ( isset($rec['csv']) && is_numeric($rec['csv']) === true && isset($csv[($rec['csv']+0)-1]) ) {
65 2
                $model->$key = $csv[($rec['csv']+0)-1];
66 4
            } elseif ( isset($rec['csv']) && is_numeric($rec['csv']) === false && isset($csv[$rec['csv']]) ) {
67 1
                $model->$key = $csv[$rec['csv']];
68 1
            } else {
69 2
                $model->$key = null;
70
            }
71 4
        }
72 3
        $model->save();
73 2
    }
74
75
}
76
77
78