Passed
Pull Request — master (#6)
by
unknown
02:26
created

JsonCollectionParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 31
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 22 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sergio.rodenas
5
 * Date: 15/5/18
6
 * Time: 19:58
7
 */
8
9
namespace Rodenastyle\StreamParser\Services;
10
11
12
use JsonCollectionParser\Parser;
13
use Rodenastyle\StreamParser\Exceptions\IncompleteParseException;
14
use JsonCollectionParser\Listener;
15
use JsonStreamingParser\Parser as StreamingParser;
16
17
class JsonCollectionParser extends Parser
18
{
19
	/**
20
	 * @param string $filePath Source file path
21
	 * @param callback|callable $itemCallback Callback
22
	 * @param bool $assoc Parse as associative arrays
23
	 *
24
	 * @throws \Exception
25
	 */
26 5
	public function parse($filePath, $itemCallback, $assoc = true)
27
	{
28 5
		$this->checkCallback($itemCallback);
29
30 5
		$stream = $this->openFile($filePath);
31
32
		try {
33 5
			$listener = new Listener($itemCallback, $assoc);
34 5
			$this->parser = new StreamingParser(
35 5
				$stream,
36 5
				$listener,
37 5
				$this->getOption('line_ending'),
38 5
				$this->getOption('emit_whitespace')
39
			);
40 5
			$this->parser->parse();
41 1
		} catch (\Exception $e) {
42 1
			fclose($stream);
43 1
			throw $e;
44
		}
45
46 4
		if( ! fclose($stream)){
47
			throw new IncompleteParseException();
48
		}
49
	}
50
}