Passed
Push — master ( 5abdab...7936a2 )
by Todd
02:44
created

LoadDataInFile::setColMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Logikos\Csv\CsvToMysql;
4
5
class LoadDataInFile {
6
7
  private $delimiter   = ',';
8
  private $enclosure   = '"';
9
  private $eol         = "\\n";
10
  private $escape      = '\\';
11
  private $ignoreLines = 0;
12
  private $csvFile;
13
  private $tableName;
14
  private $columnMap;
15
  private $columnBinds = [];
16
17
  public function getDelimiter()           { return $this->delimiter;  }
18
  public function setDelimiter($delimiter) { $this->delimiter = $delimiter;  }
19
20
  public function getEnclosure()           { return $this->enclosure;  }
21
  public function setEnclosure($enclosure) { $this->enclosure = $enclosure;  }
22
23
  public function getEOL()                 { return $this->eol; }
24
  public function setEOL($eol)             { $this->eol = $eol; }
25
26
  public function getEscape()              { return $this->escape;  }
27
  public function setEscape($escape)       { $this->escape = $escape;  }
28
29
  public function getTableName()           { return $this->tableName;  }
30
  public function setTableName($name)      { $this->tableName = $name;  }
31
32
  public function getIgnoreLines()         { return $this->ignoreLines; }
33
  public function setIgnoreLines($count)   { $this->ignoreLines = $count; }
34
35
  public function getColMap()              { return $this->columnMap; }
36
  public function setColMap($columnMap)    { $this->columnMap = $columnMap; }
37
38
  public function getColumnBinds()         { return $this->columnBinds; }
39
  public function SetColumnBinds($binds)   { $this->columnBinds = $binds; }
40
41
  public function getCsvFile()             { return $this->csvFile; }
42
  public function setCsvFile($csvFile)     { $this->csvFile = $csvFile; }
43
44 1
  public function getQuery() {
45
    $sql  = "
46 1
      LOAD DATA LOCAL INFILE {$this->getCsvFile()}
47 1
      INTO TABLE {$this->getTableName()}
48 1
      FIELDS TERMINATED BY '{$this->getDelimiter()}'
49 1
      OPTIONALLY ENCLOSED BY '{$this->getEnclosure()}'
50 1
      ESCAPED BY '{$this->escape($this->getEscape())}'
51 1
      LINES TERMINATED BY '{$this->getEOL()}'
52 1
      IGNORE {$this->getIgnoreLines()} LINES
53 1
      ( {$this->getColVarsExpression()} )
54 1
      SET {$this->getSetExpression()}
55
    ";
56 1
    return $sql;
57
  }
58
59 1
  public function getBinds() {
60 1
    $binds = [];
61 1
    foreach ($this->columnBinds as $k=>$v) $binds[":{$k}"] = $v;
62 1
    return $binds;
63
  }
64
65 2
  public function getColVars() {
66 2
    for ($vars = [], $i=1; $i<=count($this->columnMap); $i++)
0 ignored issues
show
Performance Best Practice introduced
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
67 2
      array_push($vars, "@col{$i}");
68 2
    return $vars;
69
  }
70
71 4
  public function getSetExpression() {
72 4
    $expressions = [];
73 4
    if ($this->columnMap) array_push($expressions, $this->getColMapExpression());
74 4
    if ($this->columnBinds) array_push($expressions, $this->getBoundSetExpression());
0 ignored issues
show
Bug Best Practice introduced
The expression $this->columnBinds of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
75 4
    return $this->arrayToStringList($expressions);
76
  }
77
78 3
  protected function getColMapExpression() {
79 3
    $pieces = [];
80 3
    $colNum = 1;
81 3
    foreach ($this->columnMap as $colName) {
82 3
      if (!is_null($colName))
83 3
        array_push($pieces, "{$colName} = @col{$colNum}");
84 3
      $colNum++;
85
    }
86 3
    return implode(",\n", $pieces);
87
  }
88
89 1
  protected function getColVarsExpression() {
90 1
    return $this->arrayToStringList($this->getColVars());
91
  }
92
93 3
  protected function getBoundSetExpression() {
94 3
    $binds = [];
95 3
    foreach ($this->columnBinds as $k=>$v) {
96 3
      array_push($binds, "{$k} = :{$k}");
97
    }
98 3
    return $this->arrayToStringList($binds);
99
  }
100
101 4
  protected function arrayToStringList(array $items) {
102 4
    return implode(",\n", $items);
103
  }
104
105
  private function escape($string)      { return addslashes($string); }
106
}