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

SimpleCsvGateway   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 54
ccs 0
cts 37
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 11 3
A readRawData() 0 6 2
B run() 0 19 5
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