ChunkListener::processExtractedObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\JsonObjects\Listeners;
4
5
/**
6
 * The chunk listener.
7
 *
8
 */
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 4
    public function __construct(int $size, callable $callback)
39
    {
40 4
        $this->size = $size;
41 4
        $this->callback = $callback;
42 4
    }
43
44
    /**
45
     * Listen to the end of the object.
46
     *
47
     * @return void
48
     */
49 4
    public function endObject(): void
50
    {
51 4
        parent::endObject();
52
53 4
        if (count($this->chunk) === $this->size) {
54 4
            call_user_func($this->callback, $this->chunk);
55 1
            $this->chunk = [];
56
        }
57 4
    }
58
59
    /**
60
     * Process the given extracted object.
61
     *
62
     * @param array $object
63
     * @return void
64
     */
65 4
    protected function processExtractedObject(array $object): void
66
    {
67 4
        $this->chunk[] = $object;
68 4
    }
69
70
    /**
71
     * Listen to the end of the document.
72
     *
73
     * @return void
74
     */
75 1
    public function endDocument(): void
76
    {
77 1
        if (!empty($this->chunk)) {
78 1
            call_user_func($this->callback, $this->chunk);
79 1
            $this->chunk = [];
80
        }
81
82 1
        parent::endDocument();
83 1
    }
84
}
85