Passed
Push — master ( 3af51e...17417a )
by Yuichi
02:54
created

AbstractImport::getCsvCol()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 4
rs 9.2
1
<?php
2
namespace Sonar\Common\Imports;
3
4
use Symfony\Component\Yaml\Yaml;
5
use Illuminate\Filesystem\Filesystem;
6
7
abstract class AbstractImport
8
{
9
    protected $config;
10
    private $filesystem;
11
12 8
    public function __construct(Filesystem $filesystem = null)
13
    {
14 8
        $this->config = [];
15 8
        $this->filesystem = $filesystem ? $filesystem : new Filesystem;
16 8
    }
17
18 5
    public function setConfig($config_file, $is_force = false)
19
    {
20 5
        $config = Yaml::parse($config_file);
21 5
        if (is_array($config) === false || $is_force) {
22 1
            $config = Yaml::parse($this->filesystem->get($config_file));
23 1
        }
24 5
        if (is_array($config)) {
25 5
            $this->config = array_merge($this->config, $config);
26 5
        }
27 5
    }
28
29 1
    public function getConfig()
30
    {
31 1
        return $this->config;
32
    }
33
34 5
    public function setModels($models, $csv)
35
    {
36 5
        foreach ($models as $table => $model) {
37 5
            if (is_array($model) == false ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
38
                continue;
39
            }
40 5
            $total = count($model);
41 5
            for ($i = 0; $i < $total; $i++) {
42 5
                if (isset($this->config[$table][$i])) {
43 4
                    $this->setModel($model[$i], $this->config[$table][$i], $csv, $table);
44 2
                } else {
45 1
                    throw new \Exception('設定ファイルが正しくないか、構成が異なっています。table=' . $table);
46
                }
47 2
            }
48 2
        }
49 2
        return true;
50
    }
51
52 4
    public function setModel($model, $config, $csv, $table)
53
    {
54 4
        foreach ($config as $key => $rec) {
55 4
            $col = $this->getCsvCol($rec);
56 4
            $func = ( isset($rec['func']) === true && $rec['func'] ) ? $rec['func'] : null; 
57 4
            if (method_exists($this, $func) === true) {
58 1
                $this->$func($model, $key, $csv, $col);
59 4
            } elseif ( $func && method_exists($this, $func) === false) {
60 1
                throw new \Exception(get_class($this) . 'に関数=' . $func . 'が実装されていません。(table=' . $table . ')');
61 4
            } elseif (isset($rec['csv']) === true && isset($csv[$col]) === true) {
62 3
                $model->$key = $csv[$col];
63 3
            } else {
64 2
                $model->$key = null;
65
            }
66 4
        }
67 3
        $model->save();
68 2
    }
69
70 4
    private function getCsvCol($rec)
71
    {
72 4
        $col = isset($rec['csv']) ? $rec['csv'] : null;
73 4
        if (strpos($col, ",") !== false) {
74 1
            $col = explode(",", $col);
75 4
        } elseif ( is_numeric($col) === true ) {
76 2
            $col = ($col + 0 ) -1;
77 2
        } 
78 4
        return $col;
79
    }
80
81
}
82
83
84