Passed
Push — master ( 0eb049...b6cfd3 )
by Yuichi
01:50
created

AbstractImport::setModels()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 3
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 5.025
rs 9.3888
c 0
b 0
f 0
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 5
            $config = Yaml::parse($this->filesystem->get($config_file));
23
        }
24 5
        if (is_array($config)) {
25 5
            $this->config = array_merge($this->config, $config);
26
        }
27 5
    }
28
29 1
    public function getConfig()
30
    {
31 1
        return $this->config;
32
    }
33
34 5
    public function setModels($models, $csv,$is_save=true)
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,$is_save);
44
                } else {
45 1
                    throw new \Exception('設定ファイルが正しくないか、構成が異なっています。table=' . $table);
46
                }
47
            }
48
        }
49 2
        return true;
50
    }
51
52 4
    public function setModel($model, $config, $csv, $table,$is_save=true)
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
            } else {
64 2
                $model->$key = null;
65
            }
66
        }
67 3
        if ( $is_save ) {
68 3
            $model->save();
69
        }
70 2
    }
71
72 4
    private function getCsvCol($rec)
73
    {
74 4
        $col = isset($rec['csv']) ? $rec['csv'] : null;
75 4
        if (strpos($col, ",") !== false) {
76 1
            $col = explode(",", $col);
77 4
        } elseif ( is_numeric($col) === true ) {
78 2
            $col = ($col + 0 ) -1;
79
        } 
80 4
        return $col;
81
    }
82
83
}
84
85
86