Completed
Push — master ( 081071...48c049 )
by Enrico
01:58
created

SimpleCsvGateway   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 60
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 4 1
A __construct() 0 12 1
A readRawData() 0 5 1
C run() 0 24 7
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
	protected $currentRow = 0;
13
	
14
	
15
	public static function factory(array $options)
16
	{
17
		return new SimpleCsvGateway($options);
18
	}
19
	
20
	
21
	public function __construct(array $options)
22
	{
23
		$defaults = array(
24
			'factsProfile'		=> array(),
25
			'missingFactsIsError' => true, // if a missing fact must considered an error
26
			'bufferSize' 		=> 2000,
27
			'skippFirstLine'	=> true,
28
			'fieldDelimiter' 	=> ',',
29
		);
30
		$this->options = array_merge($defaults,$options);
31
		$this->factsFactory = new \BOTK\FactsFactory($options['factsProfile']);
32
	}
33
	
34
	
35
	protected function readRawData()
36
	{
37
		$this->currentRow++;
38
		return fgetcsv(STDIN, $this->options['bufferSize'], $this->options['fieldDelimiter']);
39
	}
40
	
41
	
42
	public function run()
43
	{
44
		echo $this->factsFactory->generateLinkedDataHeader();
45
	
46
	    while ($rawdata = $this->readRawData()) {
47
	    	if($this->currentRow==1 && $this->options['skippFirstLine']){
48
	    		echo "# Header skipped\n";
49
	    		continue;
50
			}
51
    		try {
52
    			$facts =$this->factsFactory->factualize($rawdata);
53
	    		echo $facts->asTurtleFragment(), "\n";
54
				$droppedFields = $facts->getDroppedFields();
55
		    	if(!empty($droppedFields) && $this->options['missingFactsIsError']) {
56
				    echo "\n# WARNING MISSING FACT on row {$this->currentRow}: dropped ".implode(",", $droppedFields)."\n";
57
					$this->factsFactory->addToCounter('error');
58
				}	    
59
			} catch (\BOTK\Exception\Warning $e) {
60
				echo "\n# WARNING on row {$this->currentRow}: ".$e->getMessage()."\n";
61
			} 
62
	    }
63
		
64
		echo $this->factsFactory->generateLinkedDataFooter();		
65
	}
66
}
67