ProcessCsvIterator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 58
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A ensureProcessStarted() 0 22 3
A retrieveNextCsvRow() 0 5 1
A __destruct() 0 4 1
1
<?php
2
3
namespace itertools;
4
5
use InvalidArgumentException;
6
7
8
class ProcessCsvIterator extends AbstractCsvIterator
9
{
10
	protected $cmd;
11
	protected $cwd;
12
	protected $env;
13
	protected $otherOptions;
14
15
	protected $processHandle;
16
	protected $stdOutHandle;
17
	protected $processStarted = false;
18
19
	public function __construct($cmd, array $options = array(), $cwd = null, $env = null, array $otherOptions = null)
20
	{
21
		$defaultOptions = array(
22
			'length' => 0,
23
		);
24
		$this->options = array_merge($defaultOptions, $options);
25
		$this->cmd = $cmd;
26
		$this->cwd = $cwd;
27
		$this->env = $env;
28
		$this->otherOptions = $otherOptions;
29
		parent::__construct(array_diff_key($options, $defaultOptions));
30
	}
31
32
	protected function ensureProcessStarted()
33
	{
34
		if($this->processStarted) {
35
			return;
36
		}
37
38
		$descriptorSpec = array(
39
			0 => array('pipe', 'r'),
40
			1 => array('pipe', 'w'),
41
		);
42
43
		$this->processHandle = proc_open($this->cmd, $descriptorSpec, $pipes, $this->cwd, $this->env, $this->otherOptions);
44
45
		if(! is_resource($this->processHandle)) {
46
			throw new InvalidArgumentException("Unable to open process: $cmd");
47
		}
48
49
		fclose($pipes[0]);
50
		$this->stdOutHandle = $pipes[1];
51
52
		$this->processStarted = true;
53
	}
54
55
	public function retrieveNextCsvRow()
56
	{
57
		$this->ensureProcessStarted();
58
		return fgetcsv($this->stdOutHandle, $this->options['length'], $this->options['delimiter'], $this->options['enclosure'], $this->options['escape']);
59
	}
60
61
	public function __destruct()
62
	{
63
		proc_close($this->processHandle);
64
	}
65
}
66
 
67