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

JSONParser::chunk()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 7
nop 2
dl 0
loc 28
ccs 15
cts 16
cp 0.9375
crap 6.0087
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sergio.rodenas
5
 * Date: 14/5/18
6
 * Time: 11:02
7
 */
8
9
namespace Rodenastyle\StreamParser\Parsers;
10
11
12
use Rodenastyle\StreamParser\Exceptions\StopParseException;
13
use Rodenastyle\StreamParser\Services\JsonCollectionParser as Parser;
14
use Rodenastyle\StreamParser\StreamParserInterface;
15
use Tightenco\Collect\Support\Collection;
16
17
class JSONParser implements StreamParserInterface
18
{
19
	protected $reader, $source;
20
21
	public function __construct()
22
	{
23
		Collection::macro('recursive', function () {
24 5
			return $this->map(function ($value) {
0 ignored issues
show
Bug introduced by
The method map() does not exist on Rodenastyle\StreamParser\Parsers\JSONParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
			return $this->/** @scrutinizer ignore-call */ map(function ($value) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25 5
				if (is_array($value) || is_object($value)) {
26 5
					return (new Collection($value))->recursive();
27
				}
28 5
				return $value;
29 5
			});
30 5
		});
31 5
	}
32
33 5
	public function from(String $source): StreamParserInterface
34
	{
35 5
		$this->source = $source;
36
37 5
		return $this;
38
	}
39
40 5
	public function each(callable $function)
41
	{
42 5
		$this->start();
43
		try {
44 5
			$this->reader->parse($this->source, function(array $item) use ($function){
45 5
				if($function((new Collection($item))->recursive()) === false) {
46 1
					throw new StopParseException();
47
				}
48 5
			});
49 1
		} catch (StopParseException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
50
		}
51 5
	}
52
53 5
	public function chunk($count, callable $function)
54
	{
55 5
		if($count <= 0) {
56
			return;
57
		}
58
59 5
		$this->start();
60
		try {
61 5
			$chunk = new Collection();
62
63 5
			$this->reader->parse($this->source, function(array $item) use ($function, &$chunk, &$count){
64 5
				$chunk->push((new Collection($item))->recursive());
65
66 5
				if($chunk->count() >= $count) {
67 5
					$stop = $function($chunk) === false;
68
69 5
					$chunk = new Collection();
70
71 5
					if($stop) {
72 1
						throw new StopParseException();
73
					}
74
				}
75 5
			});
76
77 4
			if($chunk->count() > 0) {
78 4
				$function($chunk);
79
			}
80 1
		} catch (StopParseException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
81
		}
82 5
	}
83
84 5
	private function start()
85
	{
86 5
		$this->reader = new Parser();
87
88 5
		return $this;
89
	}
90
}
91