Total Complexity | 6 |
Total Lines | 74 |
Duplicated Lines | 0 % |
Coverage | 66.67% |
Changes | 0 |
1 | <?php |
||
9 | class ChunkListener extends AbstractListener |
||
10 | { |
||
11 | /** |
||
12 | * The size of the chunk. |
||
13 | * |
||
14 | * @var int |
||
15 | */ |
||
16 | protected $size; |
||
17 | |||
18 | /** |
||
19 | * The callback processing the chunk. |
||
20 | * |
||
21 | * @var callable |
||
22 | */ |
||
23 | protected $callback; |
||
24 | |||
25 | /** |
||
26 | * The chunk of objects. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $chunk = []; |
||
31 | |||
32 | /** |
||
33 | * Set the dependencies. |
||
34 | * |
||
35 | * @param int $size |
||
36 | * @param callable $callback |
||
37 | */ |
||
38 | 9 | public function __construct(int $size, callable $callback) |
|
42 | 9 | } |
|
43 | |||
44 | /** |
||
45 | * Listen to the end of the object. |
||
46 | * |
||
47 | * @return void |
||
48 | */ |
||
49 | 9 | public function endObject() : void |
|
50 | { |
||
51 | 9 | parent::endObject(); |
|
52 | |||
53 | 9 | if (count($this->chunk) === $this->size) { |
|
54 | 9 | call_user_func($this->callback, $this->chunk); |
|
55 | $this->chunk = []; |
||
56 | } |
||
57 | 9 | } |
|
58 | |||
59 | /** |
||
60 | * Process the given extracted object. |
||
61 | * |
||
62 | * @param array $object |
||
63 | * @return void |
||
64 | */ |
||
65 | 9 | protected function processExtractedObject(array $object) : void |
|
66 | { |
||
67 | 9 | $this->chunk[] = $object; |
|
68 | 9 | } |
|
69 | |||
70 | /** |
||
71 | * Listen to the end of the document. |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | public function endDocument() : void |
||
83 | } |
||
84 | } |
||
85 |