Completed
Push — master ( e1b3cc...52b7dd )
by Enrico
03:03
created

SimpleCsvGateway::run()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 17
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 8
nop 0
crap 30
1
<?php
2
namespace BOTK;
3
4
/**
5
 * just a simple example class to integrate FactsFactory and a Data Model
6
 */
7
class SimpleCsvGateway 
8
{
9
	
10
	protected $options;
11
	protected $factsFactory;
12
	
13
	
14
	static public function factory(array $options)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
15
	{
16
		return new SimpleCsvGateway($options);
17
	}
18
	
19
	
20
	public function __construct(array $options)
21
	{
22
		assert(!empty($options['factsProfile']['model']) && isset($options['factsProfile']['options']) && is_array($options['factsProfile']['options'])) ;
23
		$defaults = array(
24
			'bufferSize' 		=> 2000,
25
			'skippFirstLine'	=> true,
26
			'fieldDelimiter' 	=> ','
27
		);
28
		$this->options = array_merge($defaults,$options);
29
		$this->factsFactory = new \BOTK\FactsFactory($options['factsProfile']);
30
	}
31
	
32
	
33
	protected function readRawData()
34
	{
35
		if( $this->factsFactory->tooManyErrors()) return false;
36
		
37
		return fgetcsv(STDIN, $this->options['bufferSize'], $this->options['fieldDelimiter']);
38
	}
39
	
40
	
41
	public function run()
42
	{
43
		echo $this->factsFactory->generateLinkedDataHeader();
44
		if($this->options['skippFirstLine']){ $this->readRawData();}
45
	
46
	    while ($rawdata = $this->readRawData()) {
47
	    	if($this->factsFactory->acceptable($rawdata)){
48
		    	try{
49
		    		$facts =$this->factsFactory->factualize($rawdata);
50
		    		echo $facts->asTurtle(), "\n";
51
		    	}catch (\Exception $e) {
52
					$this->factsFactory->addError($e->getMessage());
53
				    echo "\n# Caught exception: " ,  $e->getMessage(), "\n";
54
				}	    		
55
	    	}
56
	    }
57
		
58
		echo $this->factsFactory->generateLinkedDataFooter();		
59
	}
60
}
61