Completed
Push — master ( 2215c7...0db5fc )
by Damian
10s
created

CsvTableWriter::putRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\SsPak\DataExtractor;
4
5
class CsvTableWriter implements TableWriter
6
{
7
8
	private $filename;
9
	private $handle;
10
	private $columns;
11
12
	function __construct($filename) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
		$this->filename = $filename;
14
	}
15
16
	function start($columns) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
		$this->open();
18
		$this->putRow($columns);
19
		$this->columns = $columns;
20
	}
21
22
	function finish() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
		$this->close();
24
	}
25
26
	function writeRecord($record) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
		if (!$this->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->columns 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...
28
			$this->start(array_keys($record));
29
		}
30
31
		$this->putRow($this->mapFromColumns($record));
32
	}
33
34
	private function mapFromColumns($record) {
35
		$row = [];
36
		foreach($this->columns as $i => $column)
37
		{
38
			$row[$i] = isset($record[$column]) ? $record[$column] : null;
39
		}
40
		return $row;
41
	}
42
43
	private function putRow($row) {
44
		return fputcsv($this->handle, $row);
45
	}
46
47
	private function open() {
48
		if ($this->handle) {
49
			fclose($this->handle);
50
			$this->handle = null;
51
		}
52
		$this->handle = fopen($this->filename, 'w');
53
		if (!$this->handle) {
54
			throw new \LogicException("Can't open $this->filename for writing.");
55
		}
56
	}
57
58
	private function close() {
59
		if ($this->handle) {
60
			fclose($this->handle);
61
			$this->handle = null;
62
		}
63
	}
64
}
65