Passed
Push — master ( 09194d...95db02 )
by Yuichi
02:42
created

AbstractImport::setModel()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 13.1458

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 18
c 3
b 0
f 0
nc 12
nop 4
dl 0
loc 24
ccs 19
cts 21
cp 0.9048
crap 13.1458
rs 5.1754

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Sonar\Common\Imports;
3
4
use Symfony\Component\Yaml\Yaml;
5
use Symfony\Component\Yaml\Exception\ParseException;
6
use File;
7
8
abstract class AbstractImport
9
{
10
    protected $config;
11
12 7
    public function __construct()
13
    {
14 7
        $this->config = [];
15 7
    }
16
17 4
    public function setConfig($config_file)
18
    {
19 4
        $config = Yaml::parse($config_file);
20 4
        if ( is_array($config) === false  ) {
21
            $config = Yaml::parse(File::get($config_file));
22
        }
23 4
        if ( is_array($config) ) {
24 4
            $this->config = array_merge($this->config,$config);
25 4
        }
26 4
    }
27 1
    public function getConfig()
28
    {
29 1
        return $this->config;
30
    }
31 4
    public function setModels($models,$csv)
32
    {
33 4
        foreach ( $models as $table => $model ) {
34 4
            if ( is_array($model) ) {
35 4
                $total = count($model);
36 4
                for($i=0;$i<$total;$i++) {
37 4
                    if ( isset($this->config[$table][$i]) ) {
38 3
                        $this->setModel($model[$i],$this->config[$table][$i],$csv,$table);
39 1
                    } else {
40 1
                        throw new \Exception('設定ファイルが正しくないか、構成が異なっています。table=' . $table);
41
                    }
42 1
                }
43 1
            }
44 1
        }
45 1
        return true;
46
    }
47
48 3
    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...
49
    {
50 3
        foreach ( $config as $key => $rec ) {
51 3
            if ( isset($rec['func']) === true && $rec['func'] ) {
52 2
                $func = $rec['func'];
53 2
                $col = isset($rec['csv']) ? $rec['csv'] : null;
54 2
                if ( strpos($col,",") !== false ) {
55 2
                    $col = explode(",",$col);
56 2
                }
57 2
                if ( method_exists($this,$func) === true ) {
58 1
                    $this->$func($model,$key,$csv,$col);
59 1
                } else {
60 1
                    throw new \Exception(get_class($this) . 'に関数=' . $func . 'が実装されていません。');
61
                }
62 3
            } elseif ( isset($rec['csv']) && is_numeric($rec['csv']) === true && isset($csv[($rec['csv']+0)-1]) ) {
63 2
                $model->$key = $csv[($rec['csv']+0)-1];
64 3
            } elseif ( isset($rec['csv']) && is_numeric($rec['csv']) === false && isset($csv[$rec['csv']]) ) {
65
                $model->$key = $csv[$rec['csv']];
66
            } else {
67 1
                $model->$key = null;
68
            }
69 3
        }
70 2
        $model->save();
71 1
    }
72
73
}
74
75
76